In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail the example analysis of DOM in XML parsing. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
Introduction of 1.1.XML parsing technology for 1.XML programming
1.XML parsing is divided into dom parsing and sax parsing.
Dom: (Document Object Model, document object model), is a way of dealing with XML recommended by W3C.
Sax: (Simple API for XML), not the official standard, but it is the de facto standard of the XML community, and almost all XML parsers support it
SAX parsing uses an event-driven model to parse while reading: parsing line by line from top to bottom, parsing to an element, and calling the corresponding parsing method.
DOM allocates a tree structure in memory according to the XML hierarchy, encapsulating elements such as XML tags, attributes and text into node objects of the tree.
Different companies and organizations provide parsers for both DOM and SAX:
JAXP of Sun
Dom4j organized by Dom4j (most commonly used, such as hibernate)
Jdom of JDom Organization
JASP is part of J2SE, which provides DOM and SAX parsers for DOM and SAX, respectively.
Three kinds of parsing are also introduced here: dom, sax and dom4j.
1.2.JAXP introduction
Sun provides the Java API for XML Parsing (JAXP) interface to use SAX and DOM, and with JAXP, we can use any JAXP-compatible XML parser.
The JAXP development package is a part of J2SE, which consists of javax.xml, org.w3c.dom, org.xml.sax packages and their subpackages
In the javax.xml.parsers package, several factory classes are defined, and programmers call these factory classes to get DOM or SAX parser objects that parse the xml document.
DOM parsing 2.1.XML DOM Node Tree of 2.JAXP
First of all, it explains the principle of JAXP parsing the DOM object of XML. XML DOM regards the XML document as a node-tree, and all the nodes in the tree are related to each other. All nodes can be accessed through this tree. You can modify or delete their contents, or you can create new elements.
For example, the current XML document is as follows (this example is from the w3cschool online tutorial):
Harry Potter J K. Rowling 2005 29.99 Everyday Italian Giada De Laurentiis 2005 30.00 Learning XML Erik T. Ray 2003 39.95 XQuery Kick Start James McGovern Per Bothner Kurt Cagle James Linn Vaidyanathan Nagarajan 2003 49.99
The tree starts at the root node and grows branches to the text node at the lowest level of the tree:
[several knowledge points to know]:
1.dom treats the xml file as a tree and loads it into memory
2.dom is especially suitable for crud operations.
3.dom is not suitable for manipulating larger xml files (taking up memory)
4.dom maps every element, attribute, and text in the xml file to the corresponding Node object.
2.2. Get the DOM parser step in JAXP
1. Call the DocumentBuilderFactory.newInstance () method to get the factory that creates the DOM parser
two。 Call the newDocumentBuilder method of the factory object to get the DOM parser object
3. Call the parse () method of the DOM parser object to parse the XML document, get the Document object that represents the entire document, and manipulate the entire XML document using the DOM feature.
An example of DOM resolution of 2.3.JAXP:
The XML documentation is as follows:
Zhou Xiaoxing 23 studies hard Lin Xiao 25 is a good student 2.3.1. Read XML document
First, we use the three steps described in 2.2 to get the document object that represents the entire document, and call the read (Document document) method we wrote, as follows:
/ / 1. Create a DocumentBuilderFactoryDocumentBuilderFactory factory = DocumentBuilderFactory.newInstance (); / / 2. Get the DocumentBuilder object DocumentBuilder builder = factory.newDocumentBuilder (); / / 3 through the factory instance. Specify the xml file to parse and return the document object Document document = builder.parse (new File ("src/myClass.xml")); read (document)
The read method reads as follows:
/ * display all information about all students * @ param document * / public static void read (Document document) {/ / get NodeList NodeList nodeList = document.getElementsByTagName ("student") through the name of the student tag; for (int iStudent 0
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.