Java操作xml
dom解析XML
<?xml version="1.0" encoding="utf-8" ?>
<books>
<book category ="文学">
<title>西游记</title>
<author>吴承恩</author>
<publish>人民文学出版社</publish>
</book>
<book category ="历史">
<title>魏晋南北朝</title>
<author>陈寅格</author>
<publish>人民文学出版社</publish>
</book>
</books>
代码实现过程
package com.shanghai.xml;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
import static javax.xml.parsers.DocumentBuilderFactory.newDefaultInstance;
public class Driver {
public static void main(String[] args) {
// 创建新的XML文档对象
File xmlFile=new File("D:\\project\\CheckXML\\src\\main\\resources\\book.xml");
// 调用newDefaultInstance() 方法得到创建DOM 解析器工厂
DocumentBuilderFactory documentBuilderFactory=
newDefaultInstance();
try {
DocumentBuilder documentBuilder=documentBuilderFactory.newDocumentBuilder();
// 调用DOM解析器对象的parse()方法解析XML文档,得到文档的Docment对象
Document document=documentBuilder.parse(xmlFile);
document.getDocumentElement().normalize();
System.out.println("Root element:\t"+document.getDocumentElement().getNodeName());
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
// 逐级解析节点
NodeList nodeList=document.getElementsByTagName("book");
// 遍历结点,输出结果
for (int i = 0; i <nodeList.getLength() ; i++) {
Node node=nodeList.item(i);
System.out.println("Node name:\t"+node.getNodeName());
Element element= (Element) node;
System.out.println("\n\n**************************************\n\n");
if(node.getNodeType()==Element.ELEMENT_NODE){
System.out.println("book category:\t"+element.getAttribute("category"));
System.out.println("title name:\t"+element.getElementsByTagName("title").item(0).getTextContent());
System.out.println("author name:\t"+element.getElementsByTagName("author").item(0).getTextContent());
System.out.println("publish:\t"+element.getElementsByTagName("publish").item(0).getTextContent());
System.out.println("\n\n**************************************\n\n");
}
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
DOM4J解析XML示例
<?xml version="1.0" encoding="UTF-8" ?>
<图书信息>
<图书>
<书名>西游记</书名>
<作者>吴承恩</作者>
<出版社>人民文学出版社</出版社>
</图书>
<图书>
<书名>魏晋南北朝</书名>
<作者>陈寅格</作者>
<出版社>人民文学出版社</出版社>
</图书>
</图书信息>
代码实现详细过程
package com.dom4j;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.File;
import java.util.Iterator;
import java.util.List;
public class ReadXMLHandler {
public void readXML(String xmlPath){
// 指定要解析的文件
File file=new File(xmlPath);
// 选定解析方式
SAXReader saxReader=new SAXReader();
try {
// 具体解析XML文档
Document document=saxReader.read(file);
// 获取根节点
Element root=document.getRootElement();
Element foo;
//方法1
Iterator iterator0= root.elementIterator("图书");
while (iterator0.hasNext()){
foo= (Element) iterator0.next();
System.out.println("书名"+foo.elementText("书名"));
System.out.println("作者"+foo.elementText("作者"));
System.out.println("出版社"+foo.elementText("出版社"));
System.out.println("============================================");
}
//方法2
List nodes=root.elements("图书");
for (Iterator iterator1=nodes.iterator();iterator1.hasNext();) {
foo= (Element) iterator1.next();
for (Iterator iterator2=foo.elementIterator();iterator2.hasNext();) {
Element element= (Element) iterator2.next();
System.out.println(element.getName()+":"+element.getText()+":");
}
System.out.println("============================================");
}
} catch (DocumentException e) {
e.printStackTrace();
}
}
}
测试
package com.test;
import com.dom4j.ReadXMLHandler;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.File;
import java.util.Iterator;
public class Test {
public static void main(String[] args) {
String xmlFile="D:\\project\\CheckDom4j\\src\\main\\resources\\books.xml";
ReadXMLHandler readXMLHandler=new ReadXMLHandler();
readXMLHandler.readXML(xmlFile);
}
}
posted on 2018-11-12 18:15 Indian_Mysore 阅读(283) 评论(1) 编辑 收藏 举报