DOM方式解析xml实例2
老样子,javabean实体类:
import java.io.*;
public class Book implements Serializable {
private int id;
private String name;
private double price; public Book(){}
public Book(int id,String name,double price){
this.id=id;
this.name=name;
this.price=price;
}
public void setId(int id){
this.id=id;
}
public void setName(String name){
this.name=name;
}
public void setPrice(double price){
this.price=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
DOM方式解析xml实例2的更多相关文章
- 用JAXP的dom方式解析XML文件
用JAXP的dom方式解析XML文件,实现增删改查操作 dom方式解析XML原理 XML文件 <?xml version="1.0" encoding="UTF-8 ...
- Dom方式解析XML
public class TestXML { public static void main(String[] args) throws SAXException, IOException { //D ...
- 用DOM方式解析XML
一.用DOM方式解析XML 此例子节点结构如下: 1.获取book节点属性 (1).如果不知道节点的属性,通过 NamedNodeMap attrs = book.getAttributes(); 来 ...
- 在iOS 开发中用GDataXML(DOM方式)解析xml文件
因为GDataXML的内部实现是通过DOM方式解析的,而在iOS 开发中用DOM方式解析xml文件,这个时候我们需要开启DOM,因为ios 开发中是不会自动开启的,只有在mac 开发中才自动开启的.我 ...
- Java-Web DOM方式解析xml文件
XML DOM 树形结构: DOM 节点 根据 DOM,XML 文档中的每个成分都是一个节点. DOM 是这样规定的: 整个文档是一个文档节点 每个 XML 元素是一个元素节点 包含在 XML 元素中 ...
- Java&Xml教程(二)使用DOM方式解析XML文件
DOM XML 解析方式是最容易理解的,它將XML文件作为Document对象读取到内存中,然后你可以轻松地遍历不同的元素和节点对象.遍历元素和节点不需要按照顺序进行. DOM解析方式适合尺寸较小的X ...
- Java眼中的XML--文件读取--1 应用DOM方式解析XML
初次邂逅XML: 需要解析的XML文件: 这里有两个book子节点. 1.如何进行XML文件解析前的准备工作,另外解析先获取book节点. 这个我后来看懂了: 这个Node的ELEMENT_NODE= ...
- DOM方式解析XML文件实例
books.XML文件: <?xml version="1.0" encoding="utf-8"?><bookstore> &l ...
- Java DOM方式解析XML(模板)
//创建一个DocumentBuilderFactory工厂实例 DocumentBuilderFactory DBF=DocumentBuilderFactory.newInstance(); // ...
随机推荐
- 前端开发利器webStorm 3.0配置使用
安装了phpstorm之后,想配置svn,结果在file->settings->Version Contorl->subversion->with conmand line ...
- Backspace doesn't delete inner html tags of a contenteditable DIV in Firefox
https://bugzilla.mozilla.org/show_bug.cgi?id=439808 backspace键 在ff下不能使用 div contenteditable=true时
- 大数据学习——hive基本操作
1 建表 create table student(id int,name string ,age int) row format delimitedfields terminated by ','; ...
- Boolean.valueOf("true")的用法
Boolean.valueOf(a);a为true时返回true不管大小写,a为其他值时都返回false:
- 【Dijstra堆优化】HDU 3986 Harry Potter and the Final Battle
http://acm.hdu.edu.cn/showproblem.php?pid=3986 [题意] 给定一个有重边的无向图,T=20,n<=1000,m<=5000 删去一条边,使得1 ...
- windows7 下安装使用Redis
Redis 安装使用 本地环境:Windows7 64位web环境:wamp集成环境,php版本:PHP Version 7.1.17 学习参考网站: RUNOOB.COM官网 http://www ...
- Linux虚拟机fdisk分区
以下操作全部基于win7 64位系统上的Linux虚拟机(CentOS6.6). 当Linux虚拟机的硬盘空间不够用时,可以手动添加硬盘块,流程如下: 右键虚拟机,点击“Add”按钮: 选择“Hard ...
- 动态规划: HDU 1789Doing Homework again
Problem Description Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of h ...
- Baby Step Giant Step model
******************************************** */ #include <stdio.h> #include <string.h> # ...
- 09-js数组常用方法
<html> <head> <title>js数组的常用操作</title> <meta charset="UTF-8"/&g ...