Java implementation of DOM document manipulation and XML file conversion (paste)

Source: Internet
Author: User
Tags add time interface object model
Java implementation of dom|xml| conversion DOM document manipulation and XML file conversion

[Author: Guo Hongfeng Add Time: 2001-10-19 8:16:09]




Guo Hongfeng (ghf_email@sohu.com)

Introduction: This paper briefly describes the concept and internal logic structure of DOM, and gives the Java implementation process of DOM document operation and XML file conversion.
1. Introduction to DOM
Currently, the consortium has introduced the specification DOM level 2 on November 13, 2000. The Document Object Model (DOM) is a programming interface specification for HTML and XML documents that is independent of platform and language and can be implemented on a variety of platforms in a variety of languages. The model defines the logical structure of the THML and XML files in memory (that is, documents), providing a way to access, Access THML, and XML files. The DOM specification enables the conversion of DOM documents and XML, traversing and manipulating the contents of the corresponding DOM document. It can be said that in order to manipulate the XML file freely, it is necessary to use the DOM specification.

2. DOM internal Logical Structure
The logical structure in a DOM document can be expressed in the form of a node tree. By parsing the XML file, the elements in the XML file are transformed into node objects in the DOM document. The document nodes of the DOM have node types such as document, Element, Comment, type, and so on, where each DOM document must have a document node and be the root node of the node tree. It can have child nodes, or leaf nodes such as text nodes, comment nodes, and so on. Every element in a well-formed XML file has a node type in the DOM document that corresponds to it. Using the DOM interface to transform the XML file into a DOM document, we are free to process the XML file.

3. Dom interface in Java
The specification of APIs provided by the DOM specification, the Java API in the jdk1.4 beta version of the current Sun company follows the semantic description of the DOM Level 2 core recommendation interface, providing the corresponding Java language implementation.

In Org.xml.dom, jkd1.4 provides interfaces such as document, DocumentType, Node, NodeList, Element, and text, which are required to access DOM documents. We can use these interfaces to create, traverse, and modify DOM documents.

In Javax.xml.parsers, the Doumentbuilder and documentbuilderfactory combinations provided by jkd1.4 can parse the XML file and convert it into a DOM document.

In Javax.xml.transform.dom and Javax.xml.transform.stream, jdk1.4 provides Domsource classes and Streamsource classes that can be used to write updated DOM documents to the generated XML file.

4. Routine

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

In Jdk1.4, the document interface describes the documentation tree corresponding to the entire XML file, provides access to the document data, and is the goal of the step. The document interface can be obtained from the class Documentbuilder, which contains an API to obtain a DOM document instance from an XML document. The parser for XML can be obtained from the class documentbuilderfactory. In jdk1.4, XML files can be translated into DOM documents with the following code implementations:
To obtain a parser for an XML file
Documentbuilderfactory factory = Documentbuilderfactory.newinstance ();
Parses an XML file to generate an interface class for a DOM document to access the DOM.
Documentbuilder builder = Factory.newdocumentbuilder ();
Document = Builder.parse (new File (FileName));



4.2 Traverse DOM Document
After you get the interface class document instance, you can access the document tree of the DOM. To traverse a DOM document, you first have to get the root element. The list of child nodes of the root element is then obtained. Here the recursive method is used to achieve the purpose of traversal.


Get root element
Element element = Document.getdocumentelement ();
Gets the list of child nodes of the root element
NodeList = Element.getchildnodes ();
Recursive method for the traversal of DOM documents
GetElement (nodelist);
Where the GetElement method is implemented as follows:
public void GetElement (NodeList nodelist) {
Node Cnode;
int I,len;
String str;

if (nodelist.getlength () = = 0) {
The node does not have child nodes
Return
}
for (I=0;i 1)
System.out.println ("" "+str+" "+len");
}
}
}

Note: The above code is just an object that shows node type and text type. Their type identities are 1 and 3, respectively.

4.3 Modifying DOM documents
The API for modifying the DOM document is illustrated in the DOM Level 2 core specification, which is implemented in the Org.xml.dom in jkd1.4. Modifying DOM document operations are mainly concentrated in the documents, Element, Node, text, and so on, where the examples given are to add a series of objects to the parsed DOM document and add a record to the XML file.
Get root Object
Element root = Document.getdocumentelement ();
Add an element node to a DOM document
Element BookType = document.createelement ("computes");
Converts the node to a child node of the root object
Root.appendchild (CDROM);
Add an element node to a DOM document
Element booktitle = document.createelement ("Title");
Converts the node into a child node of the BookType object
Booktype.appendchild (BookTitle);
Add a text node to the DOM document
Text bookname = document.createTextNode ("Understand Corba");
Converts the node into a child node of the BookName object
Booktitle.appendchild (BookName);



4.4 Converting DOM documents to XML files
Get the converter to convert the DOM document into an XML file, in jdk1.4, there are classes transformerfactory
To implement, the class transformer implements the transformation API.
Transformerfactory tfactory = Transformerfactory.newinstance ();
Transformer Transformer = Tfactory.newtransformer ();
Converts a DOM object into a Domsource class object that is represented by an information container that is transformed into another form of expression.

Domsource Source = new Domsource (document);
Gets a Streamresult class object that is a container for other forms of document that the DOM document translates into, which can be XML files, text files, HTML files. Here is an XML file.
Streamresult result = new Streamresult (New File ("Text.xml"));
Invokes the API to convert the DOM document into an XML file.
Transformer.transform (Source,result);


The complete program for the routine, which runs through the jdk1.4 environment in Windows 2000, is provided here.

This gives an example of how the reader can learn about the DOM operation. Because operations on the DOM follow the DOM specification, they also apply to the processing of the DOM in other languages.

Resources:

http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-2000-1113
Java 2 Platform, Standard Edition, V1.4.0 API specificaion

About the author
Guo Hongfeng: Mainly engaged in the development and research of Distributed Application under UNIX system. You can contact him via email ghf_email@sohu.com.


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.