http://blog.csdn.net/linghu_java/article/details/40155899
http://blog.csdn.net/linghu_java/article/details/40155899
http://blog.csdn.net/linghu_java/article/details/40155899
http://blog.csdn.net/linghu_java/article/details/40155899
关键字:Java解析xml、解析xml四种方法、DOM、SAX、JDOM、DOM4j、XPath
目前在Java中用于解析XML的技术很多,主流的有DOM、SAX、JDOM、DOM4j,下文主要介绍这4种解析XML文档技术的使用、优缺点及性能测试。
一、【基础知识——扫盲】
sax、dom是两种对xml文档进行解析的方法(没有具体实现,只是接口),所以只有它们是无法解析xml文档的;jaxp只是api,它进一步封装了sax、dom两种接口,并且提供了DomcumentBuilderFactory/DomcumentBuilder和SAXParserFactory/SAXParser(默认使用xerces解释器)。@L_502_14@二、【DOM、SAX、JDOM、DOM4j简单使用介绍】
1、【DOM(Document Object Model) 】由W3C提供的接口,它将整个XML文档读入内存,构建一个DOM树来对各个节点(Node)进行操作。
示例代码:
- <?xmlversion="1.0"encoding="UTF-8"?>
- <universityname="pku">
- collegename="c1">
- classname="class1"studentname="stu1"sex='male'age="21"/>
- studentname="stu2"sex='female'age="20"/>
- studentname="stu3"sex='female'age="20"</classclassname="class2"studentname="stu4"sex='male'age="19"studentname="stu5"sex='female'age="20"studentname="stu6"sex='female'age="21"collegecollegename="c2"classname="class3"studentname="stu7"sex='male'age="20"collegename="c3"university>
- packagetest.xml;
- importjava.io.File;
- importjava.io.FileNotFoundException;
- importjava.io.FileOutputStream;
- importjava.io.IOException;
- importjava.io.InputStream;
- importjavax.xml.parsers.DocumentBuilder;
- importjavax.xml.parsers.DocumentBuilderFactory;
- importjavax.xml.parsers.ParserConfigurationException;
- importjavax.xml.transform.Transformer;
- importjavax.xml.transform.TransformerConfigurationException;
- importjavax.xml.transform.TransformerException;
- importjavax.xml.transform.TransformerFactory;
- importjavax.xml.transform.dom.DOMSource;
- importjavax.xml.transform.stream.StreamResult;
- importorg.w3c.dom.Document;
- importorg.w3c.dom.Element;
- importorg.w3c.dom.Node;
- importorg.w3c.dom.NodeList;
- importorg.w3c.dom.Text;
- importorg.xml.sax.SAXException;
- /**
- *dom读写xml
- *@authorwhwang
- */
- publicclassTestDom{
- staticvoidmain(String[]args){
- read();
- //write();
- }
- voidread(){
- DocumentBuilderFactorydbf=DocumentBuilderFactory.newInstance();
- try{
- DocumentBuilderbuilder=dbf.newDocumentBuilder();
- InputStreamin=TestDom.class.getClassLoader().getResourceAsStream("test.xml");
- Documentdoc=builder.parse(in);
- //root<university>
- Elementroot=doc.getDocumentElement();
- if(root==null)return;
- System.err.println(root.getAttribute("name"));
- //allcollegenode
- NodeListcollegeNodes=root.getChildNodes();
- if(collegeNodes==for(inti=0;i<collegeNodes.getLength();i++){
- Nodecollege=collegeNodes.item(i);
- if(college!=null&&college.getNodeType()==Node.ELEMENT_NODE){
- System.err.println("\t"+college.getAttributes().getNamedItem("name").getNodeValue());
- //allclassnode
- NodeListclassNodes=college.getChildNodes();
- if(classNodes==continue;
- intj=0;j<classNodes.getLength();j++){
- Nodeclazz=classNodes.item(j);
- if(clazz!=null&&clazz.getNodeType()==Node.ELEMENT_NODE){
- System.err.println("\t\t"+clazz.getAttributes().getNamedItem("name").getNodeValue());
- //allstudentnode
- NodeListstudentNodes=clazz.getChildNodes();
- if(studentNodes==continue;
- intk=0;k<studentNodes.getLength();k++){
- Nodestudent=studentNodes.item(k);
- if(student!=null&&student.getNodeType()==Node.ELEMENT_NODE){
- System.err.print("\t\t\t"+student.getAttributes().getNamedItem("name").getNodeValue());
- System.err.print(""+student.getAttributes().getNamedItem("sex").getNodeValue());
- System.err.println(""+student.getAttributes().getNamedItem("age").getNodeValue());
- }
- }catch(ParserConfigurationExceptione){
- e.printStackTrace();
- catch(FileNotFoundExceptione){
- catch(SAXExceptione){
- catch(IOExceptione){
- voidwrite(){
- DocumentBuilderFactorydbf=DocumentBuilderFactory.newInstance();
- try{
- DocumentBuilderbuilder=dbf.newDocumentBuilder();
- InputStreamin=TestDom.class.getClassLoader().getResourceAsStream("test.xml");
- Documentdoc=builder.parse(in);
- //root<university>
- Elementroot=doc.getDocumentElement();
- return;
- //修改属性
- root.setAttribute("name","tsu");
- NodeListcollegeNodes=root.getChildNodes();
- if(collegeNodes!=null){
- 0;i<collegeNodes.getLength()-1;i++){
- //删除节点
- if(college.getNodeType()==Node.ELEMENT_NODE){
- StringcollegeName=college.getAttributes().getNamedItem("name").getNodeValue();
- if("c1".equals(collegeName)||"c2".equals(collegeName)){
- root.removeChild(college);
- elseif("c3".equals(collegeName)){
- ElementnewChild=doc.createElement("class");
- newChild.setAttribute("name","c4");
- college.appendChild(newChild);
- //新增节点
- ElementaddCollege=doc.createElement("college");
- addCollege.setAttribute("name","c5");
- root.appendChild(addCollege);
- Texttext=doc.createTextNode("text");
- addCollege.appendChild(text);
- //将修改后的文档保存到文件
- TransformerFactorytransFactory=TransformerFactory.newInstance();
- TransformertransFormer=transFactory.newTransformer();
- DOMSourcedomSource=newDOMSource(doc);
- Filefile=newFile("src/dom-modify.xml");
- if(file.exists()){
- file.delete();
- file.createNewFile();
- FileOutputStreamout=newFileOutputStream(file);
- StreamResultxmlResult=newStreamResult(out);
- transFormer.transform(domSource,xmlResult);
- System.out.println(file.getAbsolutePath());
- catch(TransformerConfigurationExceptione){
- catch(TransformerExceptione){
- }