Experience 4--xml Programming (CRUD)--dom, sax parsing

Source: Internet
Author: User
Tags documentation xml parser xml reader

1. XML Programming (CRUD), crud refers to: C is Create;r is the read;u is the update;d is delete, that is, the database of additions and deletions to check, XML technology is actually equivalent to the database.

2.XML Analysis Technology Overview

L XML parsing methods are divided into two types: Dom and sax

The DOM: (Document Object model, the Documentation objects module) is a recommended way for the organization to process XML.

sax: (Simple API for XML) is not an official standard, but it is the de facto standard for XML communities, and almost all XML parsers support it.

L XML Parser

Crimson, Xerces, Aelfred2

L XML Parsing Development package

Jaxp (API developed by Sun Company), Jdom, dom4j (the latter two are the convenience APIs developed by developers based on actual requirements)

The JAXP development package is part of the J2SE, which consists of Javax.xml, Org.w3c.dom, Org.xml.sax packages, and their child packages in the Javax.xml.parsers package, defines several factory classes, and the programmer calls these factory classes, You can get the parser object for the DOM or sax that resolves the XML document.

3. Using JAXP for DOM parsing

The documentbuilderfactory in the Javax.xml.parsers package is used to create the parser object for the DOM schema, Documentbuilderfactory is an abstract factory class that cannot be instantiated directly, However, this class provides a Newinstance method that automatically creates an object for a factory and returns it based on the parser installed by default on the local platform.

L CALL the Documentbuilderfactory.newinstance () method to get the factory that created the DOM parser.

L Gets the DOM parser object by invoking the Newdocumentbuilder method of the factory object.

L invoke the Parse () method of the DOM parser object to parse the XML document, get the Document object representing the entire file, and perform an operation on the entire XML document using the DOM attribute.

4.DOM programming

L DOM Model (Document Object model)

when parsing an XML document, the DOM parser parses all the elements in the document into node objects (nodes) according to the hierarchical relationships it appears.

• In the DOM, the relationships between nodes are as follows:

• The node above one node is the parent of the node (parent)

• Nodes under one node are child nodes of the node (children)

• At the same level, nodes with the same parent node are sibling nodes (sibling)

• The node set at the next level of a node is a node descendant (descendant)

• The parent, grandparent node, and all nodes above the node are the ancestors of the nodes (ancestor)

L Node Object

Provides a series of Changshilai representing node types, and when a developer obtains a node type, it is possible to convert node nodes into corresponding node objects (node subclass objects) to facilitate the invocation of their unique methods. (View API document) The node object provides a way to obtain its parent node or child node. These methods allow programmers to read the contents of an entire XML document, or to add, modify, or delete the contents of an XML document.

5.DOM Parsing xml file

L DOM parsing programming

• Traverse all nodes

• Find a node

• Delete Nodes

• Update Nodes

• Add Nodes

L Update XML documents

The transformer class in the Javax.xml.transform package is used to convert the Document object representing the XML file to a format for output, such as converting an XML file to an HTML document when it is applied to a style sheet. With this object, you can, of course, write the document object back into an XML file.

The L transformer class completes the conversion operation through the transform method, which receives a source and a destination. We can pass:

Javax.xml.transform.dom.domsource class to associate the Document object to be converted,

• Use the Javax.xml.transform.stream.StreamResult object to represent the destination of the data.

L transformer objects are obtained by Transformerfactory.

6.SAX parsing

L when parsing an XML document using the DOM, it is necessary to read the entire XML document, and in memory to construct the Doucment object representing the entire DOM tree, and then manipulate the XML document. In this case, if the XML document is particularly large, it consumes a large amount of memory on the computer and can easily cause a memory overflow.

L 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 processed, that is, while the Edge is being read. Sax parses XML files using event handling, parsing XML documents with sax, and involves two parts: parsers and event handlers: parsers can be created using Jaxp's API, and after creating a SAX parser, you can specify the parser to parse an XML document.

The L parser uses sax to parse an XML document, as long as it resolves to one part of an XML document, it invokes a method of the event handler, which, when invoking the method of the event handler, passes the contents of the current parsed XML file to the event handler as the parameter of the method.

L event handlers are written by programmers , and programmers can easily get the data parsed by the SAX parser through the parameters of the method in the event handler so that they can decide how to process the data.

7.SAX analytic diagram

8.SAX Parsing XML document

L Create a Sax parsing factory using SAXParserFactory

SAXPARSERFACTORYSPF = Saxparserfactory.newinstance ();

L GET the parser object through the Sax parsing factory

SAXPARSERSP = Spf.newsaxparser ();

L get an XML reader through the parser object

Xmlreaderxmlreader = Sp.getxmlreader ();

L Set the event handler for the reader

Xmlreader.setcontenthandler (Newbookparserhandler ());

The Bookparserhanler class is written by programmers

L Parsing XML files

Xmlreader.parse ("Book.xml");

Read ContentHandler API documentation, common methods:startelement, endelement, characters

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.