import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class DoomTest {
public static void main(String[] args) throws Exception {
// (1)生成工厂类的对象
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// (2)生成解析器对象
DocumentBuilder db = factory.newDocumentBuilder();
// (3)对xml文档进行解析
Document doc = db.parse(new File("src/ab.xml"));
// (4)读取ab.xml文档中到底有几个节点?节点的名称、节点的类型以及节点的值
Node root = doc.getChildNodes().item(0);
findElementChildren(doc);
Node root1 = doc.getElementsByTagName("phone").item(0);
findChildren(doc);
findAttibutes(root);
findFixedChild(root);
}
public static void findChildren(Document doc) {
// 先通过文档对象doc得到根节点对象root
Node root = doc.getElementsByTagName("phone").item(0);
// 通过root对象得到根节点的所有儿子节点,返回一个节点的集合
NodeList list = root.getChildNodes();
for (int a = 0; a < list.getLength(); a++) {
Node n = list.item(a);
System.out.println(n.getNodeName() + "---" + n.getNodeType()
+ "---" + n.getNodeValue());
}
}
public static void findElementChildren(Document doc) {
// 先通过文档对象doc得到根节点对象root
Node root = doc.getElementsByTagName("phone").item(0);
// 通过root对象得到根节点的所有儿子节点,返回一个节点的集合
NodeList list = root.getChildNodes();
System.out.println("根节点下的所有子节点的个数:" + list.getLength());
int count = 0;
for (int i = 0; i < list.getLength(); i++) {
Node n = list.item(i);
if (n.getNodeType() == Node.ELEMENT_NODE) {
System.out.println(n.getNodeName() + "---" + n.getNodeType()
+ "---" + n.getNodeValue());
count++;
}
}
System.out.println("元素节点的总数为:" + count);
}
public static void findFixedChild(Node node) {
NodeList list = node.getChildNodes();
int count = 0;
for (int i = 0; i < list.getLength(); i++) {
boolean flag = false;
Node n1 = list.item(i);
if (n1.getNodeName().equals("phone")) {
flag = true;
count++;
}
if (flag && count == 2) {
NodeList list1 = n1.getChildNodes();
for (int j = 0; j < list1.getLength(); j++) {
Node n = list1.item(j);
System.out.println(n.getNodeName() + "---"
+ n.getNodeType() + "---" + n.getNodeValue());
}
}
}
}
public static void findAttibutes(Node node) {
NamedNodeMap map = node.getAttributes();
if (map != null) {
for (int i = 0; i < map.getLength(); i++) {
Node d = map.item(i);
System.out.println(d.getNodeName() + "---" + d.getNodeType()
+ "---" + d.getNodeValue());
}
}
}
}
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class DoomTest {
public static void main(String[] args) throws Exception {
// (1)生成工厂类的对象
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// (2)生成解析器对象
DocumentBuilder db = factory.newDocumentBuilder();
// (3)对xml文档进行解析
Document doc = db.parse(new File("src/ab.xml"));
// (4)读取ab.xml文档中到底有几个节点?节点的名称、节点的类型以及节点的值
Node root = doc.getChildNodes().item(0);
findElementChildren(doc);
Node root1 = doc.getElementsByTagName("phone").item(0);
findChildren(doc);
findAttibutes(root);
findFixedChild(root);
}
public static void findChildren(Document doc) {
// 先通过文档对象doc得到根节点对象root
Node root = doc.getElementsByTagName("phone").item(0);
// 通过root对象得到根节点的所有儿子节点,返回一个节点的集合
NodeList list = root.getChildNodes();
for (int a = 0; a < list.getLength(); a++) {
Node n = list.item(a);
System.out.println(n.getNodeName() + "---" + n.getNodeType()
+ "---" + n.getNodeValue());
}
}
public static void findElementChildren(Document doc) {
// 先通过文档对象doc得到根节点对象root
Node root = doc.getElementsByTagName("phone").item(0);
// 通过root对象得到根节点的所有儿子节点,返回一个节点的集合
NodeList list = root.getChildNodes();
System.out.println("根节点下的所有子节点的个数:" + list.getLength());
int count = 0;
for (int i = 0; i < list.getLength(); i++) {
Node n = list.item(i);
if (n.getNodeType() == Node.ELEMENT_NODE) {
System.out.println(n.getNodeName() + "---" + n.getNodeType()
+ "---" + n.getNodeValue());
count++;
}
}
System.out.println("元素节点的总数为:" + count);
}
public static void findFixedChild(Node node) {
NodeList list = node.getChildNodes();
int count = 0;
for (int i = 0; i < list.getLength(); i++) {
boolean flag = false;
Node n1 = list.item(i);
if (n1.getNodeName().equals("phone")) {
flag = true;
count++;
}
if (flag && count == 2) {
NodeList list1 = n1.getChildNodes();
for (int j = 0; j < list1.getLength(); j++) {
Node n = list1.item(j);
System.out.println(n.getNodeName() + "---"
+ n.getNodeType() + "---" + n.getNodeValue());
}
}
}
}
public static void findAttibutes(Node node) {
NamedNodeMap map = node.getAttributes();
if (map != null) {
for (int i = 0; i < map.getLength(); i++) {
Node d = map.item(i);
System.out.println(d.getNodeName() + "---" + d.getNodeType()
+ "---" + d.getNodeValue());
}
}
}
}
970

被折叠的 条评论
为什么被折叠?



