import java.io.File;
import java.io.*;
import org.w3c.dom.Document;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
public class test
{
public test()
{
try
{
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
File file=new File("test.xml");
Document doc=db.parse(file);
NodeList products=doc.getElementsByTagName("product");
Element productElement;
for (int i = 0; i < products.getLength(); i++)
{
productElement=(Element)products.item(i);
String id=productElement.getAttribute("id");
System.out.println("产品编号: "+id);
String name=doc.getElementsByTagName("name").item(i).getFirstChild().getNodeValue();
System.out.println("产品名称: "+name);
String price=doc.getElementsByTagName("price").item(i).getFirstChild().getNodeValue();
System.out.println("产品价格: "+price);
System.out.println("--------------------------------------");
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
new test();
}
}
给位大虾 能帮我解释一下这段代码吗? 我在网上找的 我很不理解
[code="java"]import java.io.File;
import java.io.*;
import org.w3c.dom.Document;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
public class test
{
public test()
{
try
{
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance(); //获取一个DocumentBuilderFactory的实例
DocumentBuilder db=dbf.newDocumentBuilder(); //使用工厂生成一个DocumentBuilder
File file=new File("test.xml"); //打开文件,获得句柄
Document doc=db.parse(file); //使用dom解析xml文件
NodeList products=doc.getElementsByTagName("product"); //将所有节点名为product的节点取出
Element productElement; //元素对象,声明
for (int i = 0; i < products.getLength(); i++) //循环处理对象
{
productElement=(Element)products.item(i); //将每个Product赋值给元素
String id=productElement.getAttribute("id"); //取得元素的属性“id” ……取得这样的a
System.out.println("产品编号: "+id);
String name=doc.getElementsByTagName("name").item(i).getFirstChild().getNodeValue(); //取得第i个节点的第一个字节点,取得节点中的值book取得这样的book
System.out.println("产品名称: "+name);
String price=doc.getElementsByTagName("price").item(i).getFirstChild().getNodeValue();
System.out.println("产品价格: "+price);
System.out.println("--------------------------------------");
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
new test();
}
} [/code]
学习Dom的时候最好把要解析的文件打开看,这样对比看很清晰。记得要解析的xml文件格式搞的规整一些。
[size=medium]楼主看看这篇文章吧,写的很详细:[/size]
[url]http://download.zdnet.com.cn/software_zone/2007/0919/514369.shtml[/url]
//使用DocumentBuilderFactory类的静态方法newInstance(),来获取该类的一个对象dbf
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
//使用dbf对象的newDocumentBuilder()方法,来获取DocumentBuilder类的对象
DocumentBuilder db=dbf.newDocumentBuilder();
//创建File的对象file,用来在内存中加载XML文件
File file=new File("test.xml");
/使用DocumentBuilder类对象的parse()方法,来解析XML文件,并返回Document对象
Document doc=db.parse(file);
//获得其子元素名为"product"的一个列表list
NodeList products=doc.getElementsByTagName("product");
Element productElement;
for (int i = 0; i < products.getLength(); i++)
{
productElement=(Element)products.item(i);
String id=productElement.getAttribute("id");
System.out.println("产品编号: "+id);
String name=doc.getElementsByTagName("name").item(i).getFirstChild().getNodeValue();
System.out.println("产品名称: "+name);
String price=doc.getElementsByTagName("price").item(i).getFirstChild().getNodeValue();
System.out.println("产品价格: "+price);
System.out.println("--------------------------------------");
}
}
//获取文档生成对象的工厂方法
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
//读取xml文件
File file=new File("test.xml");
//将xml文件解析成DOM的document对象
Document doc=db.parse(file);
//获得document对象的所有product子元素
NodeList products=doc.getElementsByTagName("product");
Element productElement;
//遍历所有product元素
for (int i = 0; i < products.getLength(); i++)
{
productElement=(Element)products.item(i);
//获得元素的id属性
String id=productElement.getAttribute("id");
System.out.println("产品编号: "+id);
//获得该元素下面的所有name子元素的第一个子元素的节点值
String name=doc.getElementsByTagName("name").item(i).getFirstChild().getNodeValue();
System.out.println("产品名称: "+name);
String price=doc.getElementsByTagName("price").item(i).getFirstChild().getNodeValue();
System.out.println("产品价格: "+price);
System.out.println("--------------------------------------");
}
这个说得超详细