dom4j难道不能遍历出来吗,不会吧
jdom都有的功能,不断升级的dom4j会没有?

解决方案 »

  1.   

    递归就可以了http://faq.csdn.net/read/204669.html
      

  2.   

    http://topic.csdn.net/t/20041202/10/3608010.html
      

  3.   

    两位给的都是同样内容的链接,里面的方法不对啊,element.node(i).attributes根本就没有。我也是想用递归来取得标签名,属性,值等等,相当于我对XML里面的一切东西(标签名,属性,值等等)一概不知道,通过递归把所有东西都解析出来
      

  4.   

    这次是我自己写的,code真累package com;import java.io.StringReader;
    import java.net.MalformedURLException;
    import java.util.Iterator;import org.dom4j.Attribute;
    import org.dom4j.Document;
    import org.dom4j.DocumentException;
    import org.dom4j.Element;
    import org.dom4j.Node;
    import org.dom4j.io.SAXReader;public class Test1 {
    public static Document read() throws MalformedURLException,
    DocumentException {
    StringBuffer str = new StringBuffer(
    "<?xml version=\"1.0\" encoding=\"GBK\" ?>");
    str.append("<root>");
    str.append("<a name='aa' value='1'>");
    str.append("<b name='bb' value='2'>");
    str.append("<c name='cc' value='3'>");
    str.append("<d name='dd' value='4'>");
    str.append("</d>");
    str.append("</c>");
    str.append("</b>");
    str.append("</a><e></e>");
    str.append("</root>");
    StringReader sr = new StringReader(str.toString());
    SAXReader reader = new SAXReader();
    Document document = reader.read(sr);
    return document;
    } public static void searchAtt(Element e) {
    for (Iterator i = e.attributeIterator(); i.hasNext();) {
    Attribute attribute = (Attribute) i.next();
    System.out.println(attribute.getName()+" "+attribute.getValue());
    }
    } public static void searchEle(Element e) {
          for (int i = 0, size = e.nodeCount(); i < size; i++)     {
               Node node = e.node(i);
               if (node instanceof Element) {
                Element nd=(Element) node;
                System.out.println(nd.getName());
                searchAtt(nd);
                searchEle((Element) node);
               }
           } } public static void main(String[] args) throws Exception, DocumentException {
    Document doc = read();
    searchEle(doc.getRootElement());
    }}