获取根元素
一旦解析了文档并创建了一个 Document
,应用程序就能单步调试该结构以审核、查找或显示信息。这种导航功能是将要在 Document
上执行的许多操作的基础。
对文档的单步调试首先从根元素开始。格式良好的文档仅有一个根元素,也称为 DocumentElement
。应用程序首先检索这个元素。
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class OrderProcessor {
...
System.exit(1);
}
//STEP 1: Get the root element
Element root = doc.getDocumentElement();
System.out.println("The root element is " + root.getNodeName());
}
}
编译和运行这个应用程序将输出根元素的名称。
获取节点的孩子
一旦应用程序确定了根元素,它就把根元素的孩子的列表作为一个 NodeList
来检索。NodeList
类是一系列的项,应用程序将逐个迭代这些项。
...
import org.w3c.dom.NodeList;
...
//STEP 1: Get the root element
Element root = doc.getDocumentElement();
System.out.println("The root element is "+root.getNodeName());
//STEP 2: Get the children
NodeList children = root.getChildNodes();
System.out.println("There are "+children.getLength()
+" nodes in this document.");
}
}
使用 getFirstChild() 和 getNextSibling()
父子和兄弟关系提供了迭代某个节点的所有孩子的替代方法,它在某些场合下可能更为适宜,比如在这些关系和孩子
的出现顺序对理解数据至关重要的时候。
for 循环首先从根元素的第一个孩子开始。 应用程序迭代第一个孩子的所有兄弟,直至已全部对它们求值。
每次应用程序执行该循环,它都要检索一个
Node
对象,输出其名称和值。注意orders
的五个孩子包
括order
元素和三个文本节点。还要注意元素具有一个null
值,而不是预期的文本。包含实际内容作
为其值的,是作为元素的孩子的文本节点。... import org.w3c.dom.Node; ... //STEP 3: Step through the children for (Node child = root.getFirstChild(); child != null; child = child.getNextSibling()) { System.out.println(start.getNodeName()+" = " +start.getNodeValue()); } } } ...
在多层孩子中递归使用 getFirstChild() 和 getNextSibling() 中的代码显示了第一层孩子,但那远远不是整个文档
。为了看到所有元素,必须将前一个例子中的功能转换为一个方法并递归地调用。应用程序首先从根元素开始,向屏幕打印名称和值。然后应用程序就像以前一样遍历根元素的每个
孩子。但是对于每个孩子,应用程序还会遍历该孩子的每个孩子,即检查根元素的所有孩子和孙子。... public class OrderProcessor { private static void stepThrough (Node start) { System.out.println(start.getNodeName()+" = "+start.getNodeValue()); for (Node child = start.getFirstChild(); child != null; child = child.getNextSibling()) { stepThrough(child); } } public static void main (String args[]) { File docFile = new File("orders.xml"); ... System.out.println("There are "+children.getLength() +" nodes in this document."); //STEP 4: Recurse this functionality stepThrough(root); } }
包含属性
到目前为止,所编写的
stepThrough()
方法能够遍历大多数类型的节点,但是它完全地遗漏了属性
,因为属性不是任何节点的孩子。为了显示属性,可修改stepThrough()
来检查元素节点的属性。下面修改过的代码检查每个节点的输出,通过将节点的
nodeType
与常量ELEMENT_NODE
作比较
,从而确定它是否为一个元素。Node
对象带有成员常量,它们表示每种类型的节点,比如
ELEMENT_NODE
或ATTRIBUTE_NODE
。 如果nodeType
与ELEMENT_NODE
匹配,它就
是一个元素。对于找到的每个元素,应用程序都会创建一个包含该元素的所有属性的
NamedNodeMap
。应用程序能
够迭代NamedNodeMap
,打印每个属性的名称和值,就像它迭代NodeList
一样。... import org.w3c.dom.NamedNodeMap; ... private static void stepThroughAll (Node start) { System.out.println(start.getNodeName()+" = "+start.getNodeValue()); if (start.getNodeType() == start.ELEMENT_NODE) { NamedNodeMap startAttr = start.getAttributes(); for (int i = 0; i < startAttr.getLength(); i++) { Node attr = startAttr.item(i); System.out.println(" Attribute: "+ attr.getNodeName() +" = "+attr.getNodeValue()); } } for (Node child = start.getFirstChild(); child != null; child = child.getNextSibling()) { stepThroughAll(child); } }