首頁 > 軟體

Java全面解析string型別的xml字串

2022-03-01 16:00:39

解析string型別的xml字串

我先拼接一個xml格式的字串,模擬獲取了這樣的資料,然後再解析

所需要的包自行匯入

	 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如下

<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>

解析程式碼

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("ttip:["
                                    + element3.elementText("ip") + "]");
                            System.out.println("ttmask:["
                                    + element3.elementText("mask") + "]");
                        }
                    }
                }
            } catch (DocumentException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            long end = System.currentTimeMillis();
            System.out.println("耗時:" + (end - start) + "ms");
        }
}

以上為個人經驗,希望能給大家一個參考,也希望大家多多支援it145.com。


IT145.com E-mail:sddin#qq.com