一聚教程网:一个值得你收藏的教程网站

最新下载

JAVA解析XML字符串简单方法代码示例

时间:2020-09-17 17:05:57 编辑:袖梨 来源:转载

本篇文章小编给大家分享一下JAVA解析XML字符串简单方法代码示例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。

引入 dom4j 包


  dom4j
  dom4j
  1.6.1

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

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 = "  1692545896541241 MyTopic 1692545896541241 bing-test3 C39FB8C345BBFBA8-1-1687F6FAADD-200000015 CAA1E9F5E9F854ACD8297B100BF8CCF9 {\"jobId\":\"2384a4d89b1d4f1e869559e2ff8c9fad\",\"requestId\":\"639D1D03-1557-4AD7-9AD7-691F02834516\",\"Type\":\"Transcode\",\"state\":\"Success\",\"type\":\"Transcode\",\"State\":\"Success\",\"JobId\":\"2384a4d89b1d4f1e869559e2ff8c9fad\",\"RequestId\":\"639D1D03-1557-4AD7-9AD7-691F02834516\"} 1548326251229 ";

    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

网站留言

热门栏目