A talk on Java Learning (v)

Source: Internet
Author: User

1.Java Parsing of XML

I believe you are not unfamiliar with XML, meaning is Extensible Markup language. itself it is also a vector of data that appears in tree form. Then slowly the data becomes the information, the difference is that the information can include a mutable state so that the process of hard coding changes for the unified interface hard coding and the mutable state as information into the XML store. The only way to change the state implementation extension is to add a piece of text information to the XML, and the code does not need to be altered or recompiled. This flexibility was not expected when XML was born.

Of course, if the interface needs to be able to extract the configured information in the XML, the program can parse the canonical XML file, in Java, of course, improve the package to support this behavior. The two packages that I intend to refer to are Org.w3c.dom and javax.xml.parsers and. (You can browse the interface and class definitions in the middle of these packages)

Javax.xml.parsers package is very simple, no interface, two factories with two parsers. There are obviously two ways to parse XML: DOM parsing and sax parsing. In essence, there is no good who is bad, but the realization of the idea is not the same. Give an example of an XML file:

? XML version= "1.0" encoding= "UTF-8" >

<root >

<child name= "Kitty" >

A Cat

</child >

</root >

The so-called Dom parsing idea is to put the entire tree map into memory, need that node only need to search in the tree can read the properties of the node, content, etc., the advantage is that all nodes in memory can be repeated search re-use, the disadvantage is that the corresponding memory needs to be consumed.

Natural Sax parsing is to overcome the shortcomings of the DOM, with the event trigger as the basic idea, the order of the search down, hit the element before the trigger what event, what action to do after the encounter. Due to the need to write the processing of the trigger event, you need to use another custom handler, in the Org.xml.sax.helpers package. Its advantages of course is not to read the entire packet into memory, the disadvantage is only sequential search, go over it again.

It's easy to guess what kind of exposure to the Java framework, It's obviously DOM. Because a similar struts,hibernate framework configuration file is a very small part of configuration information, and requires frequent searches to read, of course, the DOM approach (in fact, inside Sax is also used in the DOM structure to store node information). Now no matter what the framework, it is difficult to find the use of Sax to parse the XML technology, if any one who knows, please let the author also learn.

Now that the parsing is in place, there is a need for a resolved storage location. Do not know whether you find Org.w3c.dom this package is not implemented class are all interfaces. Here I want to say how Java parsing XML is the JDK should consider, it is its responsibility. While the consortium is the organization that maintains the definition of XML standards, how an XML structure is decided by the world's consortium, it does not care how Java is implemented, and thus dictates the rules that all XML storage structures should follow, which is the purpose of all the interfaces in Org.w3c.dom. In the author's opinion, the concept of simple understanding interface is the principle that the implementing person must obey.

The entire XML corresponding structure is called the document, the child element corresponding to is called the element, also has the node related nodes, NodeList, Text, Entity, Characterdata, cdatasection and so on interface, They can all find a corresponding meaning in the middle of the XML syntax. Since this is not a description of the basic XML syntax, it is not much introduced. If you are interested, I can write a special article on the syntax of XML to share with you.

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.