package com.accp.xml.dom;import java.io.InputStream;import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;public class DOMTest { /**
 * @param args
 * @throws Exception
 */
public static void main(String[] args) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = factory.newDocumentBuilder(); InputStream is = Thread.currentThread().getContextClassLoader()
.getResourceAsStream("student.xml");
Document doc = documentBuilder.parse(is);//  NodeList nodes=doc.getChildNodes();
//
//  for(int i=0;i<nodes.getLength();i++){
//  Node node=nodes.item(i);
//  //System.out.println(node.getNodeName());
//  } Node root = doc.getFirstChild();  System.out.println(root.getNodeName()); NodeList nodes = root.getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
// System.out.println(node.getNodeName());
NodeList nodeChilds = node.getChildNodes();

for(int j=0;j<nodeChilds.getLength();j++){
Node nodeChild=nodeChilds.item(j);

if("name".equals(nodeChild.getNodeName())){
System.out.println(nodeChild.getNodeName());
System.out.println(nodeChild.getFirstChild().getNodeName());
System.out.println(nodeChild.getFirstChild().getNodeValue());
}

}
} }
}

解决方案 »

  1.   

    新学XML,
    有人帮我把main方法里面的代码给我注释一下好吗?
    最好每一句后面都注释下。
      

  2.   

    package test;import java.io.InputStream;import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;import org.w3c.dom.Document;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    //引入的是JDK解析XML的包
    //使用的是DOM方式,可以去看下另外一种SAX方式//另外推荐你看下Dom4j  JDom等第三方包public class DOMTest {/**
    * @param args
    * @throws Exception
    */
    public static void main(String[] args) throws Exception {

    //获取文档对象建造者工厂   【看看工厂模式】
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    //从工厂拿一个建造对象
    DocumentBuilder documentBuilder = factory.newDocumentBuilder();//读入student.xml   拿到输入流
    InputStream is = Thread.currentThread().getContextClassLoader()
    .getResourceAsStream("student.xml");
    //使用建造对象解析xml文件输入流,得到一个DOM对象【文档对象】
    Document doc = documentBuilder.parse(is);// NodeList nodes=doc.getChildNodes();
    //
    // for(int i=0;i<nodes.getLength();i++){
    // Node node=nodes.item(i);
    // //System.out.println(node.getNodeName());
    // }//拿到XML文档根节点
    Node root = doc.getFirstChild();
    //打印根节点标签
    System.out.println(root.getNodeName());
    //拿到根节点下所有下一级节点列表
    NodeList nodes = root.getChildNodes();//遍历第一级节点列表
    for (int i = 0; i < nodes.getLength(); i++) {
    Node node = nodes.item(i);
    // System.out.println(node.getNodeName());
    //拿到一级节点下面的所有二级节点
    NodeList nodeChilds = node.getChildNodes();
    //遍历二级节点
    for(int j=0;j<nodeChilds.getLength();j++){
    Node nodeChild=nodeChilds.item(j);if("name".equals(nodeChild.getNodeName())){ //如果节点标签为name,打印 标签名  第一个字节点标签名 第一个字节点节点值
    System.out.println(nodeChild.getNodeName());
    System.out.println(nodeChild.getFirstChild().getNodeName());
    System.out.println(nodeChild.getFirstChild().getNodeValue());
    }}
    }}
    }