There are four common ways in which XML is parsed in Java: sax,dom,jdom,dom4j
When parsing an XML document, the SAX parser triggers a series of events that enable the application to access the XML document through an event handler function. Because the event trigger itself is sequential, the SAX parser provides a sequential access mechanism to the XML document, which can no longer be processed back to the parsed part. Sax is called a "simple" application interface because the SAX parser does only a few simple tasks, and most of the work is done by the application itself. That is, when the SAX parser is implemented, it simply checks the stream of bytes in the XML document sequentially, determines which part of the XML syntax the current byte is, checks whether it conforms to the XML syntax, and triggers the corresponding event. For the event handler itself, it should be implemented by the application itself. The SAX parser lacks the flexibility to handle XML documents compared to the DOM parser, but the SAX parser is more efficient for applications that only need to access the data in an XML document without making changes to the document. Because the SAX parser is simple and has low memory requirements, it has a wide range of application value as well as high efficiency.
By parsing the XML document, the DOM parser stores the entire XML document in memory as a DOM tree, and the application can access and manipulate any part of the DOM tree at any time, meaning that the application can randomly access the XML document through the DOM tree. This access gives the application a great flexibility to control the content of the entire XML document in any way. However, because the DOM parser transforms the entire XML document into a DOM tree in memory, the need for memory is higher when the XML document is large or the document structure is more complex. Also, the traversal of a complex tree is a more time-consuming operation. Therefore, the DOM parser has higher requirements for machine performance and is not very efficient to achieve. However, because the idea of the DOM parser's tree structure is consistent with the structure of the XML document, random access is possible through the DOM tree mechanism. Therefore, Dom parser also has a relatively wide use value.
Jdom is a pure Java API that handles XML. Using a specific class without an interface, you need to generate an instance of most node types, as long as one or two of the elements are passed in. is currently a good performance of the Java API Processing XML.
Sax
Pros: ① eliminates the need to load entire documents into memory, thus consuming less memory
② push model allows multiple ContentHandler to be registered
Cons: ① no built-in document navigation support
② not be able to access XML documents randomly
③ does not support modifying XML in situ
④ does not support namespace scopes
Best for: Applications that read data only from XML (not available for manipulating or modifying XML documents)
Dom
Pros: ① easy to use
② Rich collection of APIs that can be used for easy navigation
③ entire tree loaded into memory, allowing random access to XML documents
Cons: ① the entire XML document must be parsed at once
② loading the whole tree into higher memory costs
③ generic DOM nodes are not ideal for object type bindings that must be created for all nodes
Best for: Applications or XSLT applications that need to modify XML documents (applications that are not available for read-only XML)
Jdom
Pros: ① is a tree-based Java API that handles XML and loads the tree in memory
② has no backward-compatible limits and is therefore simpler than DOM
③ speed, low defect
④ Java rules with sax
Disadvantage: ① cannot handle documents larger than memory
②jdom represents the XML document Logical model. There is no guarantee that each byte really transforms.
③ does not provide any actual model of DTDs and schemas for instance documents.
④ does not support the corresponding traversal of the package with the DOM
Best for: Jdom has the convenience of a tree, there are also Java rules for Sax. Use when balance is required
dom4j
An intelligent branch of ①jdom that incorporates a number of functions beyond the basic XML document representation.
②DOM4J is an excellent Java XML API using interface and abstract basic methods
The ③ features excellent performance, flexibility, power and extreme ease of use.
④ is a software that develops source code
--A brief description of the DOM4J parsing process
1. Create object Saxreader Saxreader = new Saxreader ();
2. Load XML file Document Doudocument = Saxreader.read (xmlfile);
3. Get the XML file with node Element rootelement = Doudocument.getrootelement ();
4. Get all child nodes under the root node list<element> elements = Rootelement.elements ();
5. Traversal.
6. Get Node properties list<attribute> attrs = Element.attributes ();
Each property name/property value is then traversed.
7. Get the child node collection list<element> childelements = Element.elements ();
The values in the final node can then be traversed.
DOM4J Parsing xml