Java—解析XML的几种方式

来源:互联网 发布:怎么骗取淘宝运费险 编辑:程序博客网 时间:2024/06/10 19:48

DOM的全称是Document Object Model,也即文档对象模型。在应用程序中,基于DOM的XML分析器将一个XML文档转换成一个对象模型的集合(通常称DOM树),应用程序正是通过对这个对象模型的操作,来实现对XML文档数据的操作。通过DOM接口,应用程序可以在任何时候访问XML文档中的任何一部分数据,因此,这种利用DOM接口的机制也被称作随机访问机制。

DOM接口提供了一种通过分层对象模型来访问XML文档信息的方式,这些分层对象模型依据XML的文档结构形成了一棵节点树。无论XML文档中所描述的是什么类型的信息,即便是制表数据、项目列表或一个文档,利用DOM所生成的模型都是节点树的形式。也就是说,DOM强制使用树模型来访问XML文档中的信息。由于XML本质上就是一种分层结构,所以这种描述方法是相当有效的。

DOM树所提供的随机访问方式给应用程序的开发带来了很大的灵活性,它可以任意地控制整个XML文档中的内容。然而,由于DOM分析器把整个XML文档转化成DOM树放在了内存中,因此,当文档比较大或者结构比较复杂时,对内存的需求就比较高。而且,对于结构复杂的树的遍历也是一项耗时的操作。所以,DOM分析器对机器性能的要求比较高,实现效率不十分理想。不过,由于DOM分析器所采用的树结构的思想与XML文档的结构相吻合,同时鉴于随机访问所带来的方便,因此,DOM分析器还是有很广泛的使用价值的。

Java代码  收藏代码
  1. import java.io.File;  
  2.   
  3. import javax.xml.parsers.DocumentBuilder;  
  4. import javax.xml.parsers.DocumentBuilderFactory;  
  5.   
  6. import org.w3c.dom.Document;  
  7. import org.w3c.dom.Element;  
  8. import org.w3c.dom.NodeList;  
  9.   
  10. public class DomTest1  
  11. {  
  12.     public static void main(String[] args) throws Exception  
  13.     {  
  14.         // step 1: 获得dom解析器工厂(工作的作用是用于创建具体的解析器)  
  15.         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();  
  16.           
  17. //      System.out.println("class name: " + dbf.getClass().getName());  
  18.           
  19.         // step 2:获得具体的dom解析器  
  20.         DocumentBuilder db = dbf.newDocumentBuilder();  
  21.           
  22. //      System.out.println("class name: " + db.getClass().getName());  
  23.           
  24.         // step3: 解析一个xml文档,获得Document对象(根结点)  
  25.         Document document = db.parse(new File("candidate.xml"));  
  26.           
  27.         NodeList list = document.getElementsByTagName("PERSON");  
  28.           
  29.         for(int i = 0; i < list.getLength(); i++)  
  30.         {  
  31.             Element element = (Element)list.item(i);  
  32.               
  33.             String content = element.getElementsByTagName("NAME").item(0).getFirstChild().getNodeValue();  
  34.               
  35.             System.out.println("name:" + content);  
  36.               
  37.             content = element.getElementsByTagName("ADDRESS").item(0).getFirstChild().getNodeValue();  
  38.               
  39.             System.out.println("address:" + content);  
  40.               
  41.             content = element.getElementsByTagName("TEL").item(0).getFirstChild().getNodeValue();  
  42.               
  43.             System.out.println("tel:" + content);  
  44.               
  45.             content = element.getElementsByTagName("FAX").item(0).getFirstChild().getNodeValue();  
  46.               
  47.             System.out.println("fax:" + content);  
  48.               
  49.             content = element.getElementsByTagName("EMAIL").item(0).getFirstChild().getNodeValue();  
  50.               
  51.             System.out.println("email:" + content);  
  52.               
  53.             System.out.println("--------------------------------------");  
  54.         }  
  55.     }  
  56. }  

Java代码  收藏代码
  1. import java.io.File;  
  2.   
  3. import javax.xml.parsers.DocumentBuilder;  
  4. import javax.xml.parsers.DocumentBuilderFactory;  
  5.   
  6. import org.w3c.dom.Attr;  
  7. import org.w3c.dom.Comment;  
  8. import org.w3c.dom.Document;  
  9. import org.w3c.dom.Element;  
  10. import org.w3c.dom.NamedNodeMap;  
  11. import org.w3c.dom.Node;  
  12. import org.w3c.dom.NodeList;  
  13.   
  14. /** 
  15.  * 使用递归解析给定的任意一个xml文档并且将其内容输出到命令行上 
  16.  * @author zhanglong 
  17.  * 
  18.  */  
  19. public class DomTest3  
  20. {  
  21.     public static void main(String[] args) throws Exception  
  22.     {  
  23.         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();  
  24.         DocumentBuilder db = dbf.newDocumentBuilder();  
  25.           
  26.         Document doc = db.parse(new File("student.xml"));  
  27.         //获得根元素结点  
  28.         Element root = doc.getDocumentElement();  
  29.           
  30.         parseElement(root);  
  31.     }  
  32.       
  33.     private static void parseElement(Element element)  
  34.     {  
  35.         String tagName = element.getNodeName();  
  36.           
  37.         NodeList children = element.getChildNodes();  
  38.           
  39.         System.out.print("<" + tagName);  
  40.           
  41.         //element元素的所有属性所构成的NamedNodeMap对象,需要对其进行判断  
  42.         NamedNodeMap map = element.getAttributes();  
  43.           
  44.         //如果该元素存在属性  
  45.         if(null != map)  
  46.         {  
  47.             for(int i = 0; i < map.getLength(); i++)  
  48.             {  
  49.                 //获得该元素的每一个属性  
  50.                 Attr attr = (Attr)map.item(i);  
  51.                   
  52.                 String attrName = attr.getName();  
  53.                 String attrValue = attr.getValue();  
  54.                   
  55.                 System.out.print(" " + attrName + "=\"" + attrValue + "\"");  
  56.             }  
  57.         }  
  58.           
  59.         System.out.print(">");  
  60.           
  61.         for(int i = 0; i < children.getLength(); i++)  
  62.         {  
  63.             Node node = children.item(i);  
  64.             //获得结点的类型  
  65.             short nodeType = node.getNodeType();  
  66.               
  67.             if(nodeType == Node.ELEMENT_NODE)  
  68.             {  
  69.                 //是元素,继续递归  
  70.                 parseElement((Element)node);  
  71.             }  
  72.             else if(nodeType == Node.TEXT_NODE)  
  73.             {  
  74.                 //递归出口  
  75.                 System.out.print(node.getNodeValue());  
  76.             }  
  77.             else if(nodeType == Node.COMMENT_NODE)  
  78.             {  
  79.                 System.out.print("<!--");  
  80.                   
  81.                 Comment comment = (Comment)node;  
  82.                   
  83.                 //注释内容  
  84.                 String data = comment.getData();  
  85.                   
  86.                 System.out.print(data);  
  87.                   
  88.                 System.out.print("-->");  
  89.             }  
  90.         }  
  91.           
  92.         System.out.print("</" + tagName + ">");  
  93.     }  
  94. }  


sax:SAX的全称是Simple APIs for XML,也即XML简单应用程序接口。与DOM不同,SAX提供的访问模式是一种顺序模式,这是一种快速读写XML数据的方式。当使用SAX分析器对XML文档进行分析时,会触发一系列事件,并激活相应的事件处理函数,应用程序通过这些事件处理函数实现对XML文档的访问,因而SAX接口也被称作事件驱动接口。

 

Java代码  收藏代码
  1. import java.io.File;  
  2.   
  3. import javax.xml.parsers.SAXParser;  
  4. import javax.xml.parsers.SAXParserFactory;  
  5.   
  6. import org.xml.sax.Attributes;  
  7. import org.xml.sax.SAXException;  
  8. import org.xml.sax.helpers.DefaultHandler;  
  9.   
  10. public class SaxTest1  
  11. {  
  12.     public static void main(String[] args) throws Exception  
  13.     {  
  14.         //step1: 获得SAX解析器工厂实例  
  15.         SAXParserFactory factory = SAXParserFactory.newInstance();  
  16.           
  17.         //step2: 获得SAX解析器实例  
  18.         SAXParser parser = factory.newSAXParser();  
  19.           
  20.         //step3: 开始进行解析  
  21.         parser.parse(new File("student.xml"), new MyHandler());  
  22.           
  23.     }  
  24. }  
  25.   
  26. class MyHandler extends DefaultHandler  
  27. {  
  28.     @Override  
  29.     public void startDocument() throws SAXException  
  30.     {  
  31.         System.out.println("parse began");  
  32.     }  
  33.       
  34.     @Override  
  35.     public void endDocument() throws SAXException  
  36.     {  
  37.         System.out.println("parse finished");  
  38.     }  
  39.       
  40.     @Override  
  41.     public void startElement(String uri, String localName, String qName,  
  42.             Attributes attributes) throws SAXException  
  43.     {  
  44.         System.out.println("start element");  
  45.     }  
  46.       
  47.     @Override  
  48.     public void endElement(String uri, String localName, String qName)  
  49.             throws SAXException  
  50.     {  
  51.         System.out.println("finish element");  
  52.     }  
  53. }  

 

Java代码  收藏代码
  1. import java.io.File;  
  2. import java.util.Stack;  
  3.   
  4. import javax.xml.parsers.SAXParser;  
  5. import javax.xml.parsers.SAXParserFactory;  
  6.   
  7. import org.xml.sax.Attributes;  
  8. import org.xml.sax.SAXException;  
  9. import org.xml.sax.helpers.DefaultHandler;  
  10.   
  11. public class SaxTest2  
  12. {  
  13.     public static void main(String[] args) throws Exception  
  14.     {  
  15.         SAXParserFactory factory = SAXParserFactory.newInstance();  
  16.           
  17.         SAXParser parser = factory.newSAXParser();  
  18.           
  19.         parser.parse(new File("student.xml"), new MyHandler2());  
  20.     }  
  21. }  
  22.   
  23. class MyHandler2 extends DefaultHandler  
  24. {  
  25.     private Stack<String> stack = new Stack<String>();  
  26.       
  27.     private String name;  
  28.       
  29.     private String gender;  
  30.       
  31.     private String age;  
  32.       
  33.     @Override  
  34.     public void startElement(String uri, String localName, String qName,  
  35.             Attributes attributes) throws SAXException  
  36.     {  
  37.         stack.push(qName);  
  38.           
  39.         for(int i = 0; i < attributes.getLength(); i++)  
  40.         {  
  41.             String attrName = attributes.getQName(i);  
  42.             String attrValue = attributes.getValue(i);  
  43.               
  44.             System.out.println(attrName + "=" + attrValue);  
  45.         }  
  46.     }  
  47.       
  48.     @Override  
  49.     public void characters(char[] ch, int start, int length)  
  50.             throws SAXException  
  51.     {  
  52.         String tag = stack.peek();  
  53.           
  54.         if("姓名".equals(tag))  
  55.         {  
  56.             name = new String(ch, start,length);  
  57.         }  
  58.         else if("性别".equals(tag))  
  59.         {  
  60.             gender = new String(ch, start, length);  
  61.         }  
  62.         else if("年龄".equals(tag))  
  63.         {  
  64.             age = new String(ch, start, length);  
  65.         }  
  66.     }  
  67.       
  68.     @Override  
  69.     public void endElement(String uri, String localName, String qName)  
  70.             throws SAXException  
  71.     {  
  72.         stack.pop(); //表示该元素已经解析完毕,需要从栈中弹出  
  73.           
  74.         if("学生".equals(qName))  
  75.         {  
  76.             System.out.println("姓名:" + name);  
  77.             System.out.println("性别:" + gender);  
  78.             System.out.println("年龄:" + age);  
  79.               
  80.             System.out.println();  
  81.         }  
  82.           
  83.     }  
  84. }  

 

JDOM:

JDOM是一个开源项目,它基于树型结构,利用纯JAVA的技术对XML文档实现解析、生成、序列化以及多种操作。(http://jdom.org)

•JDOM 直接为JAVA编程服务。它利用更为强有力的JAVA语言的诸多特性(方法重载、集合概念等),把SAX和DOM的功能有效地结合起来。

•JDOM是用Java语言读、写、操作XML的新API函数。在直接、简单和高效的前提下,这些API函数被最大限度的优化。

 

 

jdom创建xml

Java代码  收藏代码
  1. import java.io.FileWriter;  
  2.   
  3. import org.jdom.Attribute;  
  4. import org.jdom.Comment;  
  5. import org.jdom.Document;  
  6. import org.jdom.Element;  
  7. import org.jdom.output.Format;  
  8. import org.jdom.output.XMLOutputter;  
  9.   
  10. public class JDomTest1  
  11. {  
  12.     public static void main(String[] args) throws Exception  
  13.     {  
  14.         Document document = new Document();  
  15.   
  16.         Element root = new Element("root");  
  17.   
  18.         document.addContent(root);  
  19.   
  20.         Comment comment = new Comment("This is my comments");  
  21.   
  22.         root.addContent(comment);  
  23.   
  24.         Element e = new Element("hello");  
  25.   
  26.         e.setAttribute("sohu""www.sohu.com");  
  27.   
  28.         root.addContent(e);  
  29.   
  30.         Element e2 = new Element("world");  
  31.   
  32.         Attribute attr = new Attribute("test""hehe");  
  33.   
  34.         e2.setAttribute(attr);  
  35.   
  36.         e.addContent(e2);  
  37.   
  38.         e2.addContent(new Element("aaa").setAttribute("a""b")  
  39.                 .setAttribute("x""y").setAttribute("gg""hh").setText("text content"));  
  40.   
  41.           
  42.         Format format = Format.getPrettyFormat();  
  43.           
  44.         format.setIndent("    ");  
  45. //      format.setEncoding("gbk");  
  46.           
  47.         XMLOutputter out = new XMLOutputter(format);  
  48.   
  49.         out.output(document, new FileWriter("jdom.xml"));  
  50.           
  51.     }  
  52. }  
 

JDOM解析xml

Java代码  收藏代码
  1. import java.io.File;  
  2. import java.io.FileOutputStream;  
  3. import java.util.List;  
  4.   
  5. import org.jdom.Attribute;  
  6. import org.jdom.Document;  
  7. import org.jdom.Element;  
  8. import org.jdom.input.SAXBuilder;  
  9. import org.jdom.output.Format;  
  10. import org.jdom.output.XMLOutputter;  
  11.   
  12. public class JDomTest2  
  13. {  
  14.     public static void main(String[] args) throws Exception  
  15.     {  
  16.         SAXBuilder builder = new SAXBuilder();  
  17.           
  18.         Document doc = builder.build(new File("jdom.xml"));  
  19.           
  20.         Element element = doc.getRootElement();  
  21.           
  22.         System.out.println(element.getName());  
  23.           
  24.         Element hello = element.getChild("hello");  
  25.           
  26.         System.out.println(hello.getText());  
  27.           
  28.         List list = hello.getAttributes();  
  29.           
  30.         for(int i = 0 ;i < list.size(); i++)  
  31.         {  
  32.             Attribute attr = (Attribute)list.get(i);  
  33.               
  34.             String attrName = attr.getName();  
  35.             String attrValue = attr.getValue();  
  36.               
  37.             System.out.println(attrName + "=" + attrValue);  
  38.         }  
  39.           
  40.         hello.removeChild("world");  
  41.           
  42.         XMLOutputter out = new XMLOutputter(Format.getPrettyFormat().setIndent("    "));  
  43.           
  44.           
  45.         out.output(doc, new FileOutputStream("jdom2.xml"));       
  46.           
  47.     }  
  48. }  
 

 

Dom4j

 

Java代码  收藏代码
  1. import java.io.FileOutputStream;  
  2. import java.io.FileWriter;  
  3.   
  4. import org.dom4j.Document;  
  5. import org.dom4j.DocumentHelper;  
  6. import org.dom4j.Element;  
  7. import org.dom4j.io.OutputFormat;  
  8. import org.dom4j.io.XMLWriter;  
  9.   
  10. public class Test1  
  11. {  
  12.     public static void main(String[] args) throws Exception  
  13.     {  
  14.         // 创建文档并设置文档的根元素节点 :第一种方式  
  15.         // Document document = DocumentHelper.createDocument();  
  16.         //  
  17.         // Element root = DocumentHelper.createElement("student");  
  18.         //  
  19.         // document.setRootElement(root);  
  20.   
  21.         // 创建文档并设置文档的根元素节点 :第二种方式  
  22.         Element root = DocumentHelper.createElement("student");  
  23.         Document document = DocumentHelper.createDocument(root);  
  24.   
  25.         root.addAttribute("name""zhangsan");  
  26.   
  27.         Element helloElement = root.addElement("hello");  
  28.         Element worldElement = root.addElement("world");  
  29.   
  30.         helloElement.setText("hello");  
  31.         worldElement.setText("world");  
  32.   
  33.         helloElement.addAttribute("age""20");  
  34.   
  35.         XMLWriter xmlWriter = new XMLWriter();  
  36.         xmlWriter.write(document);  
  37.           
  38.         OutputFormat format = new OutputFormat("    "true);  
  39.           
  40.         XMLWriter xmlWriter2 = new XMLWriter(new FileOutputStream("student2.xml"), format);  
  41.         xmlWriter2.write(document);  
  42.           
  43.         XMLWriter xmlWriter3 = new XMLWriter(new FileWriter("student3.xml"), format);  
  44.           
  45.         xmlWriter3.write(document);  
  46.         xmlWriter3.close();  
  47.   
  48.     }  
  49. }  
 

Java代码  收藏代码
  1. import java.io.File;  
  2. import java.util.Iterator;  
  3. import java.util.List;  
  4.   
  5. import javax.xml.parsers.DocumentBuilder;  
  6. import javax.xml.parsers.DocumentBuilderFactory;  
  7.   
  8. import org.dom4j.Document;  
  9. import org.dom4j.Element;  
  10. import org.dom4j.io.DOMReader;  
  11. import org.dom4j.io.SAXReader;  
  12.   
  13. public class Test2  
  14. {  
  15.     public static void main(String[] args) throws Exception  
  16.     {  
  17.         SAXReader saxReader = new SAXReader();  
  18.           
  19.         Document doc = saxReader.read(new File("student2.xml"));  
  20.           
  21.         Element root = doc.getRootElement();  
  22.           
  23.         System.out.println("root element: " + root.getName());  
  24.           
  25.         List childList = root.elements();  
  26.           
  27.         System.out.println(childList.size());  
  28.           
  29.         List childList2 = root.elements("hello");  
  30.           
  31.         System.out.println(childList2.size());  
  32.           
  33.         Element first = root.element("hello");  
  34.           
  35.         System.out.println(first.attributeValue("age"));  
  36.           
  37.         for(Iterator iter = root.elementIterator(); iter.hasNext();)  
  38.         {  
  39.             Element e = (Element)iter.next();  
  40.               
  41.             System.out.println(e.attributeValue("age"));  
  42.         }  
  43.           
  44.         System.out.println("---------------------------");  
  45.           
  46.         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();  
  47.         DocumentBuilder db = dbf.newDocumentBuilder();  
  48.         org.w3c.dom.Document document = db.parse(new File("student2.xml"));  
  49.           
  50.         DOMReader domReader = new DOMReader();  
  51.           
  52.         //将JAXP的Document转换为dom4j的Document  
  53.         Document d = domReader.read(document);  
  54.           
  55.         Element rootElement = d.getRootElement();  
  56.           
  57.         System.out.println(rootElement.getName());  
  58.   
  59.     }  
  60. }  

 

Java代码  收藏代码
  1. import java.io.FileWriter;  
  2.   
  3. import org.jdom.Attribute;  
  4. import org.jdom.Document;  
  5. import org.jdom.Element;  
  6. import org.jdom.output.Format;  
  7. import org.jdom.output.XMLOutputter;  
  8.   
  9. public class Test3  
  10. {  
  11.     public static void main(String[] args) throws Exception  
  12.     {  
  13.         Document document = new Document();  
  14.   
  15.         Element root = new Element("联系人列表").setAttribute(new Attribute("公司",  
  16.                 "A集团"));  
  17.   
  18.         document.addContent(root);  
  19.           
  20.         Element contactPerson = new Element("联系人");  
  21.           
  22.         root.addContent(contactPerson);  
  23.   
  24.         contactPerson  
  25.                 .addContent(new Element("姓名").setText("张三"))  
  26.                 .addContent(new Element("公司").setText("A公司"))  
  27.                 .addContent(new Element("电话").setText("021-55556666"))  
  28.                 .addContent(  
  29.                         new Element("地址")  
  30.                                 .addContent(new Element("街道").setText("5街"))  
  31.                                 .addContent(new Element("城市").setText("上海"))  
  32.                                 .addContent(new Element("省份").setText("上海市")));  
  33.   
  34.         XMLOutputter output = new XMLOutputter(Format.getPrettyFormat()  
  35.                 .setIndent("    ").setEncoding("gbk"));  
  36.   
  37.         output.output(document, new FileWriter("contact.xml"));  
  38.   
  39.     }  
  40. }  

附:

XML现在已经成为一种通用的数据交换格式,它的平台无关性,语言无关性,系统无关性,给数据集成与交互带来了极大的方便。对于XML本身的语法知识与技术细节,需要阅读相关的技术文献,这里面包括的内容有DOM(Document Object Model),DTD(Document Type Definition),SAX(Simple API for XML),XSD(Xml Schema Definition),XSLT(Extensible Stylesheet Language Transformations),具体可参阅w3c官方网站文档http://www.w3.org获取更多信息。

XML在不同的语言里解析方式都是一样的,只不过实现的语法不同而已。基本的解析方式有两种,一种叫SAX,另一种叫DOM。SAX是基于事件流的解析,DOM是基于XML文档树结构的解析。假设我们XML的内容和结构如下: 

<?xml version="1.0" encoding="UTF-8"?> 
<employees>
<employee>
<name>ddviplinux</name>
<sex>m</sex>
<age>30</age>
</employee>
</employees>

本文使用JAVA语言来实现DOM与SAX的XML文档生成与解析。 
首先定义一个操作XML文档的接口XmlDocument 它定义了XML文档的建立与解析的接口。

package com.alisoft.facepay.framework.bean; 
/**
*
* @author hongliang.dinghl
* 定义XML文档建立与解析的接口
*/
public interface XmlDocument {
/**
* 建立XML文档
* @param fileName 文件全路径名称
*/
public void createXml(String fileName);
/**
* 解析XML文档
* @param fileName 文件全路径名称
*/
public void parserXml(String fileName);
}

1.DOM生成和解析XML文档

为 XML 文档的已解析版本定义了一组接口。解析器读入整个文档,然后构建一个驻留内存的树结构,然后代码就可以使用 DOM 接口来操作这个树结构。优点:整个文档树在内存中,便于操作;支持删除、修改、重新排列等多种功能;缺点:将整个文档调入内存(包括无用的节点),浪费时间和空间;使用场合:一旦解析了文档还需多次访问这些数据;硬件资源充足(内存、CPU)。 

package com.alisoft.facepay.framework.bean; 
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
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;
/**
*
* @author hongliang.dinghl
* DOM生成与解析XML文档
*/
public class DomDemo implements XmlDocument {
private Document document;
private String fileName;
public void init() {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
this.document = builder.newDocument();
} catch (ParserConfigurationException e) {
System.out.println(e.getMessage());
}
}
public void createXml(String fileName) {
Element root = this.document.createElement("employees");
this.document.appendChild(root);
Element employee = this.document.createElement("employee");
Element name = this.document.createElement("name");
name.appendChild(this.document.createTextNode("丁宏亮"));
employee.appendChild(name);
Element sex = this.document.createElement("sex");
sex.appendChild(this.document.createTextNode("m"));
employee.appendChild(sex);
Element age = this.document.createElement("age");
age.appendChild(this.document.createTextNode("30"));
employee.appendChild(age);
root.appendChild(employee);
TransformerFactory tf = TransformerFactory.newInstance();
try {
Transformer transformer = tf.newTransformer();
DOMSource source = new DOMSource(document);
transformer.setOutputProperty(OutputKeys.ENCODING, "gb2312");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
PrintWriter pw = new PrintWriter(new FileOutputStream(fileName));
StreamResult result = new StreamResult(pw);
transformer.transform(source, result);
System.out.println("生成XML文件成功!");
} catch (TransformerConfigurationException e) {
System.out.println(e.getMessage());
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
} catch (TransformerException e) {
System.out.println(e.getMessage());
}
}
public void parserXml(String fileName) {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(fileName);
NodeList employees = document.getChildNodes();
for (int i = 0; i < employees.getLength(); i++) {
Node employee = employees.item(i);
NodeList employeeInfo = employee.getChildNodes();
for (int j = 0; j < employeeInfo.getLength(); j++) {
Node node = employeeInfo.item(j);
NodeList employeeMeta = node.getChildNodes();
for (int k = 0; k < employeeMeta.getLength(); k++) {
System.out.println(employeeMeta.item(k).getNodeName()
+ ":" + employeeMeta.item(k).getTextContent());
}
}
}
System.out.println("解析完毕");
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
} catch (ParserConfigurationException e) {
System.out.println(e.getMessage());
} catch (SAXException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}

2.SAX生成和解析XML文档

为解决DOM的问题,出现了SAX。SAX ,事件驱动。当解析器发现元素开始、元素结束、文本、文档的开始或结束等时,发送事件,程序员编写响应这些事件的代码,保存数据。优点:不用事先调入整个文档,占用资源少;SAX解析器代码比DOM解析器代码小,适于Applet,下载。缺点:不是持久的;事件过后,若没保存数据,那么数据就丢了;无状态性;从事件中只能得到文本,但不知该文本属于哪个元素;使用场合:Applet;只需XML文档的少量内容,很少回头访问;机器内存少;

Java代码

package com.alisoft.facepay.framework.bean;   
import java.io.FileInputStream;  
import java.io.FileNotFoundException;  
import java.io.IOException;  
import java.io.InputStream;  

import javax.xml.parsers.ParserConfigurationException;  
import javax.xml.parsers.SAXParser;  
import javax.xml.parsers.SAXParserFactory;  

import org.xml.sax.Attributes;  
import org.xml.sax.SAXException;  
import org.xml.sax.helpers.DefaultHandler;  
/** 
*  
* @author hongliang.dinghl 
* SAX文档解析 
*/ 
public class SaxDemo implements XmlDocument {  

public void createXml(String fileName) {  
System.out.println("<<"+filename+">>");  
}  

public void parserXml(String fileName) {  
SAXParserFactory saxfac = SAXParserFactory.newInstance();  

try {  

SAXParser saxparser = saxfac.newSAXParser();  

InputStream is = new FileInputStream(fileName);  

saxparser.parse(is, new MySAXHandler());  

} catch (ParserConfigurationException e) {  

e.printStackTrace();  

} catch (SAXException e) {  

e.printStackTrace();  

} catch (FileNotFoundException e) {  

e.printStackTrace();  

} catch (IOException e) {  

e.printStackTrace();  

}  

}  

}  

class MySAXHandler extends DefaultHandler {  

boolean hasAttribute = false;  

Attributes attributes = null;  

public void startDocument() throws SAXException {  

System.out.println("文档开始打印了");  

}  

public void endDocument() throws SAXException {  

System.out.println("文档打印结束了");  

}  

public void startElement(String uri, String localName, String qName,  

Attributes attributes) throws SAXException {  

if (qName.equals("employees")) {  

return;  

}  

if (qName.equals("employee")) {  

System.out.println(qName);  

}  

if (attributes.getLength() > 0) {  

this.attributes = attributes;  

this.hasAttribute = true;  

}  

}  

public void endElement(String uri, String localName, String qName)  

throws SAXException {  

if (hasAttribute && (attributes != null)) {  

for (int i = 0; i < attributes.getLength(); i++) {  

System.out.println(attributes.getQName(0)  
+ attributes.getValue(0));  

}  

}  

}  

public void characters(char[] ch, int start, int length)  

throws SAXException {  

System.out.println(new String(ch, start, length));  

}  


package com.alisoft.facepay.framework.bean;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/**
*
* @author hongliang.dinghl
* SAX文档解析
*/
public class SaxDemo implements XmlDocument {
public void createXml(String fileName) {
System.out.println("<<"+filename+">>");
}
public void parserXml(String fileName) {
SAXParserFactory saxfac = SAXParserFactory.newInstance();
try {
SAXParser saxparser = saxfac.newSAXParser();
InputStream is = new FileInputStream(fileName);
saxparser.parse(is, new MySAXHandler());
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class MySAXHandler extends DefaultHandler {
boolean hasAttribute = false;
Attributes attributes = null;
public void startDocument() throws SAXException {
System.out.println("文档开始打印了");
}
public void endDocument() throws SAXException {
System.out.println("文档打印结束了");
}
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if (qName.equals("employees")) {
return;
}
if (qName.equals("employee")) {
System.out.println(qName);
}
if (attributes.getLength() > 0) {
this.attributes = attributes;
this.hasAttribute = true;
}
}
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (hasAttribute && (attributes != null)) {
for (int i = 0; i < attributes.getLength(); i++) {
System.out.println(attributes.getQName(0)
+ attributes.getValue(0));
}
}
}
public void characters(char[] ch, int start, int length)
throws SAXException {
System.out.println(new String(ch, start, length));
}
}

3.DOM4J生成和解析XML文档

DOM4J 是一个非常非常优秀的Java XML API,具有性能优异、功能强大和极端易用使用的特点,同时它也是一个开放源代码的软件。如今你可以看到越来越多的 Java 软件都在使用 DOM4J 来读写 XML,特别值得一提的是连 Sun 的 JAXM 也在用 DOM4J。

Java代码

package com.alisoft.facepay.framework.bean;   
import java.io.File;  
import java.io.FileWriter;  
import java.io.IOException;  
import java.io.Writer;  
import java.util.Iterator;  

import org.dom4j.Document;  
import org.dom4j.DocumentException;  
import org.dom4j.DocumentHelper;  
import org.dom4j.Element;  
import org.dom4j.io.SAXReader;  
import org.dom4j.io.XMLWriter;  
/** 
*  
* @author hongliang.dinghl 
* Dom4j 生成XML文档与解析XML文档 
*/ 
public class Dom4jDemo implements XmlDocument {  

public void createXml(String fileName) {  
Document document = DocumentHelper.createDocument();  
Element employees=document.addElement("employees");  
Element employee=employees.addElement("employee");  
Element name= employee.addElement("name");  
name.setText("ddvip");  
Element sex=employee.addElement("sex");  
sex.setText("m");  
Element age=employee.addElement("age");  
age.setText("29");  
try {  
Writer fileWriter=new FileWriter(fileName);  
XMLWriter xmlWriter=new XMLWriter(fileWriter);  
xmlWriter.write(document);  
xmlWriter.close();  
} catch (IOException e) {  

System.out.println(e.getMessage());  
}  


}  


public void parserXml(String fileName) {  
File inputXml=new File(fileName);  
SAXReader saxReader = new SAXReader();  
try {  
Document document = saxReader.read(inputXml);  
Element employees=document.getRootElement();  
for(Iterator i = employees.elementIterator(); i.hasNext();){  
Element employee = (Element) i.next();  
for(Iterator j = employee.elementIterator(); j.hasNext();){  
Element node=(Element) j.next();  
System.out.println(node.getName()+":"+node.getText());  
}  

}  
} catch (DocumentException e) {  
System.out.println(e.getMessage());  
}  
System.out.println("dom4j parserXml");  
}   
}  

4.JDOM生成和解析XML  

为减少DOM、SAX的编码量,出现了JDOM;优点:20-80原则,极大减少了代码量。使用场合:要实现的功能简单,如解析、创建等,但在底层,JDOM还是使用SAX(最常用)、DOM、Xanan文档。

   
<span style="background-color: rgb(255, 255, 255);">package com.alisoft.facepay.framework.bean;   <br style="clear: both; width: 0px; height: 0px;" /><br style="clear: both; width: 0px; height: 0px;" />import java.io.FileNotFoundException;   <br style="clear: both; width: 0px; height: 0px;" />import java.io.FileOutputStream;   <br style="clear: both; width: 0px; height: 0px;" />import java.io.IOException;   <br style="clear: both; width: 0px; height: 0px;" />import java.util.List;   <br style="clear: both; width: 0px; height: 0px;" /><br style="clear: both; width: 0px; height: 0px;" />import org.jdom.Document;   <br style="clear: both; width: 0px; height: 0px;" />import org.jdom.Element;   <br style="clear: both; width: 0px; height: 0px;" />import org.jdom.JDOMException;   <br style="clear: both; width: 0px; height: 0px;" />import org.jdom.input.SAXBuilder;   <br style="clear: both; width: 0px; height: 0px;" />import org.jdom.output.XMLOutputter;   <br style="clear: both; width: 0px; height: 0px;" />/**  <br style="clear: both; width: 0px; height: 0px;" />*   <br style="clear: both; width: 0px; height: 0px;" />* @author hongliang.dinghl  <br style="clear: both; width: 0px; height: 0px;" />* JDOM 生成与解析XML文档  <br style="clear: both; width: 0px; height: 0px;" />*   <br style="clear: both; width: 0px; height: 0px;" />*/  <br style="clear: both; width: 0px; height: 0px;" />public class JDomDemo implements XmlDocument {   <br style="clear: both; width: 0px; height: 0px;" /><br style="clear: both; width: 0px; height: 0px;" />public void createXml(String fileName) {   <br style="clear: both; width: 0px; height: 0px;" />Document document;   <br style="clear: both; width: 0px; height: 0px;" />Element  root;   <br style="clear: both; width: 0px; height: 0px;" />root=new Element("employees");   <br style="clear: both; width: 0px; height: 0px;" />document=new Document(root);   <br style="clear: both; width: 0px; height: 0px;" />Element employee=new Element("employee");   <br style="clear: both; width: 0px; height: 0px;" />root.addContent(employee);   <br style="clear: both; width: 0px; height: 0px;" />Element name=new Element("name");   <br style="clear: both; width: 0px; height: 0px;" />name.setText("ddvip");   <br style="clear: both; width: 0px; height: 0px;" />employee.addContent(name);   <br style="clear: both; width: 0px; height: 0px;" />Element sex=new Element("sex");   <br style="clear: both; width: 0px; height: 0px;" />sex.setText("m");   <br style="clear: both; width: 0px; height: 0px;" />employee.addContent(sex);   <br style="clear: both; width: 0px; height: 0px;" />Element age=new Element("age");   <br style="clear: both; width: 0px; height: 0px;" />age.setText("23");   <br style="clear: both; width: 0px; height: 0px;" />employee.addContent(age);   <br style="clear: both; width: 0px; height: 0px;" />XMLOutputter XMLOut = new XMLOutputter();   <br style="clear: both; width: 0px; height: 0px;" />try {   <br style="clear: both; width: 0px; height: 0px;" />XMLOut.output(document, new FileOutputStream(fileName));   <br style="clear: both; width: 0px; height: 0px;" />} catch (FileNotFoundException e) {   <br style="clear: both; width: 0px; height: 0px;" />e.printStackTrace();   <br style="clear: both; width: 0px; height: 0px;" />} catch (IOException e) {   <br style="clear: both; width: 0px; height: 0px;" />e.printStackTrace();   <br style="clear: both; width: 0px; height: 0px;" />}   <br style="clear: both; width: 0px; height: 0px;" /><br style="clear: both; width: 0px; height: 0px;" />}   <br style="clear: both; width: 0px; height: 0px;" /><br style="clear: both; width: 0px; height: 0px;" />public void parserXml(String fileName) {   <br style="clear: both; width: 0px; height: 0px;" />SAXBuilder builder=new SAXBuilder(false);    <br style="clear: both; width: 0px; height: 0px;" />try {   <br style="clear: both; width: 0px; height: 0px;" />Document document=builder.build(fileName);   <br style="clear: both; width: 0px; height: 0px;" />Element employees=document.getRootElement();    <br style="clear: both; width: 0px; height: 0px;" />List employeeList=employees.getChildren("employee");   <br style="clear: both; width: 0px; height: 0px;" />for(int i=0;i<employeelist.size();i++){ Element employee=(Element)employeeList.get(i);   <br style="clear: both; width: 0px; height: 0px;" />List employeeInfo=employee.getChildren();   <br style="clear: both; width: 0px; height: 0px;" />for(int j=0;j<employeeinfo.size();j++){ System.out.println(((Element)employeeInfo.get(j)).getName()+":"+((Element)employeeInfo.get(j)).getValue());   <br style="clear: both; width: 0px; height: 0px;" /><br style="clear: both; width: 0px; height: 0px;" />}   <br style="clear: both; width: 0px; height: 0px;" />}   <br style="clear: both; width: 0px; height: 0px;" />} catch (JDOMException e) {   <br style="clear: both; width: 0px; height: 0px;" /><br style="clear: both; width: 0px; height: 0px;" />e.printStackTrace();   <br style="clear: both; width: 0px; height: 0px;" />} catch (IOException e) {   <br style="clear: both; width: 0px; height: 0px;" /><br style="clear: both; width: 0px; height: 0px;" />e.printStackTrace();   <br style="clear: both; width: 0px; height: 0px;" />}    <br style="clear: both; width: 0px; height: 0px;" /><br style="clear: both; width: 0px; height: 0px;" />}   <br style="clear: both; width: 0px; height: 0px;" />}   </span>
0 0
原创粉丝点击