A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

3. 四种xml操作方式的基本使用方法
    xml文件:
    [html]
    <?xml version="1.0" encoding="gbk"?>
    <list>
    <node>
    <name>程序猿狒狒的专栏</name>
    <space>http://blog.csdn.net/aoxiangzhiguanjun</space>
    </node>
    <node>
    <name>翱翔之冠军</name>
    <space>http://flydream5201314.qzone.qq.com</space>
    </node>
    <node>
    <name>FlyDream2014</name>
    <space>http://weibo.com/lzffly </space>
    </node>
    </list>
    <?xml version="1.0" encoding="gbk"?>
    <list>
    <node>
    <name>程序猿狒狒的专栏</name>
    <space>http://blog.csdn.net/aoxiangzhiguanjun</space>
    </node>
    <node>
    <name>翱翔之冠军</name>
    <space>http://flydream5201314.qzone.qq.com</space>
    </node>
    <node>
    <name>FlyDream2014</name>
    <space>http://weibo.com/lzffly </space>
    </node>
    </list>

    程序代码: 需要先导入jdom.jar和dom4j.jar包
    [java]
    package senior;
    import java.io.File;
    import java.io.IOException;
    import java.util.List;
    import java.util.Stack;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.parsers.SAXParser;
    import javax.xml.parsers.SAXParserFactory;
    import org.jdom.Element;
    import org.jdom.JDOMException;
    import org.jdom.input.SAXBuilder;
    import org.dom4j.DocumentException;
    import org.dom4j.io.SAXReader;
    import org.w3c.dom.Document;
    import org.w3c.dom.NodeList;
    import org.xml.sax.Attributes;
    import org.xml.sax.InputSource;
    import org.xml.sax.SAXException;
    import org.xml.sax.helpers.DefaultHandler;
    public class XMLTest extends DefaultHandler {
    /**
    * @param args
    */
    Stack<String> tags = new Stack<String>();
    public XMLTest() {
    super();
    }
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    XMLTest myXML = new XMLTest();
   .out.println("=====================DOM4J=======================");
    myXML.DOM4J();
    System.out.println("=================================================");
    }
    File f = new File("test.xml");
    public void DOM() {
    long start = System.currentTimeMillis();
    try {
       } catch (ParserConfigurationException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (SAXException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    long end = System.currentTimeMillis();
    System.out.println("dom runtime" + (end - start) + "MS");
    }
    public void SAX() {
    long start = System.currentTimeMillis();
    try {
    SAXParserFactory sf = SAXParserFactory.newInstance();
       SAXBuilder builder = new SAXBuilder();
    org.jdom.Document doc = builder.build(f);
    Element foo = doc.getRootElement();
    List<?> allChaildren = foo.getChildren();
    for (int i = 0; i < allChaildren.size(); i++) {
    System.out.println("Name|"
    + ((Element) allChaildren.get(i))。getChild("name")
    .getText());
    System.out.println("Space"
    + ((Element) allChaildren.get(i))。getChild("space")
    .getText());
    }
    } catch (JDOMException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
  System.out.println("Space|"
    + ((org.dom4j.Element) foo)。elementText("space"));
    }
    } catch (DocumentException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    long end = System.currentTimeMillis();
    System.out.println("dom4j runtime" + (end - start) + "MS");
    }
    }
    package senior;
    import java.io.File;
    import java.io.IOException;
    import java.util.List;
    import java.util.Stack;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.parsers.SAXParser;
    import javax.xml.parsers.SAXParserFactory;
    import org.jdom.Element;
    import org.jdom.JDOMException;
    import org.jdom.input.SAXBuilder;
    import org.dom4j.DocumentException;
    import org.dom4j.io.SAXReader;
    import org.w3c.dom.Document;
    import org.w3c.dom.NodeList;
    import org.xml.sax.Attributes;
    import org.xml.sax.InputSource;
    import org.xml.sax.SAXException;
    import org.xml.sax.helpers.DefaultHandler;
    public class XMLTest extends DefaultHandler {
    /**
    * @param args
    */
    Stack<String> tags = new Stack<String>();
    public XMLTest() {
    super();
    }
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    XMLTest myXML = new XMLTest();
    System.out.println("=====================DOM=========================");
    myXML.DOM();
    System.out.println("=====================SAX=========================");
    myXML.SAX();
    System.out.println("=====================JDOM========================");
    myXML.JDOM();
    System.out.println("=====================DOM4J=======================");
    myXML.DOM4J();
    System.out.println("=================================================");
    }
    File f = new File("test.xml");
    public void DOM() {
    long start = System.currentTimeMillis();
    try {
    DocumentBuilderFactory factory = DocumentBuilderFactory
    .newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(f);
    NodeList nl = doc.getElementsByTagName("node");
    for (int i = 0; i < nl.getLength(); i++) {
    System.out.println("Name|"
    + doc.getElementsByTagName("name")。item(i)
    .getFirstChild()。getNodeValue());
    System.out.println("Space|"
    + doc.getElementsByTagName("space")。item(i)
    .getFirstChild()。getNodeValue());
    }
    } catch (ParserConfigurationException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (SAXException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    long end = System.currentTimeMillis();
    System.out.println("dom runtime" + (end - start) + "MS");
    }
    public void SAX() {
    long start = System.currentTimeMillis();
    try {
    SAXParserFactory sf = SAXParserFactory.newInstance();
    SAXParser sp = sf.newSAXParser();
    XMLTest reader = new XMLTest();
    sp.parse(f, reader);
    } catch (Exception e) {
    e.printStackTrace();
    }
    System.out.println("sax runtime"
    + (System.currentTimeMillis() - start) + " MS");
    }
    public void startElement(String uri, String localName, String qName,
    Attributes attrs) {
    tags.push(qName);
    }
    public void characters(char ch[], int start, int length)
    throws SAXException {
    String tag = (String) tags.peek();
    if (tag.equals("name")) {
    System.out.println("Name|" + new String(ch, start, length));
    }
    else if (tag.equals("space")) {
    System.out.println("Space|" + new String(ch, start, length));
    }
    }
    public void JDOM() {
    long start = System.currentTimeMillis();
    try {
    SAXBuilder builder = new SAXBuilder();
    org.jdom.Document doc = builder.build(f);
    Element foo = doc.getRootElement();
    List<?> allChaildren = foo.getChildren();
    for (int i = 0; i < allChaildren.size(); i++) {
    System.out.println("Name|"
    + ((Element) allChaildren.get(i))。getChild("name")
    .getText());
    System.out.println("Space"
    + ((Element) allChaildren.get(i))。getChild("space")
    .getText());
    }
    } catch (JDOMException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    long end = System.currentTimeMillis();
    System.out.println("jdom runtime" + (end - start) + "MS");
    }
    public void DOM4J() {
    long start = System.currentTimeMillis();
    try {
    SAXReader reader = new SAXReader();
    org.dom4j.Document doc = reader.read(f);
    org.dom4j.Element root = doc.getRootElement();
    org.dom4j.Element foo;
    for (@SuppressWarnings("rawtypes")
    java.util.Iterator i = root.elementIterator("node"); i.hasNext();) {
    foo = (org.dom4j.Element) i.next();
    System.out.println("Name|"
    + ((org.dom4j.Element) foo)。elementText("name"));
    System.out.println("Space|"
    + ((org.dom4j.Element) foo)。elementText("space"));
    }
    } catch (DocumentException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    long end = System.currentTimeMillis();
    System.out.println("dom4j runtime" + (end - start) + "MS");
    }
    }

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马