public class Student {
private String id;
private String name;
private Integer age;
private Double score;
}
/**
* 功能:解析students.xml
* 技能:使用DOM技术
*
* 认识DOM 1.基本思路:将整个文档在内存中形成一棵倒挂的树,每个节点Node都是一个对象
*
* 2.常用类 Node:抽象节点 Element: student name age score Attr : id Text : 李明 89 23
*
* 3.缺点
* 1.繁琐:前面三个步骤相同,也需要书写一遍。能否封装好
* 2.繁琐:getChildNodes()不仅包括Element----1,也包括空白形成的Text---3,遍历时需要进行筛选
* 3.繁琐:getChildNodes()也包括注释、也包括外部DTD引用,大部分情况下并不是用户需要的
* 解决:DOM4J
*
* 4. 如果文本中出现了特殊字符,必如<,& \,怎么处理
* 方法1:字符实体 < >
* 方法2:只显示不解析 <23 CDATA = Char Data
*
*
*/
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.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class TestDOM {
public static void main(String[] args) throws ParserConfigurationException,
SAXException, IOException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse("students.xml");
NodeList studentsList = doc.getElementsByTagName("students");
Node studentsNode = studentsList.item(0);
NodeList studentList = studentsNode.getChildNodes();
for (int i = 0; i < studentList.getLength(); i++) {
Node studentNode = studentList.item(i);
if (studentNode.getNodeType() == Node.ELEMENT_NODE) {
Element stuElem = (Element) studentNode;
String value = stuElem.getAttribute("id");
System.out.println("id----->" + value);
NodeList nasList = stuElem.getChildNodes();
for (int j = 0; j < nasList.getLength(); j++) {
Node nasNode = nasList.item(j);
if (nasNode.getNodeType() == Node.ELEMENT_NODE) {
Element nasElem = (Element) nasNode;
String name = nasElem.getNodeName();
String content = nasElem.getTextContent();
System.out.println(name + "----" + content);
}
}
System.out.println();
}
}
}
}
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.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class TestDom2 {
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse("students.xml");
NodeList rootList = doc.getElementsByTagName("students");
Node students = rootList.item(0);
NodeList childNodes = students.getChildNodes();
for(int i=0;i<childNodes.getLength();i++){
Node student = childNodes.item(i);
if(student.getNodeType()==Node.ELEMENT_NODE){
Element el = (Element)student;
String id = el.getAttribute("id");
System.out.println(id);
NodeList childNodes2 = el.getChildNodes();
for(int j=0;j<childNodes2.getLength();j++){
Node item = childNodes2.item(j);
if(item.getNodeType()==Node.ELEMENT_NODE){
Element el2 = (Element)item;
String name = el2.getNodeName();
String value = el2.getTextContent();
System.out.println(name+":"+value);
}
}
System.out.println("-------------------");
}
}
}
}