【转】DOM 与 DOM 解析详解以DOM方式解析XML
java dom解析xml
package Test_DOM;import java.io.File;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.NamedNodeMap;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import org.xml.sax.SAXException;public class Test {public static void main(String[] args) throws ParserConfigurationException,SAXException, IOException {DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();DocumentBuilder builder = builderFactory.newDocumentBuilder();/* * builder.parse()方法将给定文件的内容解析为一个 XML 文档, 并且返回一个新的 DOM Document对象。 */Document document = builder.parse(new File("books.xml"));//打印document节点printNode(document,0);//获取文档的根元素,赋值给rootElement变量Element rootElement = document.getDocumentElement();//获取元素的count属性int countOfBooks = Integer.parseInt(rootElement.getAttribute("count"));System.out.println("There are "+countOfBooks+" books , they are ");//获取rootElement的所有子节点(不包括属性节点),返回一个NodeList对象NodeList childNodes = rootElement.getChildNodes();for(int i = 0;i < childNodes.getLength();i++){//获取childNodes的第i个节点Node childNode = childNodes.item(i);//判断childNode是不是一个元素节点,并且它的 nodeName 值为bookif(childNode.getNodeType() == Node.ELEMENT_NODE && childNode.getNodeName().equals("book")){//若是,则获取childNode的所有子节点(不包括属性节点),返回一个NodeList对象NodeList childNodes_2 = childNode.getChildNodes();for(int j = 0;j < childNodes_2.getLength();j++){//获取childNodes_2的第j个节点Node childNode_2 = childNodes_2.item(j);//判断childNode_2是不是一个元素节点,并且它的 nodeName 值为nameif(childNode_2.getNodeType() == Node.ELEMENT_NODE && childNode_2.getNodeName().equals("name")){//若是,则获取childNode_2的所有子节点(不包括属性节点),返回一个NodeList对象NodeList childNodes_3 = childNode_2.getChildNodes();for(int k = 0;k < childNodes_3.getLength();k++){//获取childNodes_3的第k个节点Node childNode_3 = childNodes_3.item(k);//判断childNodes_3是不是一个文本节点if(childNode_3.getNodeType() == Node.TEXT_NODE){//若是,则打印输出这个文本节点的nodeValueSystem.out.println(" <<"+childNode_3.getNodeValue()+">>");}}}}}}}/* * 打印 DOM 节点 * 输出格式为: * nodeType(nodeName,nodeValue) * "ATTRIBUTE"(attributeName=attributeValue) * ... * childNodeType[childNodeName,childNodeValue] * ... */public static void printNode(Node node,int count) {if (node != null) {String tmp = "";for(int i = 0 ; i < count ; i++) tmp += " ";//获取node节点的节点类型,赋值给nodeType变量int nodeType = node.getNodeType();switch (nodeType) {case Node.ATTRIBUTE_NODE: tmp += "ATTRIBUTE";break;case Node.CDATA_SECTION_NODE: tmp += "CDATA_SECTION";break;case Node.COMMENT_NODE:tmp += "COMMENT";break;case Node.DOCUMENT_FRAGMENT_NODE:tmp += "DOCUMENT_FRAGMENT";break;case Node.DOCUMENT_NODE:tmp += "DOCUMENT";break;case Node.DOCUMENT_TYPE_NODE:tmp += "DOCUMENT_TYPE";break;case Node.ELEMENT_NODE:tmp += "ELEMENT";break;case Node.ENTITY_NODE:tmp += "ENTITY";break;case Node.ENTITY_REFERENCE_NODE:tmp += "ENTITY_REFERENCE";break;case Node.NOTATION_NODE:tmp += "NOTATION";break;case Node.PROCESSING_INSTRUCTION_NODE:tmp += "PROCESSING_INSTRUCTION";break;case Node.TEXT_NODE:tmp += "TEXT";break;default:return;//invalid node type.}System.out.println(tmp+" ("+node.getNodeName()+","+node.getNodeValue()+")");/* * node.getAttributes()方法返回 * 包含node节点的属性的 NamedNodeMap(如果它是 Element) */NamedNodeMap attrs = node.getAttributes();if(attrs != null)for(int i = 0 ; i < attrs.getLength() ; i++){printNode(attrs.item(i),count+1);}/* * node.getChildNodes()方法返回 * 包含node节点的所有子节点的 NodeList。 */NodeList childNodes = node.getChildNodes();for(int i = 0 ; i < childNodes.getLength() ; i++){printNode(childNodes.item(i),count+1);}}}}
<?xml version="1.0" encoding="GB2312"?><books count="3"xmlns="http://test.org/books"><!--books's comment--><book id="1"><name>Thinking in JAVA</name></book><book id="2"><name>Core JAVA2</name></book><book id="3"><name>C++ primer</name></book></books>