Java implements DOM document manipulation and XML file conversion

Source: Internet
Author: User
Tags object model

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.DOM Introduction

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 Logic 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.

Dom interface in 3.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:

//获得一个XML文件的解析器
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//解析XML文件生成DOM文档的接口类,以便访问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.

//获得Root元素
Element element = document.getDocumentElement();
//获得Root元素的子节点列表
nodelist = element.getChildNodes();
//用递归方法实现DOM文档的遍历
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)
  // 该节点没有子节点
  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.

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.