Dom,SAX,JAXP,DOM4J读写XML文件

本文来自于163.com/blog/static/9732381120118303351185/

使用DOM读取XML文件:

package test;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class TestDOM {

 public static void main(String args[]) {
  long start =System.currentTimeMillis();
  try {
   //取得DOM工厂
   DocumentBuilderFactoryfactory = DocumentBuilderFactory
     .newInstance();

   //取得DOM解析器
   DocumentBuilderbuilder = factory.newDocumentBuilder();

   //解析文件
   File file =new File("src/books.xml");
   Document doc= builder.parse(file);

   //取得根节点
   Element root= doc.getDocumentElement();

   //取得子节点列表
   NodeListbooks = root.getChildNodes();
   for (int i =0; i < books.getLength(); i++) {
    //取得某一个子节点
    Nodebook = books.item(i);

    if(book.getNodeType() == Node.ELEMENT_NODE) {
     //取得属性值
     Stringisbn = book.getAttributes().getNamedItem("isbn")
       .getNodeValue();
     System.out.print(isbn);

     //轮循子节点
     for(Node node = book.getFirstChild(); node != null; node = node
       .getNextSibling()){
      if(node.getNodeType() == Node.ELEMENT_NODE) {
       if(node.getNodeName().equals("name")) {
        Stringname = node.getFirstChild()
          .getNodeValue();
        System.out.print("\t"+ name);
       }
       if(node.getNodeName().equals("price")) {
        Stringprice = node.getFirstChild()
          .getNodeValue();
        System.out.print("\t"+ price);
       }
       if(node.getNodeName().equals("author")) {
        Stringauthor = node.getFirstChild()
          .getNodeValue();
        System.out.print("\t"+ author);
       }
       if(node.getNodeName().equals("year")) {
        Stringyear = node.getFirstChild()
          .getNodeValue();
        System.out.println("\t"+ year);
       }
      }
     }
    }
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
  long end =System.currentTimeMillis();
  System.out.println(end -start);
 }
}

 

使用SAX读取XML文件:

package test;

import java.io.File;
import java.util.Stack;

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;

public class TestSAX extends DefaultHandler {
 Stack<String> tags= new Stack<String>();

 public TestSAX() {
  super();
 }

 public void characters(char ch[], int start,int length)
   throwsSAXException {
  String tag = (String)tags.peek();
  if (tag.equals("name")) {
   System.out.print("\t"+ new String(ch, start, length));
  }
  if (tag.equals("price")){
   System.out.print("\t"+ new String(ch, start, length));
  }
  if (tag.equals("author")){
   System.out.print("\t"+ new String(ch, start, length));
  }
  if (tag.equals("year")) {
   System.out.println("\t"+ new String(ch, start, length));
  }
 }

 public void startElement(String uri, StringlocalName, String qName,
   Attributesattrs) {
  if (qName.equals("book")){
   System.out.print(attrs.getValue("isbn"));
  }
  tags.push(qName);
 }

 public static void main(String args[]) {
  long start =System.currentTimeMillis();
  try {
   //取得SAX工厂
   SAXParserFactoryfactory = SAXParserFactory.newInstance();

   //取得SAX解析器
   SAXParserparser = factory.newSAXParser();

   //解析文件
   File file =new File("src/books.xml");
   TestSAX sax =new TestSAX();
   parser.parse(file,sax);

  } catch (Exception e){
   e.printStackTrace();
  }
  long end =System.currentTimeMillis();
  System.out.println(end -start);
 }
}

 

使用JDOM读写XML:

package test;

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

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

public class TestJDOM {

 public static void main(String args[]) {
  long start =System.currentTimeMillis();
  try {
   //取得解析器
   SAXBuilderbuilder = new SAXBuilder();

   //解析文件
   File file =new File("src/books.xml");
   Document doc= builder.build(file);

   //取得根节点
   Element root= doc.getRootElement();

   //取得子节点列表
   List<?>books = root.getChildren();
   for (int i =0; i < books.size(); i++) {
    //取得某一个子节点
    Elementbook = (Element)books.get(i);

    //取得属性值
    Stringisbn = book.getAttributeValue("isbn");
    System.out.print(isbn);

    Stringname = book.getChild("name").getText();
    Stringprice = book.getChild("price").getText();
    Stringauthor = book.getChild("author").getText();
    Stringyear = book.getChild("year").getText();
    System.out.print("\t"+ name);
    System.out.print("\t"+ price);
    System.out.print("\t"+ author);
    System.out.println("\t"+ year);
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
  long end =System.currentTimeMillis();
  System.out.println(end -start);
 }
}
使用DOM4J读写XML文件

package test;

import java.io.File;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class TestDOM4J {

 public static void main(String args[]) {
  long start =System.currentTimeMillis();
  try {
   //取得SAX解析器
   SAXReaderreader = new SAXReader();

   //解析文件
   File file =new File("src/books.xml");
   Document doc= reader.read(file);

   //取得根节点
   Element root= doc.getRootElement();

   //取得子节点列表
   for (int i =0; i < root.nodeCount(); i++) {
    //取得某一个子节点
    Elementbook = (Element) root.node(i);

    //取得属性值
    Stringisbn = book.attributeValue("isbn");
    System.out.print(isbn);

        Stringname = book.node(0).getText();
        Stringprice = book.node(1).getText();
        Stringauthor = book.node(2).getText();
        Stringyear = book.node(3).getText();
        System.out.print("\t"+ name);
        System.out.print("\t"+ price);
        System.out.print("\t"+ author);
        System.out.println("\t"+ year);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    long end =System.currentTimeMillis();
    System.out.println(end -start);
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值