如何用JAVA实现简单的XML字符串解析代码示例?

2026-05-21 06:343阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计394个文字,预计阅读时间需要2分钟。

如何用JAVA实现简单的XML字符串解析代码示例?

plaintext导入dom4j包到依赖配置中,如下:dom4j groupId: dom4j artifactId: dom4j version: 1.6.1

例如,阿里云视频转码服务的回调解析,代码示例:import org.dom4j.Document;import org.dom4j.DocumentException;

引入 dom4j 包

如何用JAVA实现简单的XML字符串解析代码示例?

<dependency>   <groupId>dom4j</groupId>   <artifactId>dom4j</artifactId>   <version>1.6.1</version> </dependency>

比如阿里云视频转码服务的回调通知解析,代码如下:

import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; import java.util.Iterator; public class DOMParser { public static void main(String[] args) { String strXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <Notification xmlns=\"mns.aliyuncs.com/doc/v1/\"> <TopicOwner>1692545896541241</TopicOwner> <TopicName>MyTopic</TopicName> <Subscriber>1692545896541241</Subscriber> <SubscriptionName>bing-test3</SubscriptionName> <MessageId>C39FB8C345BBFBA8-1-1687F6FAADD-200000015</MessageId> <MessageMD5>CAA1E9F5E9F854ACD8297B100BF8CCF9</MessageMD5> <Message>{\"jobId\":\"2384a4d89b1d4f1e869559e2ff8c9fad\",\"requestId\":\"639D1D03-1557-4AD7-9AD7-691F02834516\",\"Type\":\"Transcode\",\"state\":\"Success\",\"type\":\"Transcode\",\"State\":\"Success\",\"JobId\":\"2384a4d89b1d4f1e869559e2ff8c9fad\",\"RequestId\":\"639D1D03-1557-4AD7-9AD7-691F02834516\"}</Message> <PublishTime>1548326251229</PublishTime> </Notification>"; Document doc = null; try { doc = DocumentHelper.parseText(strXML); } catch (DocumentException e) { e.printStackTrace(); } Element root = doc.getRootElement();// 指向根节点 Iterator it = root.elementIterator(); while (it.hasNext()) { Element element = (Element) it.next();// 一个Item节点 System.out.println(element.getName() + " : " + element.getTextTrim()); } } }

输出结果

TopicOwner : 1692545896541241 TopicName : MyTopic Subscriber : 1692545896541241 SubscriptionName : bing-test3 MessageId : C39FB8C345BBFBA8-1-1687F6FAADD-200000015 MessageMD5 : CAA1E9F5E9F854ACD8297B100BF8CCF9 Message : {"jobId":"2384a4d89b1d4f1e869559e2ff8c9fad","requestId":"639D1D03-1557-4AD7-9AD7-691F02834516","Type":"Transcode","state":"Success","type":"Transcode","State":"Success","JobId":"2384a4d89b1d4f1e869559e2ff8c9fad","RequestId":"639D1D03-1557-4AD7-9AD7-691F02834516"} PublishTime : 1548326251229

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

本文共计394个文字,预计阅读时间需要2分钟。

如何用JAVA实现简单的XML字符串解析代码示例?

plaintext导入dom4j包到依赖配置中,如下:dom4j groupId: dom4j artifactId: dom4j version: 1.6.1

例如,阿里云视频转码服务的回调解析,代码示例:import org.dom4j.Document;import org.dom4j.DocumentException;

引入 dom4j 包

如何用JAVA实现简单的XML字符串解析代码示例?

<dependency>   <groupId>dom4j</groupId>   <artifactId>dom4j</artifactId>   <version>1.6.1</version> </dependency>

比如阿里云视频转码服务的回调通知解析,代码如下:

import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; import java.util.Iterator; public class DOMParser { public static void main(String[] args) { String strXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <Notification xmlns=\"mns.aliyuncs.com/doc/v1/\"> <TopicOwner>1692545896541241</TopicOwner> <TopicName>MyTopic</TopicName> <Subscriber>1692545896541241</Subscriber> <SubscriptionName>bing-test3</SubscriptionName> <MessageId>C39FB8C345BBFBA8-1-1687F6FAADD-200000015</MessageId> <MessageMD5>CAA1E9F5E9F854ACD8297B100BF8CCF9</MessageMD5> <Message>{\"jobId\":\"2384a4d89b1d4f1e869559e2ff8c9fad\",\"requestId\":\"639D1D03-1557-4AD7-9AD7-691F02834516\",\"Type\":\"Transcode\",\"state\":\"Success\",\"type\":\"Transcode\",\"State\":\"Success\",\"JobId\":\"2384a4d89b1d4f1e869559e2ff8c9fad\",\"RequestId\":\"639D1D03-1557-4AD7-9AD7-691F02834516\"}</Message> <PublishTime>1548326251229</PublishTime> </Notification>"; Document doc = null; try { doc = DocumentHelper.parseText(strXML); } catch (DocumentException e) { e.printStackTrace(); } Element root = doc.getRootElement();// 指向根节点 Iterator it = root.elementIterator(); while (it.hasNext()) { Element element = (Element) it.next();// 一个Item节点 System.out.println(element.getName() + " : " + element.getTextTrim()); } } }

输出结果

TopicOwner : 1692545896541241 TopicName : MyTopic Subscriber : 1692545896541241 SubscriptionName : bing-test3 MessageId : C39FB8C345BBFBA8-1-1687F6FAADD-200000015 MessageMD5 : CAA1E9F5E9F854ACD8297B100BF8CCF9 Message : {"jobId":"2384a4d89b1d4f1e869559e2ff8c9fad","requestId":"639D1D03-1557-4AD7-9AD7-691F02834516","Type":"Transcode","state":"Success","type":"Transcode","State":"Success","JobId":"2384a4d89b1d4f1e869559e2ff8c9fad","RequestId":"639D1D03-1557-4AD7-9AD7-691F02834516"} PublishTime : 1548326251229

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。