java用SAX递归获取XML中的数据

java中解析XML的方法有很多种,今天学了下用SAX来解析xml。

所需jar包:jdom.jar

xml:


    toUser
    fromUser
    12345678
    "
    + "
    image
    
        media_id
    


解析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 = "toUserfromUser12345678"
                + "imagemedia_id";
        // 创建SAXBuilder
        SAXBuilder saxBuilder = new SAXBuilder();
        // 保存xml数据到map
        Map map = new HashMap();
        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.Entryentry:map.entrySet()){
            System.out.println(entry.getKey()+":"+entry.getValue());
        }
    }

    public static void dfs(Element e, Map map, String fatherName) {
        // 获取节点e的所有属性
        if(e.getAttributes().size() > 0) {
            Iterator 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 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 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);
                    }
                }
            }
        }
    }
}


你可能感兴趣的:(java)