java 해석 xml에서 자주 사용하는 몇 가지 방식 요약

5764 단어
여러 가지 방법을 다 써 보았다.지금 총괄해 봅시다.자주 기억이 안 나니 자료를 찾아야 한다.지금 총괄해 봅시다.
xml 파일은 다음과 같습니다.
 
  



  Everyday Italian
  Giada De Laurentiis
  2005
  30.00
 
   
    ccccc
   

 


  Harry Potter
  J K. Rowling
  2005
  29.99


  XQuery Kick Start
  James McGovern
  Per Bothner
  Kurt Cagle
  James Linn
  Vaidyanathan Nagarajan
  2003
  49.99


  Learning XML
  Erik T. Ray
  2003
  39.95


 
  
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());;

 }

}

좋은 웹페이지 즐겨찾기