XML parsing in Java,

XML in the process of developing more and more important, we often need to parse XML documents in the programming. Parsing XML documents in Java in a variety of ways, each approach has its own characteristics. Here we use a different analytical methods by parsing XML documents to demonstrate

First, using DOM (Document Object Model Document Object Model) approach. Parsing XML documents using this method when loading the XML document as a whole, and the formation of the document object tree. All the contents of the document will form a tree of nodes (either elements, attributes or text). So that we can use random read mode for reading. Advantage is that we can use when parsing the document order of the contents of any operation, flexible operation. But the document needs a whole load. So when the contents of the document when the larger of, and we only need to read through of a small part, Xianran yes there Wen Ti's. Therefore, if we read the xml document is relatively small, this approach can be used. This method is relatively simple programming. (Ajax operation XML and Html generally use this method)

Suppose we have the following xml document the existence, name: student.xml, our way of parsing through DOM elements in the document

Note that comments section, the same process can be completed parsing the document. Why are child nodes to achieve when the serial number used for the 1,3,5-it? Because each student has a sub-nodes instead of 3 to 7, the text and student text contains (in this blank) is also a child node.

For XML parsing using SAX

Parsing XML with SAX is the order of nature. Is the top-down parsing XML documents. Event-driven mechanism is adopted. When faced with a node when processing immediately, not wait until the whole document loaded handle. SAX parsing provided by the use of a number of ways. When faced with a certain part of the document, the use of callback mechanism call the appropriate methods for proper handling. For example, SAX methods to manipulate a node exists: startElement and endElement

When faced with a start node calls startElement, after the end of node encounters a call to endElement. We can write these two methods to realize the specific process of parsing XML documents, read the content provided by using a method other characters

There is also time to start parsing the document and the document calls the startDocument call endDocument end analysis

The overall load without using SAX parsing the document, so the larger the document analysis is more suitable when using SAX approach. Analysis of speed and better than DOM, but the operation code will take some work.

Similarly, we resolve just students.xml, use of SAX, can be as follows:

相关文章

Quoted from http://www.ibm.com/developerworks/cn/java/l-javaxml/ This paper briefly discusses the Java programming language update XML documents in the four commonly used methods, and analysis of the merits of the four methods. Secondly, the article

1) DOM (JAXP Crimson parser) DOM is platform-and language-independent manner, said the official W3C standard XML document. DOM is a hierarchical organization of nodes or pieces of information collection. This hierarchy allows the developer to find sp

There are many ways to read and write XML ~ ~ JAVA in a rich class of the XML file at ~ ~ such XMLReader .... JDOM ... DOM and so on ~ ~ Here is what I found from online information and examples of successful pilot ~ ~ Using the DOM ~ ~ ~ do not need

XML documents with DOM4J on CRUD operations such as reading and writing is my own exercises, and notes there is no order and only later see use. Main method in the constructor to do the simple instructions, involved XML, XSD, DTD document is no longe

Android PULL technical analysis used in the system than using SAX XML documents much better technical performance. Android developers proposed technical parsing XML documents using PULL. SAX with similar, but the return type of event PULL technology

1, Libxml2 Description: Libxml2 is an xml parser c-language version, was originally a tool for the Gnome project development, based on the MIT License is a free open source software. It addition to supporting the c-language version, it also supports