有人知道怎么遍历 org.w3c.dom.Document 这个对象吗?

解决方案 »

  1.   

    咋遍历?取子节点?
    public NodeList getChildNodes()A NodeList that contains all children of this node. If there are no children, this is a NodeList containing no nodes. 要是网页上的话,就是document.childNodes
      

  2.   


            public static void main(String[] args) {
    Document d = null;
    if(d.hasChildNodes()) {
    parse(d);
    }
    }

    public static void parse(Node parent) {
    NodeList list = parent.getChildNodes();
    Node n;
    for(int i = 0; i < list.getLength(); i++) {
    n = list.item(i);
    if(n.hasChildNodes()) {
    parse(n);
    }
    }
    }
      

  3.   

    用DOM4J么?
    不明白你的意思  是解析xml么?
    给了例子不知道能不能解决你的问题 private static Document doc;
    private static SAXReader saxReader;
    private static Element element;


    private static File getFile(String filePath)
    {
    File file = new File(filePath);
    return file;
    }

    public Element getDocument(String filePath)
    {
    try {
    File file = getFile(filePath);
    saxReader = new SAXReader();
    doc = saxReader.read(file);
    element = doc.getRootElement();
    } catch (DocumentException e) {
    // TODO 自动生成 catch 块
    e.printStackTrace();
    }
    return element;
    }如此迭代 public DBConnection(String xPath)
    {
    MyDom4j dom4j = new MyDom4j();
    e = dom4j.getDocument(xPath);
    for(Iterator it = e.elementIterator("connection");it.hasNext();)
    {
    element = (Element)it.next();
    }
    this.url = element.elementText("url");
    this.driver = element.elementText("driver");
    this.password = element.elementText("pwd");
    this.user = element.elementText("user");
    }
      

  4.   

     Document doc = new SAXReader().read(new File(path + "\\daoContext.xml"));
            Element root = doc.getRootElement();
            List el =  root.elements();
            for (Iterator it = el.iterator();it.hasNext() ; )
            {
                Element em = (Element)it.next();
                String id = em.attributeValue("id");
                String impl = em.attributeValue("class");
                Class implClazz = Class.forName(impl);
                Object obj = implClazz.newInstance();
                daoMap.put(id , obj);            
            }