编程语言
首页 > 编程语言> > JAVA 读取xml格式的数据

JAVA 读取xml格式的数据

作者:互联网

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <column-enums>
 3     <type 
 4         name="third_ctrip_type">
 5         <value-label>
 6             <value>1</value>
 7             <label-key>enums.type.1</label-key>
 8             <bundle>third-ctrip</bundle>
 9         </value-label>
10         <value-label>
11             <value>2</value>
12             <label-key>enums.type.2</label-key>
13             <bundle>third-ctrip</bundle>
14         </value-label>
15     </type>
16     <type 
17         name="third_ctrip_poi">
18         <value-label>
19             <value>0</value>
20             <label-key>enums.poi.0</label-key>
21             <bundle>third-ctrip</bundle>
22         </value-label>
23         <value-label>
24             <value>3</value>
25             <label-key>enums.poi.3</label-key>
26             <bundle>third-ctrip</bundle>
27         </value-label>
28         <value-label>
29             <value>4</value>
30             <label-key>enums.poi.4</label-key>
31             <bundle>third-ctrip</bundle>
32         </value-label>
33         <value-label>
34             <value>5</value>
35             <label-key>enums.poi.5</label-key>
36             <bundle>third-ctrip</bundle>
37         </value-label>
38         <value-label>
39             <value>6</value>
40             <label-key>enums.poi.6</label-key>
41             <bundle>third-ctrip</bundle>
42         </value-label>
43         <value-label>
44             <value>9</value>
45             <label-key>enums.poi.9</label-key>
46             <bundle>third-ctrip</bundle>
47         </value-label>
48     </type>
49     <type 
50         name="third_ctrip_city_flag">
51         <value-label>
52             <value>0</value>
53             <label-key>enums.city_flag.0</label-key>
54             <bundle>third-ctrip</bundle>
55         </value-label>
56         <value-label>
57             <value>1</value>
58             <label-key>enums.city_flag.1</label-key>
59             <bundle>third-ctrip</bundle>
60         </value-label>
61         <value-label>
62             <value>2</value>
63             <label-key>enums.city_flag.2</label-key>
64             <bundle>third-ctrip</bundle>
65         </value-label>
66         <value-label>
67             <value>3</value>
68             <label-key>enums.city_flag.3</label-key>
69             <bundle>third-ctrip</bundle>
70         </value-label>
71     </type>
72 </column-enums>

java代码:

 1 /**
 2      * xml格式数据读取
 3      */
 4     @SuppressWarnings("unchecked")
 5     public static List getxmlString(String xmlString) throws Exception {
 6 
 7         String stringValueArr[] = null;
 8         List stringValueList = new ArrayList<>();
 9          Document document = DocumentHelper.parseText(xmlString);
10 
11         /*SAXReader reader = new SAXReader();
12         Document document = reader.read(new File("F:/strxml.xml"));*/
13         
14         // 3.获取根节点
15         Element rootElement = document.getRootElement();
16         Iterator iterator = rootElement.elementIterator();
17         while(iterator.hasNext()){
18             Element stu = (Element) iterator.next();
19             // System.out.println("asXML=" + stu.asXML());
20             List list2 = stu.elements();
21             for (int i = 0; i < list2.size(); i++) {
22                 Element stu2 = (Element) list2.get(i);
23                 // System.out.println("list2=" + stu2.asXML());
24 
25                 String stringValue = stu2.getStringValue();
26                 // System.out.println("stringValue:" + stringValue);
27                 stringValue = stringValue.replaceAll("\n",",").replaceAll(" ", "").replaceAll(",,,", ",");
28                 // System.out.println("stringValue2:" + stringValue);
29                 stringValueArr = stringValue.split(",");
30                 System.out.println("stringValueArr:" + stringValueArr);
31                 if (stringValueArr[1].equals("fd_37ca439b7921c6")) {
32                     stringValueList.add(stringValueArr[3]);
33                 }
34             }
35         }
36 
37         return stringValueList;
38 
39     }

标签:xml,JAVA,读取,third,ctrip,stringValueArr,println,stringValue,enums
来源: https://www.cnblogs.com/Sunshine-0416/p/12024681.html