In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you how to use dom4j to recursively parse the xml of multiple nodes in the node. I hope you will get something after reading this article. Let's discuss it together.
Xml with multiple nodes in the dom4j recursive parsing node introduces some common uses of dom4j.
Introduction to dom4j:
Dom4j is a Java XML API, an upgrade of jdom, used to read and write XML files. Dom4j is a very excellent JavaXML API with excellent performance, powerful functions and extremely easy to use. Its performance exceeds the official dom technology of sun. At the same time, it is also an open source software, which can be found on SourceForge. You can also find an article on IBM developerWorks that evaluates the performance, functionality, and ease of use of mainstream Java XML API, so you can know that dom4j is excellent in every respect. Nowadays, we can see that more and more Java software are using dom4j to read and write XML. In particular, it is worth mentioning that even Sun's JAXM also uses dom4j. This is already a must-use jar package, and Hibernate also uses it to read and write configuration files.
The xml file here is (nested with two layers, built locally, and placed in any folder):
1348831860 here are the main methods and two parsing methods
Method description:
ParseXML (Element e); this parses a single, basic usage that contains dom4j.
Dom4j download: https://dom4j.github.io/
ParseMutiXML (Element e); this is to parse the XML with multiple nodes, adding a layer of judgment, that is, to determine whether the node still has child nodes, if not, print it directly, and on the contrary, re-recursively use the method to achieve the effect of re-parsing.
Package xaiver.cn;import java.io.File;import java.util.List;import org.dom4j.Document;import org.dom4j.Element;import org.dom4j.io.SAXReader;public class CopyOfParseXml {public static void main (String [] args) throws Exception {/ / here parses xml directly, using dom4j's package / / 1. There are three ways to read the file information: 1 to create a saxdom method, 2 to read according to the contents of the element, 3 to create a dom file to read SAXReader reader = new SAXReader (); Document document = reader.read ("E:\ WorkSpace\\ JavaSE\ Day01_HelloWorld\\ src\ xmlparse.xml"); Element element = document.getRootElement (); parseMutiXML (element) } @ SuppressWarnings ("unused") private static void parseXML (Element element) {/ / get the name test, the test result is xml, and you need to get the root node String name = element.getName (); System.out.println (name); / / the root node gets the root node @ SuppressWarnings ("unchecked") List list = element.elements () / / get the List and contents of the root node again, and use enhanced for loop for (Element e: list) {System.out.println (e.getName () + "= >" + e.getTextTrim ());} private static void parseMutiXML (Element root) {@ SuppressWarnings ("unchecked") List list = root.elements () If (list.size () = = 0) {System.out.println (root.getName () + "= >" + root.getTextTrim ());} else {for (Element element:list) {parseMutiXML (element);}
Execution result:
ToUserName== > toUser
FromUserName== > fromUser
CreateTime== > 1348831860
MsgType== > text
Content== > this is a test
ToUserName== > 1
ToUserName== > 2
ToUserName== > 3
FromUserName== > 4
Dom4j recursively parses all child nodes of XML string / * dom4j recursively parses all child nodes * * @ param childElements * @ param mapEle * @ return * / public Map getElementsToString (String print) {/ / parses the returned xml string to generate the document object Document document = null; Map mapEle = null Try {document = DocumentHelper.parseText (print); / / Root node Element root = document.getRootElement (); / / Child node List childElements = root.elements (); mapEle = new HashMap (); / / traversal child node mapEle = getAllElements (childElements,mapEle) } catch (DocumentException e) {/ / TODO Auto-generated catch block e.printStackTrace ();} return mapEle } / * dom4j recursively parses all child nodes * * @ param childElements * @ param mapEle * @ return * / public static Map getAllElements (List childElements,Map mapEle) {for (Element ele:childElements) {mapEle.put (ele.getName (), ele.getText ()) If (ele.elements (). Size () > 0) {mapEle = getAllElements (ele.elements (), mapEle);}} return mapEle } after reading this article, I believe you have a certain understanding of "how to use dom4j to recursively parse the xml of multiple nodes in a node". If you want to know more about it, please follow the industry information channel. Thank you for reading!
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.