XML-based JAXP Parsing

Source: Internet
Author: User
Tags xml document reader

XML-based JAXP Parsing

JAXP: a set of xml parsing developed by sun

It consists of javax. xml. parsers, org. w3c. dom, org. xml. sax, and its sub-packages.


JAXP has two Resolution Methods:


I. dom parsing: Read documents into memory to produce Document objects

Ii. sax parsing: read from the top down and read a row to process a row


Dom advantages: convenient for document CRUD

Disadvantage: High memory consumption


Advantages of sax: Small memory consumption, fast resolution, and suitable for reading documents

Disadvantage: CUD is inconvenient


Instance xml document

 
 <书架>
  <书>
   <书名 id="1234">
    
. Net
   
   <作者>
    
Wht
   
  
  <书>
   <书名>
    
JavaEE
   
   <作者>
    
Www
   
  
 


Dom parsing steps

Create the dom factory DocumentBuilderFactory to get the dom parser DocumentBuilder to read the xml Document and get the Document
Package Jaxp. dom; import javax. xml. parsers. documentBuilder; import javax. xml. parsers. documentBuilderFactory; import javax. xml. transform. transformer; import javax. xml. transform. transformerFactory; import javax. xml. transform. dom. DOMSource; import javax. xml. transform. stream. streamResult; import org. junit. test; import org. w3c. dom. document; import org. w3c. dom. element; import org. w3c. dom. node; import org. w3c. dom. nodeList; public class Dom {// read a specified tag in the document @ Testpublic void read1 () throws Exception {// 1. Create the dom factory DocumentBuilderFactory factory = DocumentBuilderFactory. newInstance (); // 2. Create the parser DocumentBuilder builder = factory. newDocumentBuilder (); // 3. Read the xml file and generate the Document Object Document document = builder. parse ("src/book. xml "); Node node = document. getElementsByTagName (" "). item (0); System. out. println (node. getNodeName ();} // read the tag @ Testpublic void read2 () throws Exception {// 1. Create the dom factory DocumentBuilderFactory factory = DocumentBuilderFactory. newInstance (); // 2. Create the parser DocumentBuilder builder = factory. newDocumentBuilder (); // 3. Read the xml file and generate the Document Object Document document = builder. parse ("src/book. xml "); Node root = document. getElementsByTagName ("bookshelf "). item (0); list (root);} private void list (Node node) {// TODO Auto-generated method stubSystem. out. println (node. getNodeName (); NodeList nodes = node. getChildNodes (); for (int I = 0; I
 
  

Create a sax factory SAXParserFactory. Get the sax Parser SAXParser. Get the reader XMLReader. Set the content processing processor reader. setContentHandler () to read the xml document.
Package Jaxp. sax; import java. io. IOException; import javax. xml. parsers. parserConfigurationException; import javax. xml. parsers. SAXParser; import javax. xml. parsers. SAXParserFactory; import org. xml. sax. attributes; import org. xml. sax. contentHandler; import org. xml. sax. locator; import org. xml. sax. SAXException; import org. xml. sax. XMLReader; public class Demo {public static void main (String [] args) throws ParserConfigurationException, SAXException, IOException {// 1. Create a sax factory SAXParserFactory factory = SAXParserFactory. newInstance (); // 2. Get the parser SAXParser saxParser = factory. newSAXParser (); // 3. Obtain the reader XMLReader reader = saxParser. getXMLReader (); // 4. Set the content processor reader. setContentHandler (new XMLHandler (); // 5. Read the xml document reader. parse ("src/book. xml ") ;}} class XMLHandler implements ContentHandler {@ Overridepublic void startElement (String uri, String localName, String qName, Attributes atts) throws SAXException {// TODO Auto-generated method stubSystem. out. println ("<" + qName + ">"); for (int I = 0; I ") ;}@ Overridepublic void characters (char [] ch, int start, int length) throws SAXException {// TODO Auto-generated method stubSystem. out. println (new String (ch, start, length) ;}@ Overridepublic void setDocumentLocator (Locator locator) {// TODO Auto-generated method stub} @ Overridepublic void startDocument () throws SAXException {// TODO Auto-generated method stub} @ Overridepublic void endDocument () throws SAXException {// TODO Auto-generated method stub} @ Overridepublic void startPrefixMapping (String prefix, string uri) throws SAXException {// TODO Auto-generated method stub} @ Overridepublic void endPrefixMapping (String prefix) throws SAXException {// TODO Auto-generated method stub} @ Overridepublic void ignorableWhitespace (char [] ch, int start, int length) throws SAXException {// TODO Auto-generated method stub} @ Overridepublic void processingInstruction (String target, String data) throws SAXException {// TODO Auto-generated method stub} @ Overridepublic void skippedEntity (String name) throws SAXException {// TODO Auto-generated method stub }}

Sun provides DefaultHandler
Package Jaxp. sax; import java. io. IOException; import javax. xml. parsers. parserConfigurationException; import javax. xml. parsers. SAXParser; import javax. xml. parsers. SAXParserFactory; import org. xml. sax. attributes; import org. xml. sax. contentHandler; import org. xml. sax. locator; import org. xml. sax. SAXException; import org. xml. sax. XMLReader; import org. xml. sax. helpers. defaultHandler; public class Demo2 {public static void main (String [] args) throws ParserConfigurationException, SAXException, IOException {// 1. Create a sax factory SAXParserFactory factory = SAXParserFactory. newInstance (); // 2. Get the parser SAXParser saxParser = factory. newSAXParser (); // 3. Obtain the reader XMLReader reader = saxParser. getXMLReader (); // 4. Set the content processor reader. setContentHandler (new TagValue (); // 5. Read the xml document reader. parse ("src/book. xml ") ;}} // obtain the second author's name class TagValue extends DefaultHandler {private String currentTag; private int currentNum; private int needTagNum = 2; @ Overridepublic void startElement (String uri, String localName, String qName, Attributes attributes) throws SAXException {currentTag = qName; if (currentTag. equals ("author") {currentNum ++ ;}@ Overridepublic void characters (char [] ch, int start, int length) throws SAXException {if ("author ". equals (currentTag) & export tNum = needTagNum) {System. out. println (new String (ch, start, length) ;}@ Overridepublic void endElement (String uri, String localName, String qName) throws SAXException {currentTag = null ;}}



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.