Java foundation of XML parsing

Because of the recent WebServer server to practice handwriting, which need to use XML parsing,

Found not previously written a parsing XML parsing stuff, and today make up about.

XML parsing mode

  • DOM parsing
  • SAX parsing
  • JDOM parsing
  • DOM4J resolve

Today the only way to write SAX parsing mode:

    advantage:

      Parsing speed,

      Small footprint,

    Disadvantages:

      Only read XML,

      You can not modify XML,

      You can not know the node currently unresolved.

I use SAX parsing summed up in these few steps:

  1. Create a SAX parser factory objects
  2. Gets an XML parser by factory object
  3. We can be parsed by xml parser parse method

      parse(InputStream is, HandlerBase hb) 

      A parameter: This is an input stream of bytes, here may be specified by the xml file for class loader getResourceAsStream

      Parameter Two: To use SAX HandlerBase, this definition needs to inherit Handle DefaultHandler, is to be resolved by the method of rewriting the node class

Code link

1  // Get a SAX parser factory 
2 the SAXParserFactory Factory = SAXParserFactory.newInstance ();
 . 3  // Get parser 
. 4 the SAXParser Parser = factory.newSAXParser ();
 . 5  // Create Handler class analysis 
. 6 PHandler pHandler = new new PHandler () ;
 7  // specified file parsed by the parse method and parsing the class 
. 8 parser.parse (.. Thread.currentThread () getContextClassLoader () the getResourceAsStream ( "NET / cncandy / parseXML / p.xml"), pHandler);

Parameter getResourceAsStream Here, if the file is placed inside the bag, here to fill in the parameters should be in the package name changed to the point / on it, remember to add the xml file name

PHandler we said above is directly written a class that is inherited DefaultHandler class, we need to implement some methods inside, let's introduce our method needed.

starting document Receive notification at the beginning of the file, by default, do nothing. Application writers may override this method in a subclass, to perform certain operations at the beginning of the document
startElement Receiving a notification element to start with, by default, do nothing. Application writers may override this method in a subclass to take a specific action at the beginning of each element
characters Receiving notification of character data inside an element. It can be converted by way of new String to string output ch
endElement Receive notification at the end of the element. By default, do nothing. Application writers may override this method in a subclass, to perform certain operations at the end of each element (e.g., completion tree node or writing output file). 
endDocument The receiving end of the file notice. By default, do nothing. Application writers may override this method in a subclass, the end of the document in order to perform specific operations
. 1  class PHandler the extends the DefaultHandler {
 2      // document parsing 
. 3      @Override
 . 4      public  void the startDocument () throws a SAXException {
 . 5          System.out.println ( "document parsing" );
 6      }
 . 7      
. 8      // start parsing element 
. 9      @Override
 10      public  void the startElement (String URI, the localName String, String the qName, the Attributes Attributes) throws a SAXException {
 . 11  
12 is      }
 13 is      
14      @Override
 15     public void characters(char[] ch, int start, int length) throws SAXException {
16         String string = new String(ch,start,length).trim();
17         if(string.length()>0) {
18             System.out.println(string);
19         }
20         
21     }
22     
23     //结束解析元素
24     @Override
25     public void endElement(String uri, String localName, String qName) throws SAXException {
26 
27      }
 28      
29      // End parse the document 
30      @Override
 31 is      public  void the endDocument () throws a SAXException {
 32          System.out.println ( "end parse the document" );
 33      }
 34 }

XML node table, George is the characters , just forget to write

 

Guess you like

Origin www.cnblogs.com/mCarrYoung/p/11099699.html