1. 程式人生 > >java用SAX遞迴獲取XML中的資料

java用SAX遞迴獲取XML中的資料

java中解析XML的方法有很多種,今天學了下用SAX來解析xml。

所需jar包:jdom.jar

xml:

<xml id='id'>
    <ToUserName>toUser</ToUserName>
    <FromUserName>fromUser</FromUserName>
    <CreateTime>12345678</CreateTime>
    "
    + "
    <MsgType>image</MsgType>
    <Image>
        <MediaId>media_id</MediaId>
    </Image>
</xml>
解析xml的java程式碼:
package com.linb.xml;

import java.io.IOException;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

public class XmlTest {
    public static void main(String[] args) {
        run();
    }

    public static void run() {
        // xml字串
        String xmlString = "<xml id='id'><ToUserName>toUser</ToUserName><FromUserName>fromUser</FromUserName><CreateTime>12345678</CreateTime>"
                + "<MsgType>image</MsgType><Image><MediaId>media_id</MediaId></Image></xml>";
        // 建立SAXBuilder
        SAXBuilder saxBuilder = new SAXBuilder();
        // 儲存xml資料到map
        Map<String, String> map = new HashMap<String, String>();
        try {
            // 通過xml字串建立Document
            Document doc = saxBuilder.build(new StringReader(xmlString));
            // 根元素
            Element root = doc.getRootElement();
            map.put("root.name", root.getName());
            // 遞迴獲取xml的資料
            dfs(root, map, root.getName());
        } catch(JDOMException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch(IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //遍歷map輸出資料
        for(Map.Entry<String, String>entry:map.entrySet()){
            System.out.println(entry.getKey()+":"+entry.getValue());
        }
    }

    public static void dfs(Element e, Map<String, String> map, String fatherName) {
        // 獲取節點e的所有屬性
        if(e.getAttributes().size() > 0) {
            Iterator<Attribute> it = e.getAttributes().iterator();
            while(it.hasNext()) {
                Attribute attribute = it.next();
                String name = attribute.getName();
                String value = e.getAttributeValue(name);
                map.put(fatherName + "->" + name, value);
            }
        }
        List children = e.getChildren();
        Iterator<Element> it = children.iterator();
        while(it.hasNext()) {
            Element child = it.next();
            String name = fatherName + "." + child.getName();
            // 如果有子節點就遞迴
            if(child.getChildren().size() > 0) {
                dfs(child, map, name);
            } else {
                // 沒有子節點,獲取節點的文字
                map.put(name, child.getText());
                // 獲取這個節點的屬性值
                if(child.getAttributes().size() > 0) {
                    Iterator<Attribute> childIt = child.getAttributes()
                            .iterator();
                    while(childIt.hasNext()) {
                        Attribute attribute = childIt.next();
                        String attrName = attribute.getName();
                        String attrValue = child
                                .getAttributeValue(attrName);
                        map.put(name + "->" + attrName, attrValue);
                    }
                }
            }
        }
    }
}