XML解析(DOM)
001 |
public class DOM_Parser { |
003 |
public static void main(String[] args) { |
005 |
String path = DOM_Parser. class .getClassLoader() |
006 |
.getResource( "books.xml" ).getPath(); |
009 |
Document doc = getDocumentByPath(path); |
013 |
Element root = doc.getDocumentElement(); |
015 |
BookVO vo = new BookVO(); |
017 |
vo.setName( "JAVA未眠者" ); |
019 |
createBookElement(doc, root, vo); |
023 |
iteratorElement2(root); |
025 |
} catch (ParserConfigurationException e) { |
027 |
} catch (SAXException e) { |
029 |
} catch (IOException e) { |
039 |
private static void iteratorElement(Element ele) { |
041 |
NodeList bookNodes = ele.getChildNodes(); |
043 |
for ( int i = 0 ; i < bookNodes.getLength(); i++) { |
044 |
Node bookNode = bookNodes.item(i); |
046 |
if (bookNode.getNodeType() == Node.ELEMENT_NODE) { |
047 |
Element childEle = (Element) bookNode; |
049 |
if (childEle.hasAttribute( "id" )) { |
050 |
System.out.print( "id = " + childEle.getAttribute( "id" ) |
053 |
iteratorElement(childEle); |
055 |
System.out.print(childEle.getNodeName() + " = " |
056 |
+ childEle.getTextContent() + " " ); |
058 |
if ( "author" .equals(childEle.getNodeName())) { |
059 |
System.out.println(); |
068 |
* 通过getElementsByTagName()方法遍历文档对象树中元素 |
072 |
private static void iteratorElement2(Element root) { |
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" ); |
080 |
for ( int i = 0 ; i < books.getLength(); i++) { |
081 |
Element bookEle = (Element)books.item(i); |
082 |
System.out.print( "id = " + bookEle.getAttribute( "id" )); |
084 |
Element nameEle = (Element)name.item(i); |
085 |
System.out.print( " name = " + nameEle.getTextContent()); |
086 |
System.out.println(); |
096 |
private static void createBookElement(Document doc, Element root, BookVO vo) { |
099 |
Element book = doc.createElement( "book" ); |
100 |
book.setAttribute( "id" , vo.getId()); |
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" ); |
110 |
book.appendChild(name); |
111 |
book.appendChild(price); |
112 |
book.appendChild(type); |
113 |
book.appendChild(author); |
115 |
root.appendChild(book); |
119 |
* 根据xml文档路径,获取解析xml文档对应的文档对象 |
123 |
* @throws ParserConfigurationException |
124 |
* @throws SAXException |
125 |
* @throws IOException |
127 |
private static Document getDocumentByPath(String path) |
128 |
throws ParserConfigurationException, SAXException, IOException { |
130 |
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); |
132 |
DocumentBuilder db = dbf.newDocumentBuilder(); |
135 |
Document doc = db.parse(path); |
原文地址:https://www.cnblogs.com/meimao5211/p/3209366.html