xml解析(超全)---(二)
在用DOM解析一个比较复杂点的xml文件
有student.xml:
<?xml version="1.0" encoding="GB2312"?>
<result>
<class name="1">
???????? <student id="1">
??????????? <name>龙准</name>
??????????? <age>25</age>
???????? </student>
???????? <student id="2">
??????????? <name>龙准2</name>
??????????? <age>25</age>
???????? </student>
<student id="3">
??????????? <name>龙准3</name>
??????????? <age>25</age>
???????? </student>
</class>
<class name="2">
???????? <student id="1">
??????????? <name>廖丽1</name>
??????????? <age>25</age>
???????? </student>
???????? <student id="2">
??????????? <name>廖丽2</name>
??????????? <age>25</age>
???????? </student>
</class>
</result>
解析代码如下:
File file=new File("D://student.xml");
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document docment=builder.parse(file);
Element root=docment.getDocumentElement();
NodeList nl=root.getElementsByTagName("class");
System.out.println("总共有"+nl.getLength()+"个班级");
//得到节点的子节点
NodeList cls=root.getChildNodes();
for(int h=0;h<cls.getLength();h++){
??????? Node classes=cls.item(h);
??????? if(classes.getNodeType()==Node.ELEMENT_NODE){
?? //取得节点的属性值
//注意,节点的属性也是它的子节点。它的节点类型也是Node.ELEMENT_NODE
??????????? String name=classes.getAttributes().
getNamedItem("name").getNodeValue();
??????????? System.out.println("班级:"+name);
??????????? System.out.println("=======");
??? //轮循子节点
??????????? for(Node node=classes.getFirstChild();node!=null;node=node.getNextSibling()){
??????????? if(node.getNodeType()==Node.ELEMENT_NODE){
??????????????? if(node.getNodeName().equals("student")){
??????????????????? String id=node.getAttributes().getNamedItem("id")
.getNodeValue();
??????????????????? System.out.println("学生ID:"+id);
??????????????????? for(Node node1=node.getFirstChild();node1!=null;node1=node1.getNextSibling()){
??????????????????? if(node1.getNodeType()==Node.ELEMENT_NODE){
??????????????????????? if(node1.getNodeName().equals("name")){
??????????????????????? System.out.println("学生姓名:"+node1.getFirstChild().getNodeValue());
??????????????????????? }
??????????????????? if(node1.getNodeName().equals("age")){
??????????????????????? System.out.println("学生年龄:"+node1.getFirstChild().getNodeValue());
??????????????????? }
??????????? }
??????? }
??? }
}
}
}
}
打印结果:
总共有2个班级
班级:1
=======
学生ID:1
学生姓名:龙准
学生年龄:25
学生ID:2
学生姓名:龙准2
学生年龄:25
学生ID:3
学生姓名:龙准3
学生年龄:25
班级:2
=======
学生ID:1
学生姓名:廖丽1
学生年龄:25
学生ID:2
学生姓名:廖丽2
学生年龄:25
?
?
DOM+xpath解析XML文档
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE book SYSTEM "D:\workspace\XML\WebRoot\WEB-INF\book.dtd">
<book>
<bookname name="XML详解" font="GB2312"></bookname>
<authors>
<author name="张孝祥" sex="男" age="45"></author>
<author name="王勇" sex="男" age="35"></author>
<author name="王波" sex="男" age="30"></author>
</authors>
<price value="¥55"></price>
<publishdate>
<value>2009-08-18</value>
</publishdate>
</book>
?
?
?
try {
//读取book.xml到内存
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder dbd = dbf.newDocumentBuilder();
Document doc = dbd.parse(
new FileInputStream("D: \\book.xml"));
//通过XML获得book的authors的author子节点列表
XPathFactory f = XPathFactory.newInstance();
XPath path = f.newXPath();
NodeList authors= (NodeList) path.evaluate(
"book/authors/author", doc,XPathConstants.NODESET);
System.out.println(authors.getLength());
//遍历取到的元素
if(authors!=null){
?? for(int i=0;i<authors.getLength();i++){
?? Node author = authors.item(i);
?? int n = i + 1;
?? System.out.print(n+". 名字:"+author.getNodeName());
?? System.out.println();
? }
}
//获得book的authors的第一个子节点,注意NODESET和NODE的区别
Node author= (Node) path.evaluate("book/authors/author", doc,XPathConstants.NODE);
System.out.println(" 名称:"+author.getNodeName());
System.out.println(" 内容:"+author.getTextContent());//如果存在内容则返回内容,不存在则返回空
//获取节点的属性
NamedNodeMap attr = author.getAttributes();
System.out.println(" 该节点的属性个数"+attr.getLength());
//遍历元素的属性
if(attr!=null){
? for(int i=0;i<attr.getLength();i++){
??? int n = i + 1;
??? System.out.print(" 属性"+n+" 名称:"+attr.item(i).getNodeName());
??? System.out.print(" 值:"+attr.item(i).getNodue());
??? System.out.print(" 类型:"+attr.item(i).getNodeType());
??? System.out.println();
?}
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (XPathExpressionException e) {
e.printStackTrace();
}
}
}
DOM的增删改
?
以第一个xml文档为例:
新增一个user节点:
Element eleUser=doc.createElement(“user”);
Element eleName=doc.createElement(“name”);
Element eleAge=doc.createElement(“age”);
?
Text nametext=doc.createTextNode(“new龙准”);
Text agetext=doc.createTextNode(“23”);
?
eleName.appendChild(nametext);
eleAge.appendChild(agetext);
?
eleUser.appendChild(eleName);
eleUser.appendChild(eleAge);
eleUser.setAttribute(“id”,”3”);
?
Element root=doc.getDocumentElement();
Root.appendChild(eleUser);
删除第一个user
NodeList users=root.getElementByTagName(“user”);
Root.removeChild(users.item(0));
修改第一个user的age值
Element e=root.getElementByTagName(“user”).item(0);
Node age=e.getElementByTagName(“age”).item(0);
Age.getFirstNode.setNodeValue=”24”;
?
以上操作全在内存中,并没有持久化到文件,如果需要持久化到文件需要用到transformerFactory
TransformerFactory transformerfactory=TransformerFactory.newInstance();
Tansformer transformer=transformerfactory.newTransformer();
//设置编码字符集
Transformer.setOutPutProperty(“encoding”,”utf-8”);
//获取源数据对象
DOMSource source=new DOMSource(doc);
//设置目标对象
File ff=new File(“d://2.xml”);
If(!ff.isexists){
?????? ff.createNewFile();
}
StreamResult sr=new StreamResult(ff);
transformer..transform(source,sr);
?
Xpath的详细介绍请看Xpath的pdf文档
DOM的相关操作到此为止,把以上所讲多练习,DOM操作xml文档就没问题了
原创
?