xml解析整理

import java.io.File;
import java.io.StringReader;
import java.util.Iterator;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class ResolvingXML{
        public static void main(String[] args){
                ResolvingXML xml = new ResolvingXML();
                File f = new File("F:\\WorkSpace\\TEST\\test.xml");
                String str = "<?xml version=\"1.0\" encoding=\"GB2312\"?>" +
                                "<RESULT>" +
                                "        <VALUE>" +
                                "                <NO>A1234</NO>" +
                                "                <ADDR>陕西省凤翔县虢王镇</ADDR>" +
                                "        </VALUE>" +
                                "        <VALUE>" +
                                "                <NO>B1234</NO>" +
                                "                <ADDR>陕西省凤翔县虢王镇</ADDR>" +
                                "        </VALUE>" +
                                "        <VAL name=\"hehe\">" +
                                "                <WW>陕西省</WW>" +
                                "        </VAL>" +
                                "        <VAL name=\"heihei\">" +
                                "                <WW>ShanXiSheng</WW>" +
                                "        </VAL>" +
                                "        <HELLO>" +
                                "                <SX>SB</SX>" +
                                "        </HELLO>" +
                                "</RESULT>";
                //xml.ParseWithDOMByString(str);
                xml.ParseWithJDOMByString(str);
                //xml.ParseWithDOM4JByString(str);
                System.out.println("==============================");
                //xml.ParseWithDOMByFile(f);
                //xml.ParseWithDOM4JByFile(f);
                //xml.ParseWithJDOMByFile(f);
                
        }
        public void ParseWithDOMByString(String strXml){
                long lasting = System.currentTimeMillis();
               try {
                   javax.xml.parsers.DocumentBuilderFactory factory = javax.xml.parsers.DocumentBuilderFactory.newInstance();
                   javax.xml.parsers.DocumentBuilder builder = factory.newDocumentBuilder();
                   StringReader sr = new StringReader(strXml);
                   InputSource is = new InputSource(sr);
                   org.w3c.dom.Document doc = builder.parse(is);
                   org.w3c.dom.NodeList node = doc.getElementsByTagName("VALUE");
                   for (int i = 0; i < node.getLength(); i++ ) {
                      System.out.print("车牌号码:" + doc.getElementsByTagName("NO").item(i).getFirstChild().getNodeValue());
                      System.out.println(" 车主地址:"+ doc.getElementsByTagName("ADDR").item(i).getFirstChild().getNodeValue());
                   }
               } catch (Exception e) {
                   e.printStackTrace();
               }
               System.out.println("运行时间:" + (System.currentTimeMillis() - lasting)+" 毫秒");
        }
        public void ParseWithDOMByFile(File f){
                long lasting = System.currentTimeMillis();
               try {
                   javax.xml.parsers.DocumentBuilderFactory factory = javax.xml.parsers.DocumentBuilderFactory.newInstance();
                   javax.xml.parsers.DocumentBuilder builder = factory.newDocumentBuilder();
                   org.w3c.dom.Document doc = builder.parse(f);
               
                   org.w3c.dom.NodeList node = doc.getElementsByTagName("VALUE");
                   for (int i = 0; i < node.getLength(); i++ ) {
                      System.out.print("车牌号码:" + doc.getElementsByTagName("NO").item(i).getFirstChild().getNodeValue());
                      System.out.println(" 车主地址:"+ doc.getElementsByTagName("ADDR").item(i).getFirstChild().getNodeValue());
                   }
               } catch (Exception e) {
                   e.printStackTrace();
               }
               System.out.println("运行时间:" + (System.currentTimeMillis() - lasting)+" 毫秒");
        }
        public void ParseWithJDOMByString(String strXml){
                 long lasting = System.currentTimeMillis();
               try {
                       org.jdom.input.SAXBuilder builder = new org.jdom.input.SAXBuilder();
                   org.jdom.Document doc = (org.jdom.Document) builder.build(new InputSource(new StringReader(strXml)));
                   org.jdom.Element foo = doc.getRootElement();
                   //org.jdom.xpath.XPath xp = org.jdom.xpath.XPath.newInstance("./RESULT/VAL[@name=\"heihei\"]");
                   //org.jdom.Element ww = (org.jdom.Element)xp.selectSingleNode(foo);
                   org.jdom.Element ww = (org.jdom.Element)org.jdom.xpath.XPath.selectSingleNode(foo,"/RESULT/VAL[@name=\"heihei\"]");
                   //List list = org.jdom.xpath.XPath.selectNodes(foo,"/RESULT");
                   //org.jdom.Element ww = (org.jdom.Element)org.jdom.xpath.XPath.selectSingleNode(foo, "/RESULT/VAL/HELLO");
                   //String shengf=((org.jdom.Text)org.jdom.xpath.XPath.selectSingleNode(foo,"//VAL[@name='heihei']/WW/text()")).getTextNormalize();
                   
                   System.out.println("xpth:"+ww.getChildText("WW").trim());
                   List allChildren = foo.getChildren("VALUE");
                   for (int i = 0; i < allChildren.size(); i++ ) {
                      System.out.print("车牌号码:" + ((org.jdom.Element) allChildren.get(i)).getChild("NO").getText());
                      System.out.println(" 车主地址:" + ((org.jdom.Element) allChildren.get(i)).getChild("ADDR").getText());
                   }
               } catch (Exception e) {
                   e.printStackTrace();
               }
               System.out.println("运行时间:" + (System.currentTimeMillis() - lasting)+ " 毫秒");
        }
        public void ParseWithJDOMByFile(File f){
           long lasting = System.currentTimeMillis();
       try {
               org.jdom.input.SAXBuilder builder = new org.jdom.input.SAXBuilder();
           org.jdom.Document doc = (org.jdom.Document) builder.build(f);
           org.jdom.output.Format format = org.jdom.output.Format.getPrettyFormat();
                    format.setEncoding("GBK");
                    org.jdom.output.XMLOutputter docWriter = new org.jdom.output.XMLOutputter(format);
                        try {
                                docWriter.output(doc, System.out);
                        } catch (Exception e) {
                        }
           org.jdom.Element foo = doc.getRootElement();
           List allChildren = foo.getChildren();
           for (int i = 0; i < allChildren.size(); i++ ) {
              System.out.print("车牌号码:" + ((org.jdom.Element) allChildren.get(i)).getChild("NO").getText());
              System.out.println(" 车主地址:" + ((org.jdom.Element) allChildren.get(i)).getChild("ADDR").getText());
           }
       } catch (Exception e) {
           e.printStackTrace();
       }
       System.out.println("运行时间:" + (System.currentTimeMillis() - lasting)+ " 毫秒");
        }
        public void ParseWithDOM4JByString(String strXml){
                 long lasting = System.currentTimeMillis();
               try {
                   org.dom4j.Document doc = (org.dom4j.Document)org.dom4j.DocumentHelper.parseText(strXml);
                   org.dom4j.Element root = doc.getRootElement();
                   org.dom4j.io.OutputFormat of = new org.dom4j.io.OutputFormat(); 
                   of.setEncoding("GBK");
               of.setIndent(true);   
               of.setNewlines(true);   
               org.dom4j.io.XMLWriter xmlWriter=new org.dom4j.io.XMLWriter(System.out,of); 
               xmlWriter.write(doc);      
               //xmlWriter.close();
                   org.dom4j.Element foo;
                   for (Iterator i = root.elementIterator("VALUE"); i.hasNext();) {
                      foo = (org.dom4j.Element) i.next();
                      System.out.print("车牌号码:" + foo.elementText("NO"));
                      System.out.println(" 车主地址:" + foo.elementText("ADDR"));
                   }
               } catch (Exception e) {
                   e.printStackTrace();
               }
               System.out.println("运行时间:" + (System.currentTimeMillis() - lasting) + " 毫秒");
        }
        public void ParseWithDOM4JByFile(File f){
                 long lasting = System.currentTimeMillis();
               try {
                   org.dom4j.io.SAXReader reader = new org.dom4j.io.SAXReader();
                   org.dom4j.Document doc = reader.read(f);
                   org.dom4j.io.OutputFormat of = new org.dom4j.io.OutputFormat(); 
                   of.setEncoding("GBK");
               of.setIndent(true);   
               //of.setNewlines(true);   
               org.dom4j.io.XMLWriter xmlWriter=new org.dom4j.io.XMLWriter(System.out,of); 
               xmlWriter.write(doc);      
               //xmlWriter.close();                         
                   org.dom4j.Element root = doc.getRootElement();
                   org.dom4j.Element foo;
                   for (Iterator i = root.elementIterator("VALUE"); i.hasNext();) {
                      foo = (org.dom4j.Element) i.next();
                      System.out.print("车牌号码:" + foo.elementText("NO"));
                      System.out.println(" 车主地址:" + foo.elementText("ADDR"));
                   }
               } catch (Exception e) {
                   e.printStackTrace();
               }
               System.out.println("运行时间:" + (System.currentTimeMillis() - lasting) + " 毫秒");
        }
}
        
class ParseWithSAX extends DefaultHandler {
         
    java.util.Stack tags = new java.util.Stack();

    public ParseWithSAX() {
           super();
    }
    public static void main(String args[]) {
           long lasting = System.currentTimeMillis();
           try {
                  javax.xml.parsers.SAXParserFactory sf = javax.xml.parsers.SAXParserFactory.newInstance();
              javax.xml.parsers.SAXParser sp = sf.newSAXParser();
              ParseWithSAX reader = new ParseWithSAX();
              sp.parse(new InputSource("F:\\WorkSpace\\TEST\\test.xml"), reader);
           } catch (Exception e) {
                  e.printStackTrace();
           }
           System.out.println("运行时间:" + (System.currentTimeMillis() - lasting) + " 毫秒");
    }

    public void characters(char ch[], int start, int length)throws SAXException {
           String tag = (String) tags.peek();
           if (tag.equals("NO")) {
                  System.out.print("车牌号码:" + new String(ch, start, length));
           }
           if (tag.equals("ADDR")) {
                  System.out.println(" 地址:" + new String(ch, start, length));
           }
    }

    public void startElement(String uri, String localName, String qName,Attributes attrs) {
           tags.push(qName);
    }
}
 

你可能感兴趣的:(xml,XP,F#)