程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> java解析xml經常使用的幾種方法總結

java解析xml經常使用的幾種方法總結

編輯:關於JAVA

java解析xml經常使用的幾種方法總結。本站提示廣大學習愛好者:(java解析xml經常使用的幾種方法總結)文章只能為提供參考,不一定能成為您想要的結果。以下是java解析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());;

 }

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved