Sample code for XML parsing DOM4J parsing
Foreword: The company's APP has been around for a long time. The previous interface results were processed through XML, and in the project, everyone processes XML in different ways. There are various methods, and there is no unified processing method, so it is very troublesome in application. Therefore, every time the poster is developing a project, in order to save his own time, he does not study other people's XML parsing methods. As long as he encounters XML, he will Parse using DOM4J.
There are many ways to parse XML, such as DOM, SAX, JDOM, etc. As for the usage and principles, I won’t go into details here (ps: the original poster doesn’t know the usage and principles either). This article mainly talks about the simple operation and usage of DOM4J.
DOM4J Introduction
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 Company. It is also an open source software and can be found on SourceForge.
Dom4j is an easy-to-use, open source library for XML, XPath and XSLT. It is applied to the Java platform, adopts the Java collection framework and fully supports DOM, SAX and JAXP.
Here is a simple example to introduce the usage of DOM4J.
Note: To use DOM4J to parse XML, you need to introduce the DOM4J jar package into the project
XML file
<Response T='203' T1='6' TaskID='20130800001963' MediaNum='3' Result = '1' Desc='查询成功!' > <Media Name='IMG_20130425_141838.jpg' Mediasource ='1' Type ='1' Code='/9j/4AAQSkZJRgABAQA0'>图片1</Media> <Media Name='IMG_20130425_141838.jpg' Mediasource ='2' Type ='1' Code='/9j/4AAQSkZJRgABAQA0'>图片2</Media> <Media Name='IMG_20130425_141838.jpg' Mediasource ='3' Type ='1' Code='/9j/4AAQSkZJRgABAQA0'>图片3</Media> </Response>
Detailed explanation of DOM4J usage
Step 1: Load the xml file
Loading xml can be divided into two main methods
1. Directly load the path address of the file
2. Load xml in the form of string (This method is mainly used in server return results)
1.1. Directly load the file path
SAXReader reader = new SAXReader(); Document document = null; try { document = reader.read(new File("E://CZBK//day01//caseUp.xml")); } catch (DocumentException e) { e.printStackTrace(); }
1.2. Load xml in string form
SAXReader reader = new SAXReader(); Document document = null; try { //result是需要解析的字符串 //解析字符串需要转换成流的形式,可以指定转换字符编码 document = reader.read(new ByteArrayInputStream(result.getBytes("UTF-8"))); } catch (DocumentException e) { e.printStackTrace(); }
Steps 2: Parse XML
Before parsing XML, let’s first introduce the structural name of XML. Knowing the following four questions is very helpful for parsing XML
What is Node? What is an element? What is attribute(attribute)? What is a text value?
Nodes: "Response", "Media" are called nodes
Element: It ends with a complete tag and is called an element, including the entire element content. For example:
Attribute: Attribute value of the node , add a description of the node content. For example: T='203' T1='6' TaskID='20130800001963' MediaNum='3' Result = '1' Desc='Query successful!'
Text value: "Picture 1" is called Text value.
In the project, it is nothing more than operating around elements, attributes and text values, so if you master the value methods of these three parts, you will also master XML parsing.
2.1. Get the root node
//获取整个文档 Element rootElement = document.getRootElement();
rootElement contains the content of the entire xml document, that is, all the content contained in the Response tag
2.2. Get the attribute value of the Response node
//获取Response节点的Result属性值 String responseResult = rootElement.attributeValue("Result");
2.3. Get the Media element
//获取第一个Media元素 Element mediaElement = rootElement.element("Media"); //获取所有的Media元素 List allMeidaElements = rootElement.elements("Media");
2.4. Get the Media attribute value
//获取第一个Media元素的Name属性值 String mediaName = mediaElement.attributeValue("Name"); //遍历所有的Media元素的Name属性值 for (int i = 0; i < allMeidaElements.size(); i++) { Element element = (Element) allMeidaElements.get(i); String name = element.attributeValue("Name"); }
2.5. Get the text value of the Media tag
//获取第一个Meida元素的文本值 String value = mediaElement.getText();
Complete code
import java.io.File; import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; public class Textxml { public void xml() { SAXReader reader = new SAXReader(); Document document = null; try { document = reader.read(new File("E://CZBK//day01//caseUp.xml")); } catch (DocumentException e) { e.printStackTrace(); } //获取整个文档 Element rootElement = document.getRootElement(); System.out.println("整个文档:"+rootElement.asXML()); //获取Response节点的Result属性值 String responseResult = rootElement.attributeValue("Result"); System.out.println("Response节点的Result属性值:"+responseResult); //获取第一个Media元素 Element mediaElement = rootElement.element("Media"); System.out.println("第一个Media元素:"+mediaElement.asXML()); //获取所有的Media元素 List allMeidaElements = rootElement.elements("Media"); //获取第一个Media元素的Name属性值 String mediaName = mediaElement.attributeValue("Name"); System.out.println("第一个Media元素的Name属性值:"+mediaName); //遍历所有的Media元素的Name属性值 for (int i = 0; i < allMeidaElements.size(); i++) { Element element = (Element) allMeidaElements.get(i); String name = element.attributeValue("Name"); } //获取第一个Meida元素的文本值 String value = mediaElement.getText(); System.out.println("第一个Meida元素的文本值:"+value); } public static void main(String[] args) { Textxml textxml = new Textxml(); textxml.xml(); } }
Run results
整个文档:<Response T="203" T1="6" TaskID="20130800001963" MediaNum="3" Result="1" Desc="查询成功!"> <Media Name="IMG_20130425_141838.jpg" Mediasource="1" Type="1" Code="/9j/4AAQSkZJRgABAQA0">图片1</Media> <Media Name="IMG_20130425_141838.jpg" Mediasource="2" Type="1" Code="/9j/4AAQSkZJRgABAQA0">图片2</Media> <Media Name="IMG_20130425_141838.jpg" Mediasource="3" Type="1" Code="/9j/4AAQSkZJRgABAQA0">图片3</Media> </Response> Response节点的Result属性值:1 第一个Media元素:<Media Name="IMG_20130425_141838.jpg" Mediasource="1" Type="1" Code="/9j/4AAQSkZJRgABAQA0">图片1</Media> 第一个Media元素的Name属性值:IMG_20130425_141838.jpg 第一个Meida元素的文本值:图片1
Postscript
1. There are many XML parsing methods, and not all of them must be mastered. A kind of analysis is enough. As for the difference in performance, the main body of the building will not show it, and it cannot answer this question. 2. There are many APIs for DOM4J. This article only introduces the most basic ones. The most commonly used ones, if you are interested, you can research and use them yourself
The above is the detailed content of Sample code for XML parsing DOM4J parsing. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Can XML files be opened with PPT? XML, Extensible Markup Language (Extensible Markup Language), is a universal markup language that is widely used in data exchange and data storage. Compared with HTML, XML is more flexible and can define its own tags and data structures, making the storage and exchange of data more convenient and unified. PPT, or PowerPoint, is a software developed by Microsoft for creating presentations. It provides a comprehensive way of

Using Python to merge and deduplicate XML data XML (eXtensibleMarkupLanguage) is a markup language used to store and transmit data. When processing XML data, sometimes we need to merge multiple XML files into one, or remove duplicate data. This article will introduce how to use Python to implement XML data merging and deduplication, and give corresponding code examples. 1. XML data merging When we have multiple XML files, we need to merge them

Convert XML data in Python to CSV format XML (ExtensibleMarkupLanguage) is an extensible markup language commonly used for data storage and transmission. CSV (CommaSeparatedValues) is a comma-delimited text file format commonly used for data import and export. When processing data, sometimes it is necessary to convert XML data to CSV format for easy analysis and processing. Python is a powerful

Implementing filtering and sorting of XML data using Python Introduction: XML is a commonly used data exchange format that stores data in the form of tags and attributes. When processing XML data, we often need to filter and sort the data. Python provides many useful tools and libraries to process XML data. This article will introduce how to use Python to filter and sort XML data. Reading the XML file Before we begin, we need to read the XML file. Python has many XML processing libraries,

Importing XML data into the database using PHP Introduction: During development, we often need to import external data into the database for further processing and analysis. As a commonly used data exchange format, XML is often used to store and transmit structured data. This article will introduce how to use PHP to import XML data into a database. Step 1: Parse the XML file First, we need to parse the XML file and extract the required data. PHP provides several ways to parse XML, the most commonly used of which is using Simple

Python implements conversion between XML and JSON Introduction: In the daily development process, we often need to convert data between different formats. XML and JSON are common data exchange formats. In Python, we can use various libraries to convert between XML and JSON. This article will introduce several commonly used methods, with code examples. 1. To convert XML to JSON in Python, we can use the xml.etree.ElementTree module

Handling Errors and Exceptions in XML Using Python XML is a commonly used data format used to store and represent structured data. When we use Python to process XML, sometimes we may encounter some errors and exceptions. In this article, I will introduce how to use Python to handle errors and exceptions in XML, and provide some sample code for reference. Use try-except statement to catch XML parsing errors When we use Python to parse XML, sometimes we may encounter some

Python parses special characters and escape sequences in XML XML (eXtensibleMarkupLanguage) is a commonly used data exchange format used to transfer and store data between different systems. When processing XML files, you often encounter situations that contain special characters and escape sequences, which may cause parsing errors or misinterpretation of the data. Therefore, when parsing XML files using Python, we need to understand how to handle these special characters and escape sequences. 1. Special characters and
