XML concepts in Java

Source: Internet
Author: User

1. XML Syntax Parsing Method

 

XML parsing is a theoretical knowledge, not only in Java, but also in other languages. There are currently three types:

A. Dom (Document Object Model): the DOM method reads all XML content into the memory at a time and generates a tree structure based on the node and attribute in the XML content. This is an object model. After that, we can operate on this tree structure. Dom features simple and clear programming. The disadvantage is to read all the content at a time.

B. Sax (Simple API for XML): Sax is an event-based parsing method that embeds its own code through callback functions. Define the callback function before parsing. After the parsing starts, the callback function is called once an event is generated. For example, a callback function is called once a subnode is found. Unlike Dom, Sax does not have clear parent-child and tree structure relationships. You must maintain the relationships between nodes in the callback function. Because sax does not need to maintain the tree, it does not need to read all XML at a time, but programming is more difficult than Dom.

C. Stax (streaming API for XML): Stax is a new XML parsing method. It is more like Sax and event-based, and does not need to read all the content at a time. Unlike sax, the sax event is push, while Stax is pull. Sax pushes events by letting the parser call the callback function, while Stax has an interface that actively removes the pull event. when calling the pull interface, the parser reads the next part of XML and generates event responses. The advantage of Stax over Sax is that it can take the initiative to control the parsing process, or even stop parsing in advance.

 

2. XML syntax parser

 

The above is all about theory, but only the parsing method. You still need to install the above method to parse XML. This is the XML parser.

 

The common syntax parser is xerces and crimson. The former was first developed by IBM, and the latter was developed by Sun, which is now open-source. Crimson is much more efficient than xerces. Therefore, xerces is basically used now.

 

3. JAXP (Java API for XML Processing)

 

Sun prefers reserved interfaces such as JDBC and script when doing Java. Because there are many XML syntax parsers, the interfaces are different. Sun defines the JAXP interface to simplify development and facilitate the replacement of the parser implementation. The XML Parser usually implements these interfaces to ensure that the parser implementation is changed without changing the code. How to replace a parser? The documentbuilderfactory and saxparserfactory classes of JAXP both have a newinstance method. They are similar content. Take sax as an example:

 

[Java]View plaincopy

  1. Return (documentbuilderfactory) factoryfinder. Find (
  2. /* The default property name according to the JAXP spec */
  3. "Javax. xml. parsers. documentbuilderfactory ",
  4. /* The fallback implementation class name */
  5. "Com.sun.org. Apache. xerces. Internal. JAXP. documentbuilderfactoryimpl ");

 

Two parameters are passed in. for comments, the first is an attribute name and the second is a class name. Factoryfinder. the function implemented by FIND is to first find the parameter in the Java program according to the attribute. If the parameter is found, the parser is constructed according to the class name specified in the parameter, if not, construct the parser according to the class name specified in the second parameter. Therefore, you only need to add the parameter-davax. xml. parsers. saxparserfactory = specified class name when starting the program to replace the parser.

 

4. DOM interface

 

According to the JAXP interface, the object parsed by Dom is an org. w3C. dom. document instance, it is found that W3C interfaces are very difficult to use, so the org. w3C. dom. the idea of document is that JDOM and dom4j are both the same purpose, but the interfaces are different. In the end, dom4j is more efficient than JDOM.

 

5. jaxb (Java architecture for XML binding)

 

Relational databases use or-mapping to convert data in tables and Java objects. XML can also be used to convert data into Java objects, which are called oxm (object XML mapping ).

Jaxb is an oxm specification and is also developed by Sun. Like JAXP, it also defines some interfaces for vendors to implement and some functional specifications. Like JAXP, jaxb also defines a method to pass the implemented class name. With the development of the times, it has two more methods than JAXP, so there are three methods in total.

A. Add a file jaxb. properties to the same package of the Java class to be mapping, and define the javax. xml. Bind. Context. Factory attribute in the file to specify the class name.

B. Like JAXP, add the startup parameter-djavax. xml. Bind. jaxbcontext to specify the class name.

C. Specify the implementation class name in the file by adding a file named javax. xml. Bind. jaxbcontext under the META-INF/services/directory.

Because of the amount of code, I will not post it. You can view the find method of the context source code. The third method is to abstract a new Java function in Java 1.6, called serviceregistry. You can view the API documentation for details.

 

Keywords: Java xml dom sax Stax xerces crimson jaxp jdom dom4j jaxb

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.