系统城装机大师 - 固镇县祥瑞电脑科技销售部宣传站!

当前位置:首页 > 脚本中心 > 其它 > 详细页面

ava全面解析string类型的xml字符串

时间:2022-03-01来源:www.pcxitongcheng.com作者:电脑系统城

目录
  • 解析string类型的xml字符串
    • 所需要的包自行导入
  • 解析String类型t复杂xml,多级节点,最好的例子
    • 字符串xml如下
    • 解析代码

解析string类型的xml字符串

我先拼接一个xml格式的字符串,模拟获取了这样的数据,然后再解析

所需要的包自行导入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
StringBuilder sb = new StringBuilder();
   sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
   sb.append("<root>");
   sb.append("    <header>");
   sb.append("        <mark>" + "this is a mark!"+ "</mark>");
   sb.append("    </header>");
   sb.append("    <body>");
   sb.append("        <data>");
   sb.append("             <first>");
   sb.append("              <second>" + "来解析我啊!"+ "</second>");
   sb.append("             </first>");
   sb.append("        </data>");
   sb.append("    </body>");
   sb.append("</root>");
//开始解析
   String str= sb.toString();
   Document doc = null;
   try {
       doc = DocumentHelper.parseText(str);
   } catch (DocumentException e) {
       e.printStackTrace();
   }
   Element root = doc.getRootElement();// 指向根节点  <root>
   try {
       Element mark=root.element("header").element("mark");
       Element second=root.element("body").element("data").element("first").element("second");
       //获取xml的节点内容
       System.out.println(mark.getTextTrim());
       System.out.println(second.getTextTrim());
   }catch(Exception e){
       e.printStackTrace();
   }

运行结果:

 

解析String类型t复杂xml,多级节点,最好的例子

需要用jar包 dom4j-1.6.1.jar

字符串xml如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<root>
    <flw>
    <name>aa</name>
    <age>22</age>
    <instance_info>
          <num>1</num>
           <code>0</code>
          <instance>
               <ip>10.1.1.2</ip>
               <mask>9999</mask>
         </instance>
         <instance>
               <ip>10.1.1.5</ip>
               <mask>717</mask>
         </instance>
    </instance_info>
     
    <instance_info>
          <num>2</num>
           <code>33</code>
          <instance>
               <ip>10.1.1.2</ip>
               <mask>9999</mask>
         </instance>
         <instance>
               <ip>10.1.1.9</ip>
               <mask>878</mask>
         </instance>
    </instance_info>
   </flw>
</root>

解析代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
public class XMLParser {
       public static void main(String[] args) {
            String xml = "<root><flw><name>aa</name><age>22</age><instance_info><num>1</num><code>0</code><instance><ip>10.1.1.2</ip><mask>9999</mask></instance><instance><ip>10.1.1.5</ip><mask>717</mask></instance></instance_info><instance_info><num>2</num><code>33</code><instance><ip>10.1.1.2</ip><mask>9999</mask></instance><instance><ip>10.1.1.9</ip><mask>878</mask></instance></instance_info></flw></root>";
      
            long start = System.currentTimeMillis();
      
            SAXReader reader = new SAXReader();
            try {
                org.dom4j.Document doc = reader.read(new ByteArrayInputStream(xml
                        .getBytes("UTF-8")));
                org.dom4j.Element root = doc.getRootElement();
                org.dom4j.Element element;
                org.dom4j.Element element2;
                org.dom4j.Element element3;
                for (Iterator i = root.elementIterator("flw"); i.hasNext();) {
                    element = (org.dom4j.Element) i.next();
                    System.out
                            .println("name:[" + element.elementText("name") + "]");
                    System.out.println("age:[" + element.elementText("age") + "]");
                    for (Iterator j = element.elementIterator("instance_info"); j
                            .hasNext();) {
                        element2 = (org.dom4j.Element) j.next();
                        System.out.println("\tnum:[" + element2.elementText("num")
                                + "]");
                        System.out.println("\tcode:["
                                + element2.elementText("code") + "]");
                        for (Iterator k = element2.elementIterator("instance"); k
                                .hasNext();) {
                            element3 = (org.dom4j.Element) k.next();
                            System.out.println("\t\tip:["
                                    + element3.elementText("ip") + "]");
                            System.out.println("\t\tmask:["
                                    + element3.elementText("mask") + "]");
                        }
                    }
                }
            } catch (DocumentException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            long end = System.currentTimeMillis();
            System.out.println("耗时:" + (end - start) + "ms");
        }
}

以上为个人经验,希望能给大家一个参考

分享到:

相关信息

  • Go json反序列化“null“的问题解决

    有这么一段代码,可以先看一下有没有什么问题,作用是输入一段json字符串,反序列化成map,然后将另一个inputMap的内容,merge进这个map 1 2 3 4 5 6 7 8 9 10 11 12 13 14...

    2023-03-15

  • Golang 字符串转time类型实现

    由于数据库的类型为Data 类型,所以插入数据库的时候我先把前端传入的string类型的时间转为Time 再插入。 Go 提供了两种插入的方式,即time.Parse 和 time.ParseInLocation 。两种方式,他们的差异比较大。...

    2023-03-09

系统教程栏目

栏目热门教程

人气教程排行

站长推荐

热门系统下载