实例一:
源码:
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.NodeList;
public class DOMTest1
{
public static void main(String[] args) throws Exception
{
//获得DOM解析工厂
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
//获得解析器
DocumentBuilder db=dbf.newDocumentBuilder();
//解析xml文件
Document document=db.parse(new File("person.xml"));
NodeList list=document.getElementsByTagName("person");
for(int i=0;i<list.getLength();i++){
Element element=(Element) list.item(i);
String str1=element.getElementsByTagName("name").item(0).getFirstChild().getNodeValue();
System.out.println("name:"+str1);
String str2=element.getElementsByTagName("age").item(0).getFirstChild().getNodeValue();
System.out.println("name:"+str2);
String str3=element.getElementsByTagName("address").item(0).getFirstChild().getNodeValue();
System.out.println("name:"+str3);
System.out.println("--------------------------");
}
}
}
person.xml
文件内容:
<?xml version="1.0"?>
<people>
<person>
<name>weiwei1</name>
<age>22</age>
<address>jiangsu1</address>
</person>
<person>
<name>weiwei2</name>
<age>23</age>
<address>jiangsu2</address>
</person>
<person>
<name>weiwei3</name>
<age>24</age>
<address>jiangsu3</address>
</person>
</people>
实例二:
源码:
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Attr;
import org.w3c.dom.Comment;
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;
/*
* 使用递归解析给定的任意一个xml文档,并且将其内容输出到命令行
*/
public class DOMTest2
{
public static void main(String[] args) throws Exception
{
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
Document document=db.parse(new File("student.xml"));
//获得根元素节点
Element root=document.getDocumentElement();
parseElement(root);
}
private static void parseElement(Element element){
//获得元素的名字
String tagName=element.getNodeName();
//获得这个结点的孩子
NodeList list=element.getChildNodes();
System.out.print("<"+tagName);
//获得这个结点的所有的属性
NamedNodeMap map=element.getAttributes();
if(null!=map){
for(int i=0;i<map.getLength();i++){
Attr attr=(Attr) map.item(i);
String name=attr.getName();
String value=attr.getValue();
System.out.print(" "+name+"=\""+value+"\"");
}
}
System.out.print(">");
for(int i=0;i<list.getLength();i++){
Node node=list.item(i);
//获得结点类型
short nodeType=node.getNodeType();
//是元素
if(nodeType==Node.ELEMENT_NODE){
//是元素,所以继续递归
parseElement((Element) node);
}// 是文本
else if(nodeType==Node.TEXT_NODE){
//递归出口
System.out.print(node.getNodeValue());
}//是注释
else if(nodeType==Node.COMMENT_NODE){
System.out.print("<!--");
Comment comment=(Comment)node;
String str=comment.getData();
System.out.print(str+"-->");
}
}
System.out.println("</"+tagName+">");
}
}
student.xml文件内容:
<?xml version="1.0" encoding="gbk" standalone="yes"?>
<学生名册>
<!--This is a student information-->
<学生 学号="1">
<姓名>李斯</姓名>
<性别>男</性别>
<年龄>22</年龄>
</学生>
<学生 学号="2">
<姓名>王五</姓名>
<性别>女</性别>
<年龄>21</年龄>
</学生>
<学生 学号="3">
<姓名>张三</姓名>
<性别>男</性别>
<年龄>20</年龄>
</学生>
</学生名册>