XML parsing is divided into two ways: Dom Mode and sax mode
Dom:document object model. This approach is a way of dealing with XML as recommended by the consortium.
Sax:simple API for XML. This is not an official standard, it belongs to the open source community Xml-dev, and almost all XML parsers support it.
XML Parsing Development Package
JAXP: It is the analytic standard implementation that sun company launches.
DOM4J: is the open source organization launched the Analytic Development package.
JDom: is the open source organization launched the Analytic Development package.
Jaxp:
JAXP: (Java API for XML Processing) The development package is part of the javase, which consists of the following packages and their child packages:
Org.w3c.dom: Provides a standard interface for parsing XML in DOM mode
Org.xml.sax: Provides a standard interface for parsing XML in sax mode
Javax.xml: provides classes to parse XML documents
In the Javax.xml.parsers package, several factory classes are defined. We can get the DOM and Sax parser objects that parse the XML document by invoking these factory classes. Documentbuilderfactory
SAXParserFactory
Using JAXP for DOM parsing
The documentbuilderfactory used in the Javax.xml.parsers package to create the parser object for the DOM pattern, Documentbuilderfactory is an abstract factory class that cannot be instantiated directly, However, this class provides a Newinstance method that automatically creates a factory object and returns it.
Get the DOM parser in JAXP
Call the Documentbuilderfactory.newinstance () method to get the factory that created the DOM parser.
Invokes the Newdocumentbuilder method of the factory object to get the DOM parser object.
Invoking the Parse () method of the DOM parser object parses the XML document and obtains the Document object representing the entire file, which can be manipulated using the DOM attribute for the entire XML document.
DOM Programming:
DOM Models (Document Object model)
When parsing an XML document, the DOM parser parses all the elements in the document into a Node object (nodes) according to the hierarchical relationships that appear.
In the DOM, the relationships between nodes are as follows:
The node on top of a node is the parent of the node (parent)
The node below a node is the child node of the node (children)
At the same level, a node with the same parent node is a sibling node (sibling)
Node collection for the next level of a node is a node descendant (descendant)
The parent, grandparent, and all nodes above the node are ancestors of the node (ancestor)
DOM parsing programming
1, to get a specific node content
2. Traverse all ELEMENT nodes
3. Modify the main content of an element node
4. Adding child element nodes to the specified element node
5. Increase the sibling element node to the specified element node
6. Delete the specified element node
7. Manipulating XML file properties
Sax parsing:
When parsing an XML document using the DOM, the entire XML document needs to be read, the Doucment object representing the entire DOM tree is architected in memory, and the XML document is then manipulated. In this case, if the XML document is particularly large, it consumes a large amount of memory from the computer and is prone to memory overflow.
Sax parsing allows documents to be processed when they are read, without having to wait until the entire document is loaded before the document is manipulated.
To parse the XML document in sax mode:
Use SAXParserFactory to create a sax resolution factory SAXParserFactory SPF = saxparserfactory.newinstance ();
Get Parser object SAXParser SP = Spf.newsaxparser () through Sax parsing factory;
An XML reader is obtained through the parser object XMLReader XMLReader = Sp.getxmlreader ();
Sets the reader's event handler Xmlreader.setcontenthandler (New Bookparserhandler ());
Parse XML file Xmlreader.parse ("Book.xml");
006_03java parsing XML DOM mode and sax mode