demo01
package xml.dom;
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 {
// step 1:获得dom解析器工厂 (工厂的作用是用于创建具体的解析器)
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
// step 2:获得具体的dom解析器
DocumentBuilder db = dbf.newDocumentBuilder();
// step 3:解析一个xml文档,获得Document对象(跟节点)
Document document = db.parse(new File("file/PEOPLE.xml"));
NodeList list = document.getElementsByTagName("PERSON");
System.out.println(list.getLength());
for (int i = 0; i < list.getLength(); i++) {
Element element = (Element) list.item(i);
System.out.println("***********************************");
/* 会打印11,不是5:<PERSON>和<NAME>之间的空白符、<NAME>元素、</NAME>和<ADDRESS>之前的空白符、<ADDRESS>元素。。。。 */
System.out.println("element childNodes length:"
+ element.getChildNodes().getLength());
element.getElementsByTagName("NAME").item(0).getChildNodes()
.getLength();
String id = element.getAttribute("PERSONID");
System.out.println("id:" + id);
/*
* element.getElementsByTagName("NAME").item(0)后仍需获取下一个子节点:将内容文本也认为一个元素
*/
String content = element.getElementsByTagName("NAME").item(0)
.getFirstChild().getNodeValue();
System.out.println("name:" + content);
content = element.getElementsByTagName("ADDRESS").item(0)
.getFirstChild().getNodeValue();
System.out.println("address:" + content);
content = element.getElementsByTagName("TEL").item(0)
.getFirstChild().getNodeValue();
System.out.println("tel:" + content);
content = element.getElementsByTagName("FAX").item(0)
.getFirstChild().getNodeValue();
System.out.println("fax:" + content);
content = element.getElementsByTagName("EMAIL").item(0)
.getFirstChild().getNodeValue();
System.out.println("email:" + content);
}
}
}
package xml.dom;
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文档并且将其内容输出到命令行中
*
* @author user
*
*/
public class DomTest3 {
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());
// step 3:解析一个xml文档,获得Document对象(跟节点)
Document doc = db.parse(new File("file/student.xml"));
/* 获得根元素节点 */
Element root = doc.getDocumentElement();
System.out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
parseElement(root);
}
private static void parseElement(Element element) {
String tagName = element.getTagName();
NodeList children = element.getChildNodes();
System.out.print("<" + tagName);
/* element元素的所有属性 */
NamedNodeMap map = element.getAttributes();
if (map != null) {
for (int i = 0; i < map.getLength(); i++) {
Attr attr = (Attr) map.item(i);
String attrNane = attr.getName();
String attrValue = attr.getValue();
System.out.print(" " + attrNane + "=\"" + attrValue + "\"");
}
}
System.out.print(">");
for (int i = 0; i < children.getLength(); i++) {
Node node = children.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) {
/* 注释内容 */
Comment comment = (Comment) node;
System.out.print("<!-- " + comment.getData() + " -->");
}
}
System.out.print("</" + tagName + ">");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<学生名册>
<学生 学号="1">
<!-- is comment -->
<姓名>张三</姓名>
<性别>男</性别>
<年龄>20</年龄>
</学生>
<学生 学号="2">
<姓名>李四</姓名>
<性别>女</性别>
<年龄>19</年龄>
</学生>
<学生 学号="3">
<姓名>赵六</姓名>
<性别>男</性别>
<年龄>21</年龄>
</学生>
</学生名册>