XML解析(DOM)

001 public class DOM_Parser {
002  
003     public static void main(String[] args) {
004         try {
005             String path = DOM_Parser.class.getClassLoader()
006                 .getResource("books.xml").getPath();
007              
008             // 获取文档对象
009             Document doc = getDocumentByPath(path);
010  
011             // 遍历文档对象树(加载xml文档在内存中形成的文档对象树)
012             // 获取xml文档根元素
013             Element root = doc.getDocumentElement();
014  
015             BookVO vo = new BookVO();
016             vo.setId("004");
017             vo.setName("JAVA未眠者");
018             // 创建一个新的book元素
019             createBookElement(doc, root, vo);
020  
021             // 遍历元素
022 //          iteratorElement(root);
023             iteratorElement2(root);
024  
025         catch (ParserConfigurationException e) {
026             e.printStackTrace();
027         catch (SAXException e) {
028             e.printStackTrace();
029         catch (IOException e) {
030             e.printStackTrace();
031         }
032     }
033  
034     /**
035      * 遍历元素
036      *
037      * @param ele
038      */
039     private static void iteratorElement(Element ele) {
040         // 获取根元素下子元素(book元素)
041         NodeList bookNodes = ele.getChildNodes();
042  
043         for (int i = 0; i < bookNodes.getLength(); i++) {
044             Node bookNode = bookNodes.item(i);
045             // 判断当前节点是否为元素节点
046             if (bookNode.getNodeType() == Node.ELEMENT_NODE) {
047                 Element childEle = (Element) bookNode;
048                 // 根据元素上的属性名获取到对应的属性值
049                 if (childEle.hasAttribute("id")) {
050                     System.out.print("id = " + childEle.getAttribute("id")
051                             " ");
052                     // 遍历book元素下的子元素
053                     iteratorElement(childEle);
054                 else {
055                     System.out.print(childEle.getNodeName() + " = "
056                             + childEle.getTextContent() + " ");
057  
058                     if ("author".equals(childEle.getNodeName())) {
059                         System.out.println();
060                     }
061                 }
062  
063             }
064         }
065     }
066      
067     /**
068      * 通过getElementsByTagName()方法遍历文档对象树中元素
069      *
070      * @param root
071      */
072     private static void iteratorElement2(Element root) {
073          //获取所有book元素节点
074         NodeList books = root.getElementsByTagName("book");
075         NodeList name = root.getElementsByTagName("name");
076         NodeList type = root.getElementsByTagName("type");
077         NodeList price = root.getElementsByTagName("price");
078         NodeList author = root.getElementsByTagName("author");
079          
080         for (int i = 0; i < books.getLength(); i++) {
081             Element bookEle = (Element)books.item(i);
082             System.out.print("id = " + bookEle.getAttribute("id"));
083              
084             Element nameEle = (Element)name.item(i);
085             System.out.print("  name = " + nameEle.getTextContent());
086             System.out.println();
087         }
088     }
089  
090     /**
091      * 创建新的book元素对象
092      *
093      * @param doc
094      * @param root
095      */
096     private static void createBookElement(Document doc, Element root, BookVO vo) {
097         // 向文档对象树上添加新book子元素
098         // 创建新的book元素
099         Element book = doc.createElement("book");
100         book.setAttribute("id", vo.getId());
101  
102         // 创建book元素下的子元素
103         Element name = doc.createElement("name");
104         name.setTextContent(vo.getName());
105         Element price = doc.createElement("price");
106         Element type = doc.createElement("type");
107         Element author = doc.createElement("author");
108  
109         // 构建元素之间层次关系
110         book.appendChild(name);
111         book.appendChild(price);
112         book.appendChild(type);
113         book.appendChild(author);
114  
115         root.appendChild(book);
116     }
117  
118     /**
119      * 根据xml文档路径,获取解析xml文档对应的文档对象
120      *
121      * @param path
122      * @return
123      * @throws ParserConfigurationException
124      * @throws SAXException
125      * @throws IOException
126      */
127     private static Document getDocumentByPath(String path)
128             throws ParserConfigurationException, SAXException, IOException {
129         // 获取DOM解析器工厂对象
130         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
131         // 获取DOM解析器对象
132         DocumentBuilder db = dbf.newDocumentBuilder();
133  
134         // 加载要解析xml文档
135         Document doc = db.parse(path);
136         return doc;
137     }
138 }
原文地址:https://www.cnblogs.com/meimao5211/p/3209366.html