java解析xml 然后用jsp逐一展现xml的数据?
1个回答
展开全部
前段时间刚好自己随便写的一个解析XML的类,你只需要新建一个hashMap,把解析出来的数据放在map中,在页面上用EL表达式写出即可。。。祝你好运!
public class ParseXml
{
// 示例xml文件
/*
* <?xml version="1.0"?>
* <students>
<student>
<name>John</name>
* <grade>B</grade> <age>12</age> </student> <student> <name>Mary</name>
* <grade>A</grade> <age>11</age> </student> <student> <name>Simon</name>
* <grade>A</grade> <age>18</age>
* </student>
* </students>
*/
@SuppressWarnings("unused")
private static void getAllUserNames(String fileName) throws Exception
{
//实例化文档对象创建工厂
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//实例化文档创建类
DocumentBuilder builder = factory.newDocumentBuilder();
//要解析的文件
File file = new File(fileName);
if (file.exists())
{
//开始解析
Document doc = builder.parse(file);
//得到文档元素
Element docElement = doc.getDocumentElement();
System.out.println(docElement.getNodeName());
//根据标签名渠道节点集合
NodeList nodeList = doc.getElementsByTagName("student");
System.out.println("totle students:" + nodeList.getLength());
//遍历
if (nodeList != null && nodeList.getLength() > 0)
{
for (int i = 0; i < nodeList.getLength(); i++)
{
Node node = nodeList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE)
{
System.out.println("+=============");
Element element = (Element) node;
NodeList names = element.getElementsByTagName("name");
System.out.println("name:"
+ names.item(0).getChildNodes().item(0)
.getNodeValue());
NodeList grades = element.getElementsByTagName("grade");
System.out.println("grade:"
+ grades.item(0).getChildNodes().item(0)
.getNodeValue());
}
}
}
else
{
System.exit(1);
}
}
}
public static void main(String[] args) throws Exception
{
getAllUserNames("C:\\test.xml");
}
}
public class ParseXml
{
// 示例xml文件
/*
* <?xml version="1.0"?>
* <students>
<student>
<name>John</name>
* <grade>B</grade> <age>12</age> </student> <student> <name>Mary</name>
* <grade>A</grade> <age>11</age> </student> <student> <name>Simon</name>
* <grade>A</grade> <age>18</age>
* </student>
* </students>
*/
@SuppressWarnings("unused")
private static void getAllUserNames(String fileName) throws Exception
{
//实例化文档对象创建工厂
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//实例化文档创建类
DocumentBuilder builder = factory.newDocumentBuilder();
//要解析的文件
File file = new File(fileName);
if (file.exists())
{
//开始解析
Document doc = builder.parse(file);
//得到文档元素
Element docElement = doc.getDocumentElement();
System.out.println(docElement.getNodeName());
//根据标签名渠道节点集合
NodeList nodeList = doc.getElementsByTagName("student");
System.out.println("totle students:" + nodeList.getLength());
//遍历
if (nodeList != null && nodeList.getLength() > 0)
{
for (int i = 0; i < nodeList.getLength(); i++)
{
Node node = nodeList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE)
{
System.out.println("+=============");
Element element = (Element) node;
NodeList names = element.getElementsByTagName("name");
System.out.println("name:"
+ names.item(0).getChildNodes().item(0)
.getNodeValue());
NodeList grades = element.getElementsByTagName("grade");
System.out.println("grade:"
+ grades.item(0).getChildNodes().item(0)
.getNodeValue());
}
}
}
else
{
System.exit(1);
}
}
}
public static void main(String[] args) throws Exception
{
getAllUserNames("C:\\test.xml");
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询