DOM parsing XML

xiaoxiao2021-03-06  104

1. DOM introduction Currently, W

3C

Already

2000

November

13th

It has launched a regulatory DOM Level 2. Document Object Model (DOM) is a programming interface specification for HTML and XML documents, which is unrelated to platforms and languages, so it can be implemented on various platforms with various languages. This model defines the logical structure (ie, document) in the THML and XML files (ie, documentation), provides access, accessing THML, and XML files. With the DOM specification, the DOM documentation and XML can be implemented, traversal, and the contents of the corresponding DOM document can be used. It can be said that the DOM specification is to be used freely to manipulate the XML file.

2. The logical structure in the DOM internal logic structure DOM document can be expressed in the form of a node tree. By processing the XML file, the elements in the XML file are transformed into node objects in the DOM document. The DOM document node has Document, Element, Comment, Type and other node types, where each DOM document must have a Document node and the root node of the node tree. It can have subtocks, or leaves nodes such as Text nodes, Comment nodes, and more. Each element in any format is in a good XML file has a node type in the DOM documentation. After transforming the XML file into a DOM documentation using the DOM interface, we can freely process the XML file.

3. The DOM interface DOM specification provided by the DOM interface in Java provides the JAVA API in the JDK1.4 beta of Sun, which follows the semantic description of the Dom Level 2 Core recommendation interface, which provides the implementation of the corresponding Java language.

In org.xml.dom, JKD 1.4 provides interfaces such as Document, DocumentType, Node, NodeList, Element, and Text, which are all necessary to access the DOM document. We can use these interfaces to create, traverse, and modify the DOM documentation.

In javax.xml.parsers, the DoumentBuilder and DocumentBuilderFactory combinations provided by JKD1.4 can parse XML files to the DOM documentation.

In javax.xml.transform.dom and javax.xml.transform.Stream, JDK1.4 provides the DomSource class and the StreamSource class, which can be used to write updated DOM documents in the generated XML file.

4. routine

4.1 Transforming an XML file into a DOM document This process is a process of obtaining an XML file parser to parse the XML file into a DOM document.

In JDK1.4, the Document interface describes the document tree corresponding to the entire XML file, providing access to document data, which is the goal of this step. The Document interface can be obtained from class DocumentBuilder, which contains an API of the DOM document instance from an XML document. XML's parsers can be obtained from class DocumentBuilderFactory. In JDK1.4, the XML file translates into a DOM documentation can have the following code implementation:

// Analyze a parser for an XML file

DocumentBuilderFactory Factory = DocumentBuilderFactory.newinstance ();

// Analyze the interface class of the XML file to generate the DOM document to access the DOM.

DocumentBuilder Builder = Factory.NewDocumentBuilder ();

Document = Builder.Parse (New File (FileName);

4.2 After traversing the DOM document Get an interface class Document instance, you can access the DOM's document tree. To traverse the DOM document, first get the root element. Then get a list of bon nodes of the root element. The purpose of traversing is achieved by recursive methods. // Get root elements

ELEMENT Element = Document.getDocumentelement ();

// List of child nodes for root elements

Nodelist = element.getChildNodes ();

// Use the recursive method to realize the traversal of the DOM documentation

GetElement (nodelist);

The getElement method is implemented as follows:

Public void getElement (nodelist nodelist) {

Node CNODE;

INT I, LEN;

String Str;

IF (nodelist.getLength () == 0) {

// This node has no child node

Return;

}

For (i = 0; i 1)

System.out.println (" STR " " LEN);

}

}

}

Note: The above code is just an object that displays the Node type and the TEXT type. Their type identifiers are 1 and 3, respectively.

4.3 Modifying the DOM Document Modify the API of the DOM documentation in the Dom Level 2 Core specification, which implements these APIs in the org.xml.dom in JKD1.4. Modifying the DOM documentation This is mainly set in Document, Element, Node, Text and other classes, the example herein adds a series of objects in the parsed DOM document, and corresponds to add a record in the XML file.

// Get root objects

Element Root = Document.getDocumentElement ();

/ / Add an Element node in the DOM documentation

Element BookType = Document.createElement ("computes");

// Translate the node into the child node of the root object

Root.Appendchild (CDROM);

/ / Add an Element node in the DOM documentation

Element booktitle = document.createElement ("Title");

// Translate the node into the child node of the BookType object

BookType.Appendchild (Booktitle);

// Add a Text node in the DOM documentation

Text bookname = document.createtextNode ("Understand Corba");

// Translate the node into the child node of BOOKNAME object

Booktitle.Appendchild (BookName);

4.4 Transform the DOM document into XML file

// Get converted the DOM document into an XML file, in JDK1.4, there is class TransformerFactory

// Implementation, class Transformer implements the conversion of the API.

TransformerFactory Tfactory = TransformerFactory.newInstance ();

Transformer Transformer = TFActory.NewTransformer ();

// Translate the DOM object to a DomSource class object, which is manifested as an information container that is converted into other forms of expression.

Domsource Source = New Domsource (Document);

// Get a streamResult class object, which is a container for other forms of documents that DOM document transformation, which can be XML files, text files, HTML files. Here is an XML file. StreamResult Result = New StreamResult (New File ("text.xml");

// Call the API and convert the DOM document to an XML file.

Transformer.Transform (Source, Result);

This complete program of this routine is provided, which runs through the JDK1.4 environment in Windows 2000.

An example is given, and the reader can understand the idea of ​​DOM operation. Because the DOM specification is followed by the operation of the DOM, it is also applicable to the processing of the DOM in other languages.

转载请注明原文地址:https://www.9cbs.com/read-98037.html

New Post(0)