dom4j的讀操作

參考資料:http://blog.csdn.net/qking93415981/archive/2007/09/14/1785114.aspx
public class ReadXMLByDom {
    public static void main(String [] args) throws DocumentException{
        String str = "<diffgr><books><book id='1'><Name>Java</Name><price>100</price></book>";
        str = str + "<book id='2'><Name>Linux</Name><price>100</price></book></books></diffgr>";
        //獲取Document
        Document document = DocumentHelper.parseText(str);
        //查找需要遍歷的节点
        List list = document.selectNodes("/diffgr/books/book " );
        //遍历节点
        Iterator it = list.iterator();
        while(it.hasNext())
        {
            Element ftpElement = (Element)it.next();
            //取得該節點屬性值
            System.out.println("id屬性:"+ftpElement.attributeValue( " id " );)
            //取得需要的節點的值
            System.out.println("name="+ftpElement.element("Name").getText());
            System.out.println("price="+ftpElement.element("price").getText());
        }
    }
}
這樣,需要取什麽值,都可以指定。很方便。
這是dom4j最簡單的應用了

需要遍歷整個XML,可以參考如下
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.*;

public class TestSAX {
    //   private static Map<String, String> xmlmap = new HashMap<String,
    // String>();

    private static List<Leaf> elemList = new ArrayList<Leaf>();

    private static String srcXml =            
"<diffgr:diffgram xmlns:msdata=\"urn:schemas-microsoft-com:xml-msdata\" xmlns:diffgr=\"urn:schemas-microsoft-com:xml-diffgram-v1\">" +
            "<NewDataSet xmlns=\"\">" +
            "<Table diffgr:id=\"Table1\" msdata:rowOrder=\"0\">" +
            "<id>125704</id>" +
            "<l_name>&#x6211;&#x6688;</l_name>" +
            "<m1_number>8613574286112</m1_number>" +
            "<local_number>861052231235</local_number>" +
            "<m2_name>&#x6D77;&#xFF0C;&#x5C0F;&#x525B;</m2_name>" +
            "</Table>" +
            "<Table diffgr:id=\"Table2\" msdata:rowOrder=\"1\">" +
            "<id>124849</id><l_name>&#x6211;&#x6688;</l_name>" +
            "<m1_number>8613574286112</m1_number><local_number>" +
            "862151923508</local_number>" +
            "<m2_name>jerry217(Internet)</m2_name>" +
            "</Table>" +
            "<Table diffgr:id=\"Table6\" msdata:rowOrder=\"1\">" +
            "<id>124849</id>" +
            "<l_name>&#x6211;&#x6688;</l_name>" +
            "<m1_number>8613574286112</m1_number>" +
            "<local_number>862151923508</local_number>" +
            "<m2_name>jerry217(Internet)</m2_name><" +
            "/Table>" +
            "</NewDataSet>" +
            "</diffgr:diffgram>";

    public static void main(String args[]) throws DocumentException {
        TestSAX test = new TestSAX();
        Element root = test.getRootElement();
        test.getElementList(root);
        String x = test.getListString(elemList);
        System.out.println("-----------原xml内容------------");
        System.out.println(srcXml);
        System.out.println("-----------解析结果------------");
        System.out.println(x);         
    }

    /**
     * 获取根元素
     */
    public Element getRootElement() throws DocumentException {
        Document srcdoc = DocumentHelper.parseText(srcXml);
        Element elem = srcdoc.getRootElement();
        return elem;
    }

    /**
     * 递归遍历方法
     */
    public void getElementList(Element element) {
        List elements = element.elements();
        if (elements.size() == 0) {
            // 没有子元素
            System.out.println(element.getUniquePath());  //如果有相同屬性,會在屬性結尾加上“[數字]”進行標示
            String xpath = element.getPath();
            String value = element.getTextTrim();
            elemList.add(new Leaf(xpath, value));   //Leaf 僅僅是
        } else {
            // 有子元素
            for (Iterator it = elements.iterator(); it.hasNext();) {
                Element elem = (Element) it.next();
                // 递归遍历
                getElementList(elem);
            }
        }
    }

    public String getListString(List<Leaf> elemList) {
        StringBuffer sb = new StringBuffer();
        for (Iterator<Leaf> it = elemList.iterator(); it.hasNext();) {
            Leaf leaf = it.next();
            sb.append(leaf.getXpath()).append(" = ").append(leaf.getValue())
                    .append("\n");
        }
        return sb.toString();
    }
}

/**
 * xml节点数据结构
 */
class Leaf {
    private String xpath;
    private String value;
    public Leaf(String xpath, String value) {
        this.xpath = xpath;
        this.value = value;
    }
    public String getXpath() {
        return xpath;
    }
    public void setXpath(String xpath) {
        this.xpath = xpath;
    }
    public String getValue() {
        return value;
    }
    public void setValue(String value) {
        this.value = value;
    }
}

你可能感兴趣的:(数据结构,.net,xml,linux,Microsoft)