其他分享
首页 > 其他分享> > 使用Document解析xml

使用Document解析xml

作者:互联网

1、引入maven jar包
<dependency>
    <groupId>dom4j</groupId>
    <artifactId>dom4j</artifactId>
    <version>1.6.1</version>
    <scope>compile</scope>
</dependency>
2、使用document解析xml
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
/**
 * 使用Document形式解析xml
 */
public class Demo1 {
    public static void main(String[] args) {
        String str= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                "<msg>" +
                "    <head>" +
                "        <version>1.0</version>" +
                "        <ref>20190925090922TEST0000118165262</ref>" +
                "        <code>LD0096Q3</code>" +
                "        <dest>" +
                "            <code>ACGHGWIOM1UY</code>" +
                "            <uid>9100003648</uid>" +
                "            <uname>caij</uname>" +
                "            <uexpinf></uexpinf>" +
                "        </dest>" +
                "    </head>" +
                "    <body>" +
                "        <khyh>测试</khyh>" +
                "        <sqrq>20210914</sqrq>" +
                "        <jylx>8</jylx>" +
                "        <zcqx>36</zcqx>" +
                "        <qztsqx>0</qztsqx>" +
                "    </body>" +
                "</msg>";
        //开始解析
        try {
            Document doc = DocumentHelper.parseText(str);
            // 指向根节点<msg>
            Element root = doc.getRootElement();
       //获取<body>标签里面的<khyh>标签的值 Element mark=root.element("body").element("khyh"); //获取xml的节点内容 String text = mark.getTextTrim(); System.out.println(text); }catch(Exception e){ e.printStackTrace(); } } }

 

标签:xml,解析,String,dom4j,Element,import,Document
来源: https://www.cnblogs.com/zhangjinmei/p/15476282.html