Home  >  Article  >  Backend Development  >  What are the four common parsing methods in xml?

What are the four common parsing methods in xml?

青灯夜游
青灯夜游Original
2019-04-03 15:52:1124531browse

Xml parsing methods include: 1. DOM parsing method, which can modify xml documents; 2. SAX parsing method, which has fast parsing speed and takes up less memory; 3. JDOM parsing method, which is easy to search; 4. DOM4J parsing method, parsing XML quickly.

What are the four common parsing methods in xml?

There are many ways to parse XML, but four parsing methods are the most commonly used, namely DOM method, SAX method, JDOM method, and DOM4J method. .

Introduction to XML

##XML is an extensible markup language that can define semantic tags (tags) and is a meta-markup language. XML is not like Hypertext Markup Language HTML, which can only use specified tags. For XML, users can define the tags they need. tree model.

XML documents organize data in the form of hierarchical tags and are mostly used for configuration files, storing static data, and exchanging data.

XML syntax

1. Each XML document starts with an XML preamble. The first line in the previous code is the XML preamble,

< ;?xml version="1.0"?>

2. Any start tag must have an end tag.

3. Tags must be nested in the appropriate order, so the end tag must match the start tag in mirror order.

4. If the label has attributes, the attribute values ​​must be enclosed in double quotes.

Four ways to parse XML files

1. DOM parsing method

DOM, Document Object Model is the officially recommended standard. DOM is the programming interface specification for html and xml documents, and is independent of platform and language. Using DOM specifications, it is possible to realize mutual conversion between DOM documents and xml, and to traverse and operate the contents of the corresponding DOM documents. The core of the DOM specification is the tree model, which is parsed after all is read.

The principle is: first create a Document object in the memory, and then read the XML document and assign it to the DOM object. Since the DOM object is based on a tree structure, just traverse the DOM object. You can query, modify, and delete DOM objects in memory, and you can also write back the original XML document to save the modifications.

Advantages: Since the entire tree is in memory, the xml document can be accessed randomly; the xml document can be modified

Disadvantages: The entire document must be parsed at one time; since the entire document needs to be loaded into memory, the cost is high for large documents

2. SAX parsing method

SAX, Simple Application Programming Interface (Simple Api For Xml). Standards that are not officially provided by W3C are developed by a community of programmers. SAX is conceptually completely different from DOM. It is not document driven, it is event driven. Event-driven: A program running method based on callback mechanism. Analyze layer by layer from outside to inside.

Advantages: The parsing speed is fast, and it takes up less memory. It loads and parses what data it needs.

Disadvantages: It does not record the relationship of tags, but requires the application to handle it by itself, which will increase the burden on the program.

3. JDOM parsing method

JDOM is a combination of Java and DOM. JDOM is committed to building a complete Java-based platform that can access, manipulate and output XML data through Java code. JDOM is a new API function that uses Java language to read, write, and operate XML. Simple, efficient and optimized.

Advantages: Easy to search and can be modified

Disadvantages: Loading the entire document requires high memory capacity

4. DOM4J parsing method.

dom4j is a Java XML API, similar to jdom, used to read and write XML files. Excellent performance, powerful functions, easy to use and open source code. It is currently the most popular and best-used XML parsing tool, and it parses XML the fastest.

Operation steps:

1: Create SAXReader: SAXReader reader = new SAXReader();

2: Create a file input stream and open the xml file: InputStream in = new FileInputStream("XXX. xml");
3: Read the xml file into memory through the reader and input stream to create a Document object: Document dom = reader.read(in);
4: Get the root node: Element root=dom.getRootElement( );
5: Get the list of child nodes: List childNodes = root.elements();
6: Traverse the child nodes: Element node = childNodes.get(i);
7: Read Node information:
1), node attribute value: node.attributeValue("attribute name");
2), node name: node.getName();
3), node value: node.getValue();
4), child node text value: node.elementText("child node name")

Related video tutorial recommendation: "

XML Tutorial"

The above is the entire content of this article, I hope it will be helpful to everyone's study. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of What are the four common parsing methods in xml?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:What is XPathNext article:What is XPath