Java implements DOM document manipulation and XML file conversion

Source: Internet
Author: User
Tags interface object model

dom|xml| Conversion



This article briefly describes the concept and internal logic structure of DOM, which describes the Java implementation process of DOM document manipulation 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:










     

       Gets the parser documentbuilderfactory factory = Documentbuilderfactory.newinstance () of an XML file to parse the XML file to generate the interface class for the DOM document to access the DOM. Documentbuilder builder = Factory.newdocumentbuilder ();d ocument = 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.










     

       Gets the root elements element element = Document.getdocumentelement ();//Gets the child node list of the root element nodelist = Element.getchildnodes (); The recursive method is used to implement the traversal GetElement (nodelist) of DOM documents;









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 a child node 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.










     

       Gets the root object    element root = Document.getdocumentelement ();//Adds an element node element booktype to the DOM document    = Document.createelement ("computes");//convert the node to a child node of the root object    root.appendchild (CDROM);//Add an element node    to the DOM document Element booktitle = document.createelement ("Title");//convert the node to a child node of the BookType object    booktype.appendchild (booktitle); /Add a text node to the DOM document    BookName = document.createTextNode ("Understand Corba"); The node is transformed into a bookname booktitle.appendchild (bookname) of the object;



4.4 Converting DOM documents to XML files










     

       The converter that transforms the DOM document into an XML file is implemented in jdk1.4, and the class Transformer implements the conversion API in the transformerfactory//.      transformerfactory tfactory = Transformerfactory.newinstance ();      Transformer Transformer = Tfactory.newtransformer ()//Converts a DOM object to a Domsource class object, which is represented as an information container that is converted 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, such as an XML file, a text file, an HTML file. Here is an XML file. *    /Streamresult result = new Streamresult (New File ("Text.xml"))//Invoke API to convert DOM documents to XML files.    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.







Related Article

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.