For example:
下面是要读取的一个xml文件
<?xml version="1.0" encoding="GB2312" standalone="no"?>
<bookstore>
<book id="01">
<name>数学</name>
<price>10</price>
<memo>非常不错的一本书。</memo>
</book>
<book id="02">
<name>高等数学</name>
<price>20</price>
<memo>非常不错的一本书。</memo>
</book>
<book id="03">
<name>离散数学</name>
<price>30</price>
<memo>非常不错的一本书。</memo>
</book>
<book id="04">
<name>线性代数</name>
<price>40</price>
<memo>非常不错的一本书。</memo>
</book>
</bookstore>
为了方便存储数据,建一个与xml对应的java实体类
package com.leo.entity;
public class Book {
// 编号
private int id;
// 书名
private String name;
// 价格
private String price;
// 备注
private String memo;
public Book() {
super();
}
public Book(int id,String name, String price, String memo) {
super();
this.id=id;
this.name = name;
this.price = price;
this.memo = memo;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getMemo() {
return memo;
}
public void setMemo(String memo) {
this.memo = memo;
}
@Override
public String toString() {
return "Book [name=" + name + ", price=" + price + ", memo=" + memo + "]";
}
}
具体解析xml操作的类
package com.leo.xml;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import com.leo.entity.Book;
public class ReadXMl {
public static void main(String[] args) {
// 创建文档构建器工厂
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// 创建一个文档构建器,此处需要try,catch处理一下
DocumentBuilder db = null;
try {
db = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
// 创建xmldoc文档对象
Document xmldoc = null;
try {
// 读取本地XMl,路径可自定义
// 将给定 URI 的内容解析为一个 XML 文档,并返回Document对象
xmldoc = (Document) db.parse(new File("D:/123.xml"));
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// 自定义实体类Book,与xml文件中的每个单元对对应起来
ArrayList<Book> books = new ArrayList<Book>();
// 根据节点名获取全部节点,注意是NodeList,里面提供个getLength(),和item()两个方法来处理
NodeList bookList = xmldoc.getElementsByTagName("book");
// 根据bookList的长度循环解析数据。
for (int i = 0; i < bookList.getLength(); i++) {
// 循环一次创建一个Book实体,获取xml数据,赋值,最终加入books集合中,解析xml完成。
Book book = new Book();
// 获取第i个book结点
Node node = bookList.item(i);
// 获取第i个book的所有属性,也是集合
NamedNodeMap namedNodeMap = node.getAttributes();
// 获取已知名为id的属性值
String id = namedNodeMap.getNamedItem("id").getTextContent();
book.setId(Integer.parseInt(id));
// System.out.println(id);
// 获取book结点的子节点,包含了text类型的换行
/*
* 节点结构是这样的 [[#text: ], [name: null], [#text: ], [price: null], [#text: ],
* [memo: null], [#text: ]]
*/
NodeList cList = node.getChildNodes();
// System.out.println(cList.getLength());//7
// 将一个book里面的属性加入数组
ArrayList<String> contents = new ArrayList<>();
// 以上节点结构导致循环要从1开始,间隔为2
for (int j = 1; j < cList.getLength(); j += 2) {
// 获取book节点下的name,price,memo节点
Node cNode = cList.item(j);
// 获取name,price,memo节点中的文本内容。
String content = cNode.getFirstChild().getTextContent();
// 用集合保存文本数据
contents.add(content);
// System.out.println(contents);
}
// 从集合中取值,转为JavaBean
book.setName(contents.get(0));
book.setPrice(contents.get(1));
book.setMemo(contents.get(2));
// 将每一个book实体,加入books集合,数据读取完成。
books.add(book);
}
// 便利books集合,检验成果。
for (Book book : books) {
System.out.println(book);
}
// 备注,不同结构的xml文件结构不同,取值逻辑也不同,但只需掌握最基本的xml取值方法,剩下的相信难不倒你的。
/*
* Document中的 xmldoc.getElementsByTagName("book");根据节点名获取全部节点 org.w3c.dom.Node中的
* getAttributes();获取节点的所有属性 getChildNodes();获取所有子节点,返回的是NodeList
* getFirstChild().getTextContent()获取节点文本
* NodeList中有getLength()得到长度,和item()根据索引获取节点,从0开始
*/
}
}
//备注,不同结构的xml文件结构不同,取值逻辑也不同,但只需掌握最基本的xml取值方法,剩下的相信难不倒你的。
/* Document中的
* xmldoc.getElementsByTagName("book");根据节点名获取全部节点
* org.w3c.dom.Node中的
* getAttributes();获取节点的所有属性
* getChildNodes();获取所有子节点,返回的是NodeList
* getFirstChild().getTextContent()获取节点文本
* NodeList中有getLength()得到长度,和item()根据索引获取节点,从0开始
* */
最后,还可以看看, java创建XML文件到本地并写入数据