采用DOM方式解析xml

老样子,javabean实体类:
public class Book implements Serializable {
	private int id;
	private String name;
	private double price;


主要看这里:
public class DomParseService {
	public List<Book> getBooks(InputStream inputStream)
			throws ParserConfigurationException, SAXException, IOException {
		List<Book> bookList = new ArrayList<Book>();

		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		Document document = builder.parse(inputStream);
		Element documentElement = document.getDocumentElement();
		System.out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		// 递归解析xml并进行原样输出
		parseAll(documentElement);
		System.out.println("\n");
		// System.out.println(documentElement.getChildNodes().getLength());
		NodeList bookNodeList = document.getElementsByTagName("book");
		for (int i = 0; i < bookNodeList.getLength(); i++) {
			Element element = (Element) bookNodeList.item(i);
			// System.out.println(element.getChildNodes().getLength());
			// System.out.println(node.getNodeType()+":"+node.getNodeName()+":"+node.getNodeValue());
			Book book = new Book();
			book.setId(Integer.valueOf(element.getAttribute("id")));
			NodeList bookChildNodes = element.getChildNodes();
			for (int j = 0; j < bookChildNodes.getLength(); j++) {
				Node node = bookChildNodes.item(j);
				if (node.getNodeType() == Node.ELEMENT_NODE) {
					Element bookChildNode = (Element) node;
					if ("name".equals(bookChildNode.getNodeName())) {
						book.setName(bookChildNode.getFirstChild()
								.getNodeValue());
					} else if ("price".equals(bookChildNode.getNodeName())) {
						book.setPrice(Double.valueOf(bookChildNode
								.getFirstChild().getNodeValue()));
					}
				}

			}
			bookList.add(book);
		}

		// NodeList nodeList=documentElement.getChildNodes();
		// for(int j=0;j<nodeList.getLength();j++){
		// Node node=nodeList.item(j);
		// System.out.println(node.getNodeType()+":"+node.getNodeName()+":"+node.getNodeValue());
		// }
		return bookList;
	}

	public void parseAll(Element element) {
		String tagName = element.getNodeName();
		System.out.print("<" + tagName);

		NamedNodeMap attrMap = element.getAttributes();
		if (attrMap != null && attrMap.getLength() > 0) {
			for (int i = 0; i < attrMap.getLength(); i++) {
				Attr attr = (Attr) attrMap.item(i);
				String attrName = attr.getNodeName();
				String attrValue = attr.getNodeValue();
				System.out.print(" " + attrName + "=\"" + attrValue + "\"");
			}
		}
		System.out.print(">");

		NodeList childNodeList = element.getChildNodes();
		if (childNodeList != null && childNodeList.getLength() > 0) {
			for (int j = 0; j < childNodeList.getLength(); j++) {
				Node childNode = childNodeList.item(j);
				if (childNode.getNodeType() == Node.ELEMENT_NODE) {
					// 递归解析xml
					parseAll((Element) childNode);
				} else if (childNode.getNodeType() == Node.COMMENT_NODE) {
					System.out.print("<!--" + childNode.getNodeValue() + "-->");
				} else if (childNode.getNodeType() == Node.TEXT_NODE) {
					System.out.print(childNode.getNodeValue());
				}
			}
		}
		System.out.print("</" + tagName + ">");
	}
}


JUnit测试:
@Test
	public void test() throws ParserConfigurationException, SAXException, IOException {
		InputStream inputStream=this.getClass().getClassLoader().getResourceAsStream("xml.xml");
		DomParseService dps=new DomParseService();
		List<Book> bookList=dps.getBooks(inputStream);
		for(Book book:bookList){
			System.out.println(book);
		}
	}


结果输出如下:
<?xml version="1.0" encoding="UTF-8"?>
<books>
	<book id="12">
		<name>thinking in java</name>
		<price>85.5</price>
	</book>
	<book id="15">
		<name>Spring in Action</name>
		<price>39.0</price>
	</book>
</books>

12:thinking in java:85.5
15:Spring in Action:39.0



你可能感兴趣的:(xml,解析,dom,parse)