DOM ,SAX practise

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

public class DOMTest {
  public static void main(String[] args) {
    try {
    
      DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
    
      DocumentBuilder builder = dbfactory.newDocumentBuilder();
      
      Document doc = builder.parse(new File("operation.xml"));
      
      Element root = doc.getDocumentElement();
      System.out.println("Root Ele" + root.getTagName());
       
      NodeList list = root.getElementsByTagName("operation");
      
      for (int i=0; i < list.getLength() ; i++) {
        
        Element element = (Element)list.item(i);
     // operationName
        String operationName = element.getAttribute("name");
        
        System.out.println("Operation : " +operationName);
      
        NodeList inputParamList = element.getElementsByTagName("input");
        NodeList outputParamList = element.getElementsByTagName("output");
       
        Element inputParamEle = (Element)inputParamList.item(0);
        Element outputParamEle = (Element)outputParamList.item(0);
    
       NodeList inparamList= inputParamEle.getElementsByTagName("param");
       NodeList outparamList1 = outputParamEle.getElementsByTagName("param");
       NodeList outparamList2 = outputParamEle.getElementsByTagName("table");
       
       System.out.println("input Param list: ");
       for(int j=0;j<inparamList.getLength();j++){
    	   Element param = (Element) inparamList.item(j);
    	   
    	   String paramName = param.getAttribute("name");
    	   String paramType = param.getAttribute("type");
    	   System.out.println("\t"+ paramName+"\t\t\t"+paramType);
       }
        
       System.out.println("output Param list: ");
       for (int j=0;j<outparamList1.getLength();j++){
    	   
    	   Element param = (Element) outparamList1.item(j);
    	   
    	   String paramName = param.getAttribute("name");
    	   String paramType = param.getAttribute("type");
    	   System.out.println("\t"+ paramName +"\t\t\t"+paramType);
    	   
    	   
       }
       System.out.println("output Table list :");
       
       for (int k=0;k<outparamList2.getLength();k++){
    	  
    	   
    	   Element tblparam = (Element)outparamList2.item(k);
    	   String tblname = tblparam.getAttribute("name");
    	   System.out.println("----table name : " + tblname);
    	   NodeList nl1 =tblparam.getElementsByTagName("column");
    	   
    	   for(int k1 =0;k1<nl1.getLength();k1++){
    		   Element e1 = (Element)(nl1.item(k1));
    		   
    		   System.out.println(e1.getAttribute("name"));
    	   }
    	   
    	   System.out.println("----End for table : " + tblname);
       }
 
     
 
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}


import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.Locator;
import org.xml.sax.XMLReader;

import org.xml.sax.InputSource;
import org.xml.sax.ext.LexicalHandler;
import org.xml.sax.helpers.DefaultHandler;
import java.io.IOException;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

public class TestSaxWithLexical extends DefaultHandler implements LexicalHandler
{
  private StringBuffer buf;

  public TestSaxWithLexical()
  {
    super();
  }
  public void setDocumentLocator(Locator locator)
  {
  }
  public void startDocument() throws SAXException
  {
    buf=new StringBuffer();
    System.out.println("*******StartDoc*******");
  }
  public void endDocument() throws SAXException
  {
   System.out.println("*******endDoc*******");
  }


  public void processingInstruction( String target, String instruction )
        throws SAXException
  {
  System.err.println("got " + instruction);
  }
  public void ignorableWhitespace( char[] chars, int start, int length ) throws SAXException
  {
  }



  public void startElement(String namespaceURI,String localName,String qName,Attributes atts)
  {
   // System.out.println("*******StartEle*******");
  
    if (true||qName.equalsIgnoreCase("operation")){
   
      for(int i=0;i<atts.getLength();i++)
        {
           System.out.println(" "+atts.getQName(i)+"\t :"+atts.getValue(i));
          
        }
    }
 
  }

  public void endElement(String namespaceURI,String localName,String fullName )throws SAXException
  {
   //System.out.println("******endEle********" + fullName);
  }
  public void characters( char[] chars, int start, int length )throws SAXException
   {
                      
          buf.append(chars,start,length);
   }

public static void main(String args[])
{
   try{
   
    SAXParserFactory sf  = SAXParserFactory.newInstance();
    SAXParser sp = sf.newSAXParser();
    TestSaxWithLexical testsax=new TestSaxWithLexical();
    XMLReader xmlReader = sp.getXMLReader();
    xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler",
        testsax);
   
 
  sp.parse(new InputSource("operation.xml"),testsax);

   }catch(IOException e)
   {
    e.printStackTrace();
   }catch(SAXException e)
   {
     e.printStackTrace();
   }catch(Exception e)
   {
    e.printStackTrace();
   }

}
public void comment(char[] arg0, int arg1, int arg2) throws SAXException {
// TODO Auto-generated method stub
String str = new String(arg0,arg1,arg2);
System.err.println(str);

}
public void endCDATA() throws SAXException {
// TODO Auto-generated method stub
System.err.println(buf.toString());
System.err.println("EOF CDADA");

}
public void endDTD() throws SAXException {
// TODO Auto-generated method stub

}
public void endEntity(String arg0) throws SAXException {
// TODO Auto-generated method stub

}
public void startCDATA() throws SAXException {
// TODO Auto-generated method stub
System.err.println("Begin CDaTA");

}
public void startDTD(String arg0, String arg1, String arg2) throws SAXException {
// TODO Auto-generated method stub


}
public void startEntity(String arg0) throws SAXException {
// TODO Auto-generated method stub

}
}

你可能感兴趣的:(xml,ext,J#)