java xml jaxp sample

풀그림 2008. 8. 19. 19:05

JAXP (Java API for XML Processing)을 사용하는 간단한 예제들을 볼수 있음

== XML 데이터 == (XSD나 DTD는 안넣어놨음. )
<?xml version="1.0" encoding="utf-8"?>
<CHESSBOARDS>
 <CHESSBOARD>
  <WHITEPIECES>
   <KING><POSITION COLUMN="G" ROW="1"/></KING>
   <BISHOP><POSITION COLUMN="D" ROW="6"/></BISHOP>
     <ROOK><POSITION COLUMN="E" ROW="1"/></ROOK>
     <PAWN><POSITION COLUMN="A" ROW="4"/></PAWN>
     <PAWN><POSITION COLUMN="B" ROW="3"/></PAWN>
     <PAWN><POSITION COLUMN="C" ROW="2"/></PAWN>
     <PAWN><POSITION COLUMN="F" ROW="2"/></PAWN>
     <PAWN><POSITION COLUMN="G" ROW="2"/></PAWN>
     <PAWN><POSITION COLUMN="H" ROW="5"/></PAWN>
  </WHITEPIECES>
  <BLACKPIECES>
     <KING><POSITION COLUMN="B" ROW="6"/></KING>
     <QUEEN><POSITION COLUMN="A" ROW="7"/></QUEEN>
     <PAWN><POSITION COLUMN="A" ROW="5"/></PAWN>
     <PAWN><POSITION COLUMN="D" ROW="4"/></PAWN>
   </BLACKPIECES>
  </CHESSBOARD>
</CHESSBOARDS>

import org.xml.sax.*;
import javax.xml.parsers.*;
import java.io.*;
public class ChessboardSAXPrinter {
 private SAXParser parser;
 private static PrintStream out;
 
 @SuppressWarnings("deprecation")
 public class ChessboardHandler extends HandlerBase
 {
  private boolean whitePiece= false;
 
  @SuppressWarnings("deprecation")
  public void startElement(String name, AttributeList attrs)
  {
   if(name.equals("WHITEPIECES"))
   {
    whitePiece=true;
   }
   else if(name.equals("BLACKPIECES"))
   {
    whitePiece=false;
   }
   else if(name.equals("KING") || name.equals("QUEEN") || name.equals("BISHOP")|| name.equals("ROOK")
     || name.equals("ROOK") || name.equals("KNIGHT") || name.equals("PAWN"))
   {
    System.out.print((whitePiece ? "White" : "Black") + " " + name.toLowerCase() + ": ");   
   }
   else if(name.equals("POSITION"))
   {
    if(attrs!=null)
    {
     System.out.print(attrs.getValue("COLUMN"));
     System.out.println(attrs.getValue("ROW"));
    }  
   }
   return;   
  }
 }
 public ChessboardSAXPrinter(boolean validating) throws Exception
 {
  SAXParserFactory factory=SAXParserFactory.newInstance();
  factory.setValidating(validating);
  parser=factory.newSAXParser();
 
  return;
 }
 
 public void print(String fileName, PrintStream out) throws SAXException, IOException
 {
  ChessboardHandler dh=new ChessboardHandler();
 
  parser.parse(fileName, dh);
  return;
 }
 
 public static void main(String[] args)
 {
  boolean validating=true;
  int r,n;
  r=1;
  n=1;
  for(int k=0; k < r; k++)
  {
   try
   {
    ChessboardSAXPrinter saxPrinter=new ChessboardSAXPrinter(validating);
    long time = System.currentTimeMillis();
      for (int i = 0; i < n; i++) {
        // n: number of document processed per run
       //saxPrinter.print(args[0], out);
       saxPrinter.print(args[0], out);
      }
     
      // print out the average time (s)
      // to process a document during the current run
      System.err.print(((double) (System.currentTimeMillis()- time)) / 1000 / n + "\t");
   }
   catch(SAXException ex)
   {
    System.out.println(ex.getMessage());
   }
   catch(IOException iex)
   {
    System.out.println(iex.getMessage());
   }
   catch(Exception uex)
   {
    System.out.println(uex.getMessage());
   }
  }
 }
}


----- DOM으로 처리한 경우 ----------

import org.w3c.dom.*;
import org.xml.sax.*;
import javax.xml.parsers.*;
import java.io.*;

public class ChessboardDOMPrinter {
 private DocumentBuilder builder;
 boolean validate;
 public ChessboardDOMPrinter(boolean _validate)
 {
  validate=_validate;
 }
 
 public void print(String fileName, PrintStream out) throws SAXException, IOException, ParserConfigurationException
 { 
  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
     dbf.setValidating(validate);
     dbf.setNamespaceAware(true);
     dbf.setIgnoringElementContentWhitespace(true);
 
     DocumentBuilder builder = dbf.newDocumentBuilder();
    
  Document document = builder.parse(fileName);
  NodeList nodes_i = document.getDocumentElement().getChildNodes();
 
  for(int i=0; i< nodes_i.getLength(); i++)
  {
   Node node_i=nodes_i.item(i);
   if(node_i.getNodeType()==Node.ELEMENT_NODE && ((Element)node_i).getTagName().equals("CHESSBOARD"))
   {
    Element chessboard=(Element) node_i;
    NodeList nodes_j=chessboard.getChildNodes();
   
    for(int j=0; j< nodes_j.getLength(); j++)
    {
     Node node_j = nodes_j.item(j);
     if (node_j.getNodeType() == Node.ELEMENT_NODE) {
      Element pieces = (Element) node_j;
      NodeList nodes_k = pieces.getChildNodes();
      for (int k = 0; k < nodes_k.getLength(); k++) {
       Node node_k = nodes_k.item(k);
       if (node_k.getNodeType() == Node.ELEMENT_NODE) {
        Element piece = (Element) node_k;
        Element position = (Element) piece.getChildNodes().item(0);
        System.out.println((pieces.getTagName()
                            .equals("WHITEPIECES")
                          ? "White " : "Black ")
                         + piece.getTagName().toLowerCase()
                         + ": "
                         + position.getAttribute("COLUMN")
                         + position.getAttribute("ROW"));       
       }
      }
     }
    }
   }
  }
 
  return;
 }
 
 public void print2(String fileName, PrintStream out) throws SAXException, IOException, ParserConfigurationException {
  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
     dbf.setValidating(validate);
     dbf.setNamespaceAware(true);
     dbf.setIgnoringElementContentWhitespace(true);
 
     DocumentBuilder builder = dbf.newDocumentBuilder();
    
  Document document = builder.parse(fileName);
  NodeList positions = document.getElementsByTagName("POSITION");
  for (int i = 0; i < positions.getLength(); i++) {
   Element position = (Element) positions.item(i);
   Element piece = (Element) position.getParentNode();
   Element pieces = (Element) piece.getParentNode();
   out.println( (pieces.getTagName().equals("WHITEPIECES") ? "White " : "Black ")
      + piece.getTagName().toLowerCase() + ": "
      + position.getAttribute("COLUMN")
      + position.getAttribute("ROW"));
  }
  return;
 }
 
 public static void main(String[] args)
 {
  PrintStream out=System.out;
  try{
   ChessboardDOMPrinter domPrinter=new ChessboardDOMPrinter(false);
   domPrinter.print2("E:\\DEV_STUDY\\WebCrawler\\WebCrawler\\chessboard.xml", out);
  }
  catch(SAXException sex){
   System.err.println(sex.getMessage());
  }
  catch(IOException iex){
   System.err.println(iex.getMessage());
  }
  catch(ParserConfigurationException pcex)
  {
   System.err.println(pcex.getMessage());
  }
  catch(Exception ex){
   System.err.println(ex.getMessage());
  }
 
 }
}

====== XPATH 관련 ==========
// 추가
import javax.xml.transform.*;
import javax.xml.xpath.*;
.............

public void print3(String fileName, PrintStream out) throws TransformerException,SAXException, IOException, ParserConfigurationException
 {
  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
     dbf.setValidating(validate);
     dbf.setNamespaceAware(true);
     dbf.setIgnoringElementContentWhitespace(true);
    
     DocumentBuilder builder = dbf.newDocumentBuilder();
     Document document = builder.parse(fileName);

     NodeList allPieces=XPathAPI.selectNodeList(document.getDocumentElement(), "//*[self::WHITEPIECES or self::BLACKPIECES]/*");
    
     for(int i=0; i < allPieces.getLength(); i++)
     {
      Element piece=(Element)allPieces.item(i);
      Element pieces=(Element)piece.getParentNode();
      Element position = (Element) piece.getChildNodes().item(0);
      System.out.println((pieces.getTagName().equals("WHITEPIECES")? "White ":"Black ")+piece.getTagName().toLowerCase()+": " + position.getAttribute("COLUMN")+position.getAttribute("ROW"));    
     }
    
     return;
 }

출처 : http://ausgang.egloos.com/1998938
참조 : http://java.sun.com/developer/technicalArticles/xml/JavaTechandXML/#code14
 http://www.java2s.com/
 http://www.java2s.com/Code/JavaAPI/javax.xml.parsers/DocumentBuilderFactorynewDocumentBuilder.htm

Posted by 파이팅야
,