xml解析

[code="xml"]<?xml version="1.0" encoding="UTF-8"?>






    <menu1 id="22" name="2"   hasChild="false"   url="" target="mainframe" >
    </menu1>
</menu>

[/code]
现有如上xml文件,读取xml文件,生成Document对象doc,请问n = doc.getFirstChild()指的是哪一部分,n.getFirstChild()又是指的哪一部分,在线求等答案,万分感谢!

package cn.com.wangxiuwei.test;

import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

public class TestXml {

/**
 * @param args
 * @throws ParserConfigurationException 
 * @throws IOException 
 * @throws SAXException 
 * @throws IOException 
 * @throws ClassNotFoundException 
 */
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException{

    DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
    DocumentBuilder db=dbf.newDocumentBuilder();
    Document d=db.parse("src/test.xml");
    System.out.println(d.getFirstChild().getNodeName());

    System.out.println(d.getFirstChild().getFirstChild().getNodeName());

}

}
打印的结果
root
#text

因为有空文本存在 所以第二个是#text

n = doc.getFirstChild() 整个menu对象
n.getFirstChild() 第一个menu1对象

[code="java"]import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**

  • @author hae
  • @version 创建时间:2014-7-24 下午3:31:13 * */ public class DomTest1 { public static void main(String[] args) throws Exception { // step 1: 获得dom解析器工厂(工作的作用是用于创建具体的解析器) DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

// System.out.println("class name: " + dbf.getClass().getName());

    // step 2:获得具体的dom解析器
    DocumentBuilder db = dbf.newDocumentBuilder();

// System.out.println("class name: " + db.getClass().getName());

    // step3: 解析一个xml文档,获得Document对象(根结点)
    Document document = db.parse(new File("d:\\test.xml"));

    Node n = document.getFirstChild();  // 
    System.out.println("doc.getFirstChild(): " + n);

    Node n1 = n.getFirstChild();
    System.out.println("n.getFirstChild(): " + n1);

    String nName = n.getFirstChild().getNodeName();
    System.out.println("n.getFirstChild().getNodeName(): " + nName);

    NodeList testList = document.getElementsByTagName("root");

    for(int i = 0; i < testList.getLength(); i++)
    {
        Element element = (Element)testList.item(i);

        System.out.println("document.getElementsByTagName(root): " + element.getFirstChild().getNodeName());

    }
}

}
[/code]

输出:
[code="java"]doc.getFirstChild(): [root: null]
n.getFirstChild(): [#text:
]
n.getFirstChild().getNodeName(): #text
document.getElementsByTagName(root): #text[/code]

[code="java"]
import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**

  • 描述: 注意事项: 实例:
  • @author mengjq
    */
    public class TestXML {

    public static void main(String[] args) {

    try {
        // step 1: 获得dom解析器工厂(工作的作用是用于创建具体的解析器)
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    
        // step 2:获得具体的dom解析器
        DocumentBuilder db = dbf.newDocumentBuilder();
    
        // step3: 解析一个xml文档,获得Document对象(根结点)
        Document document = db.parse(new File("F:/a.xml"));
    
        //表示第一个根节点(root)
        Node firstChild = document.getFirstChild();
    
        //根据文档节点获取TagName是Menu节点(<menu id="system" name="11">)
        NodeList childNodes = document.getElementsByTagName("menu");
    
        //便利menu节点
        for (int i = 0; i < childNodes.getLength(); i++) {
    
            //获取文档节点
            Element element = (Element)childNodes.item(i);
    
            //根据当前获取的文档节点获取meun节点的属性。属性对象如:(<menu id="system" name="11">)此处得到将是“id="system" name="11"”
            NamedNodeMap map = element.getAttributes();
    
            //如果该元素存在属性  
            if(null != map)  
            {  
                for(int j = 0; j < map.getLength(); j++)  
                {  
                    //获得该元素的每一个属性  
                    Attr attr = (Attr)map.item(j);  
    
                    String attrName = attr.getName();  
                    String attrValue = attr.getValue();  
    
                    System.out.print("attrName " + attrName + "=\"" + attrValue + "\"");  
                }  
            }  
    
        }
    
    }
    catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    catch (SAXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    

    }
    }

[/code]

楼主把你的xml文件改为如下形式:
[code="java"]<?xml version="1.0" encoding="UTF-8"?>

root-begin1
root-begin2

menu-begin1
menu-begin2







menu-end

root-end
[/code]

然后使用我下面的程序运行,输出如下:
[code="java"]doc.getFirstChild(): [root: null]
n.getFirstChild(): [#text:
root-begin1
root-begin2
]
n.getFirstChild().getNodeName(): #text
document.getElementsByTagName(root): system[/code]

因此,我认为:
1 doc.getFirstChild(): [root: null]

即为取得整个dom树

  1. n.getFirstChild(): [#text: root-begin1 root-begin2] 说明n.getFirstChild()取得的是root节点,要取得相应的节点内容/值,需要使用getElementsByTagName("tagName").

以上都是个人认为,如有错误,欢迎指正。

n = doc.getFirstChild() 是id=system的整个节点
n.getFirstChild()是id=1的整个节点