欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java解析xml常用的几种方式总结

程序员文章站 2024-02-16 09:43:22
各种方法都用过。现在总结一下。 经常记不住,要找资料。现在总结一下。 xml 文件如下: 复制代码 代码如下:

各种方法都用过。现在总结一下。 经常记不住,要找资料。现在总结一下。

xml 文件如下:

复制代码 代码如下:

<?xml version="1.0" encoding="iso-8859-1"?>
<bookstore>
<book category="cooking">
  <title lang="en">everyday italian</title>
  <author>giada de laurentiis</author>
  <year>2005</year>
  <price>30.00</price>
  <aa>
   <bb>
    <cc>ccccc</cc>
   </bb>
  </aa>
</book>

<book category="children">
  <title lang="en">harry potter</title>
  <author>j k. rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>

<book category="web">
  <title lang="en">xquery kick start</title>
  <author>james mcgovern</author>
  <author>per bothner</author>
  <author>kurt cagle</author>
  <author>james linn</author>
  <author>vaidyanathan nagarajan</author>
  <year>2003</year>
  <price>49.99</price>
</book>

<book category="web">
  <title lang="en">learning xml</title>
  <author>erik t. ray</author>
  <year>2003</year>
  <price>39.95</price>
</book>

</bookstore>

复制代码 代码如下:

package sort;
import javax.xml.parsers.documentbuilder;
import javax.xml.parsers.documentbuilderfactory;
import org.w3c.dom.document;
public class d2 {
 /**
  * 直接使用dom解析
  * @param args
  * @throws exception
  */
 public static void main(string[] args) throws exception{

  documentbuilder sb =  documentbuilderfactory.newinstance().newdocumentbuilder();

  document root = sb.parse(d2.class.getclassloader().getresourceasstream("newfile.xml"));

  system.out.println(root.getchildnodes().item(0).getnodename());

 }
}

复制代码 代码如下:

package sort;

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;

import com.sun.org.apache.xerces.internal.jaxp.saxparserfactoryimpl;

public class d {

 /**
  * 使用sax解析
  * @param args
  * @throws exception
  */
 public static void main(string[] args) throws exception {

  saxparserfactory factory = saxparserfactoryimpl.newinstance();
  saxparser parser = factory.newsaxparser() ;
  parser.parse(d.class.getclassloader().getresourceasstream("newfile.xml"),
    new defaulthandler(){

     @override
     public void characters(char[] ch, int start, int length)
       throws saxexception {
      system.out.println("characters");
     }

     @override
     public void enddocument() throws saxexception {
      // todo auto-generated method stub
      system.out.println("enddocument");
     }

     @override
     public void endelement(string uri, string localname,
       string qname) throws saxexception {
      // todo auto-generated method stub
      system.out.println("endelement");
     }

     @override
     public void startdocument() throws saxexception {
      // todo auto-generated method stub
      system.out.println("startdocument");
     }

     @override
     public void startelement(string uri, string localname,
       string qname, attributes attributes)
       throws saxexception {
      // todo auto-generated method stub
      system.out.println("startelement");
     }

  }) ;

  
 }

}

复制代码 代码如下:

package sort;

import javax.xml.stream.xmlinputfactory;
import javax.xml.stream.xmlstreamreader;

public class d3 {

 /**
  * 使用xmlstream解析
  * @param args
  * @throws exception
  */
 public static void main(string[] args) throws exception {

  xmlinputfactory xmlfactor = xmlinputfactory.newfactory();

  xmlstreamreader reader =
   xmlfactor.createxmlstreamreader(d3.class.getclassloader().getresourceasstream("newfile.xml"));
  while(reader.hasnext()){
   int point = reader.next() ;
   switch(point){
   case xmlstreamreader.start_element :
    system.out.println("start_element");
   case xmlstreamreader.end_element :
    // do something...
   }

  }

 }

}

复制代码 代码如下:

package sort;

import org.dom4j.document;
import org.dom4j.io.saxreader;

/**
 * 使用dom4j xpath解析xml (需要加入依赖jar文件)
 * @author zhoufeng
 *
 */
public class d4 {

 public static void main(string[] args) throws exception{

  saxreader reader = new saxreader() ;

  document root = reader.read(d4.class.getclassloader().getresourceasstream("newfile.xml"));

  /* 选择所有的cc节点 */
  system.out.println(root.selectnodes("//cc").size());;

  /*选择所有的book节点,并且有子节点author的*/
  system.out.println((root.selectnodes("//book[author]").size()));;

  /* 选择所有book节点,并且有属性category的   */
  system.out.println((root.selectnodes("//book[@category]").size()));;

  /* 选择所有book节点,并且有子节点author值为james mcgovern ,并且还有category属性节点值为web   下面的price节点*/
  system.out.println(root.selectnodes("//book[author='james mcgovern'][@category='web']/price").size());;

 }

}