Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use DOM4J in Java to generate xml file and parse xml file

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you the "Java how to use DOM4J to generate xml files and parse xml files", the content is easy to understand, well-organized, hope to help you solve your doubts, the following let Xiaobian lead you to study and learn "how to use DOM4J in Java to generate xml files and parse xml files" this article.

Prepare to rely on dom4j dom4j 1.6.1 to generate xml files to generate a standard presentation

Generate xml code

/ * * use DOM4J to generate xml method * / public static void createXml () {try {/ / create document object Document document = DocumentHelper.createDocument (); / / create root node bookRoot Element StudentRoot = document.addElement ("StudentRoot") / / add the first node Element book1 = StudentRoot.addElement ("student") to the root node; / / add the attribute book1.addAttribute ("id", "1") to the child node; / / add the child node Element name = book1.addElement ("name") to the node / / assign name.setText ("Xiao Qiao") to the child node; Element price = book1.addElement ("age"); price.setText ("18"); / / add a second node Element book2 = StudentRoot.addElement ("student") to the root node Book2.addAttribute ("id", "2"). AddElement ("name"). SetText ("Bridge"); book2.addElement ("age"). SetText ("20"); / / add a third node to the root node Element book3 = StudentRoot.addElement ("student"); book3.addAttribute ("id", "3"). AddElement ("name"). SetText ("Sun ze") Book3.addElement ("age") .setText ("21"); / / set the format for generating xml OutputFormat of = OutputFormat.createPrettyPrint (); / / set the encoding format of.setEncoding ("UTF-8"); / / generate the xml file File file = new File ("E:\\ student.xml") If (file.exists ()) {file.delete ();} / create a xml document editor XMLWriter writer = new XMLWriter (new FileOutputStream (file), of); / / put the document you just created into the document editor writer.write (document); writer.close () } catch (Exception e) {e.printStackTrace ();}} parse the xml file

The file that parses the xml is just exported. We show two cases. One is that we put the parsed xml file data into the entity class if we know the attribute name and the child element name. Print what you don't know directly to the console.

Entity class display

Import lombok.Data;@Datapublic class Student {private int id; private String name; private int age;}

The method of parsing xml files

Public static void analysis () {/ / change the xml to be parsed into a file file File file = new File ("E:\\ student.xml"); / / get the parser object SAXReader reader = new SAXReader (); / / parse the file into a document tree Document document = null; try {document = reader.read (file) } catch (DocumentException e) {e.printStackTrace ();} / / get the root node Element studentRoot = document.getRootElement (); / / get all the nodes in the root node List elements = studentRoot.elements (); / / store the data set of the nodes in the xml List list = new ArrayList () / / facilitate all nodes for (Element child: elements) {Student student = new Student (); / / student.setId (Integer.parseInt (child.attributeValue ("id")) when the attribute name is known; / / get the data of the attribute / / student.setName (child.elementText ("name") when the child element name is known) / / get the element median value student.setAge (Integer.parseInt (child.elementText ("age"); / / get the element median value list.add (student); / / List attributes = child.attributes () in the case of unknown attribute name For (Attribute attribute: attributes) {System.out.println (attribute.getName () + "-->" + attribute.getValue ()); List elementList = child.elements () when the child element name is unknown For (Element ele: elementList) {System.out.println (ele.getName () + "-->" + ele.getText ());}} / / print the data set parsed from xml to list.forEach (x-> System.out.println (x));}

Analysis result display

The above is all the contents of the article "how to use DOM4J in Java to generate xml files and parse xml files". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report