Java Siege Lion Road-review xml&dom_pull programming continuation

Source: Internet
Author: User
Tags stream api

In this chapter we will learn three parsing methods of XML:

1. JAXP DOM Parsing
2. JAXP SAX Parsing
3. XML Pull for STAX parsing

The main enterprise application of XML technology
1. Storage and transfer of data
2, as the framework of the configuration file

Storing and transferring data using XML
1. Generate XML through the program
2. Reading XML parsing----XML data

What is DOM and sax?
DOM Document Object Model-----Documents
Dom idea: Loading the entire XML into memory, forming a Document object, all of the XML operations on the memory Chinese Document object
DOM is the official XML parsing standard
* Supported by all development languages

Both Java and JavaScript support DOM

SAX Easy API FOR XML-----XML Simple API
Why does the programmer invent the Sax parsing method?? When XML documents are very large, it is not possible to load all XML data into memory
SAX thinking: One side parsing, one side processing, freeing up memory resources----not allowing large-scale XML data to be preserved in memory

StAX the stream API for XML-----XML stream API
STAX is a pull-mode XML parsing method, and SAX is a push-mode XML parsing method

Push-Push mode: Server-led, proactively sending data to clients
Pull mode: Client-led, proactively requesting data from the server

Programmers in real-world development, using a toolkit that has been developed ----- JAXP, dom4j, XML Pull

Parsing and parsing development package relationship?
Parsing method is to parse the XML idea, no specific code, parsing the development package is to parse the XML idea specific code implementation

JAXP is Sun's official launch implementation technology while supporting DOM SAX STAX
DOM4J is open source Community Open source Framework supports DOM parsing method
XML pull Android mobile device built-in XML parsing technology Support STAX parsing method

When sax and Stax read XML data, the entire XML document data will be present in memory if the read to memory data is not freed-----
(similar to DOM supports modification and writeback).

DOM, SAX, STAX in the actual development of the choice?
----The use of DOM in Java EE Daily development (simple programming)
When there is a lot of XML document data, it is impossible to use DOM---cause memory overflow------priority use of Stax
Mobile development uses STAX----Android XML Pull

JAXP Development FOR XML parsing:
Javax.xml.parsers storing DOM and SAX parsers
Javax.xml.stream storing STAX parsing related classes
Org.w3c.dom data node class when storing DOM parsing
Org.xml.sax store Sax parsing related tools class

DOM parsing Quick Start
1. Create an XML document Books.xml
In the actual development of the enterprise, in order to simplify the XML generation and parsing----XML data files usually do not use constraints
2. Parsing XML using the DOM
Load the entire XML document into memory: Factory---Parser---parse load
3, document obtains the node collection through the getElementsByTagName NodeList
Provides getlength and item traversal node collection via NodeList

Traverse ArrayList
for (int i=0;i<arraylist.size (); i++) {
Arraylist.get (i);
}

Traverse NodeList
for (int i=0;i<nodelist.getlength (); i++) {
Nodelist.item (i); -----will traverse each node to convert the sub-interface type
}

What is Node? For XML, all XML data is node nodes, including:
(element nodes, attribute nodes, text nodes, annotation nodes, CDATA nodes, document nodes).

Element Attr Text Comment cdatasection Document-----is a Node sub-interface

Node has three common APIs:
Getnodename (): Returns the name of the node
Getnodetype (): Returns the type of the node
Getnodevalue (): Returns the value of the node----all element nodes value is null

------------------------------------------------------------------------------------
A summary of DOM programming ideas
1. Loading XML documents----Document
2. Document gets the specified element-----getElementsByTagName (return NodeList)
3. Traverse nodelist to get each Node
4. Force each node to convert Element
5. Manipulating attributes and text content via element node API
GetAttribute Getting property values
Gettextcontent getting the inner text content of an element

<?XML version= "1.0" encoding= "UTF-8"?><Books>    < Book>        <name>Java Programming Ideas</name>        < Price>88</ Price>    </ Book>    < Book>        <name>Master of Java Programming</name>        < Price>99</ Price>    </ Book></Books>
Books2.xml
 Packagecom.shellway.javase;Importjava.io.IOException;ImportJavax.xml.parsers.DocumentBuilder;Importjavax.xml.parsers.DocumentBuilderFactory;Importjavax.xml.parsers.ParserConfigurationException;Importorg.junit.Test;Importorg.w3c.dom.Document;Importorg.w3c.dom.Element;Importorg.w3c.dom.NodeList;Importorg.xml.sax.SAXException; Public classDemo1 {@Test Public voidGetElement ()throwsparserconfigurationexception, saxexception, ioexception{documentbuilderfactory doubuilderfactory=documentbuilderfactory.newinstance (); Documentbuilder Documentbuilder=Doubuilderfactory.newdocumentbuilder (); Document Document= Documentbuilder.parse ("Xml/books2.xml"); //get the price of the book "Java Programming Ideas" through the parent nodeNodeList NodeList = document.getElementsByTagName ("book");  for(inti = 0; I < nodelist.getlength (); i++) {element element=(Element) Nodelist.item (i); Element element1= (Element) element.getchildnodes (). Item (1); if(Element1.gettextcontent (). Equals ("Java Programming Idea") {System.out.println (Element.getchildnodes (). Item (3). Gettextcontent ()); } }} @Test Public voidGetElement2 ()throwsparserconfigurationexception, saxexception, ioexception{documentbuilderfactory documentbuilderfactory =documentbuilderfactory.newinstance (); Documentbuilder Documentbuilder=Documentbuilderfactory.newdocumentbuilder (); Document Document= Documentbuilder.parse ("Xml/books2.xml"); //get the price of the book "Java Programmer" by the Brother nodeNodeList NodeList = document.getElementsByTagName ("name");  for(inti = 0; I < nodelist.getlength (); i++) {element element=(Element) Nodelist.item (i); if(Element.gettextcontent (). Equals ("Master Java Programmer") ) {String str=element.getnextsibling (). getnextsibling (). Gettextcontent ();            System.out.println (str); }        }     }}
get the price of the book "Java Programmer master"

First Use global lookup lock range, then use relative relationship to find the required data

The getElementById method must be used with constraints in the XML document!!!!!!!

Therefore, the development language default support DTD, when using schema, separate programming to import schema!

XML DOM Add, modify, and delete operations------manipulate in-memory document objects

Write-back of XML

XML element add: 1, create node element 2, add node element to specified position

XML element modification: query to specified element 1, modify attribute SetAttribute
2. Modify element text content Settextcontent

XML element Delete: Delete node. Getparentnode (). RemoveChild (Delete node)

--------------------------------------------------------------------------
Sax and STAX are event-driven-----sax push mode STAX pull mode
Sax Common Events
Startdocument ()----Document Start Event
Startelemen ()----element Start Event
Characters ()----text element events
EndElement ()----element End Event
Enddocument ()-----Document End Event

Why does sax mean push mode parsing? Parser controls XML file parsing, which invokes the corresponding event method by the parser

Get start and end element names in Startelemen () endElement ()
Get read to text content in characters ()
Read property values in Startelemen ()


STAX mode XML parsing mode-----client program, self-control XML events, actively invoke the corresponding event method

When using XML pull if using Android system, the system built-in without downloading any development package, if you want to javase Java EE
Download the separate pull development kit using pull parsing technology.

XPP3-----XML Pull Parser 3 is the pull API code implementation

Using the Pull Parser
1, go to the site download Pull parser implementation XPP3 (Android built-in)
2. Import Xpp3-1.1.3.4.c.jar into Java engineering
The import jar package is within the current project, creates a new Lib within the project, copies the jar, and adds the pull parser jar to the build path.

Jar package is a. class file Collection compressed package (compressed in zip format)

The pull parser uses the Stax parsing method to parse the----pulling mode
Pull takes the XML document pass parser, manually triggers the document parsing event through next, gets the current event in the client code, and invokes the corresponding event-handling method

3. Create Pull Parser
4. Passing the XML document content to the pull parser

Why is STAX parsing more efficient than SAX?
1, SAX is not selective, all events will be resolved, Stax by the user control needs to handle the event type
2, when using Stax data analysis, at any time to terminate the resolution

The pull parser generates XML document functionality----generate XML documents from XmlSerializer
Parsing xml: Document start, element start, text element, end of element, end of document
Generate XML: Generate document declaration (document start), element start, text content, end of element, end of document
1. Generate Simple XML
2. Generating XML from Object data
3. Generating XML from the object list data

Two methods are extracted in the program-----1. The XML---list Object 2. The list object generates XML

Curd operations on the in-memory list


Homework:
1. Organize DOM, SAX, STAX principles and differences
2. Write the product XML file---Complete the curd operation by Jaxp DOM
3. Write a sax run case-----Understand
4, write the product XML file---through the pull parser to complete the curd operation----Key Xml2list List2xml two methods

Java Siege Lion Road-review xml&dom_pull programming continuation

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.