文章目录
一、普通方法
1、工具代码
public class DOMUtil {
public void DomXml(String sb) {
//创建一个DocumentBuilderFactory的对象
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//创建一个DocumentBuilder的对象
try {
//创建DocumentBuilder对象
DocumentBuilder db = dbf.newDocumentBuilder();
//通过DocumentBuilder对象的parser方法加载books.xml文件到当前项目下
Document document = db.parse(new InputSource(new ByteArrayInputStream(sb.getBytes("utf-8"))));
//获取所有book节点的集合
NodeList bookList = document.getElementsByTagName("book");
//通过nodelist的getLength()方法可以获取bookList的长度
System.out.println("一共有" + bookList.getLength() + "本书");
//遍历每一个book节点
for (int i = 0; i < bookList.getLength(); i++) {
System.out.println("=================下面开始遍历第" + (i + 1) + "本书的内容=================");
//通过 item(i)方法 获取一个book节点,nodelist的索引值从0开始
Node book = bookList.item(i);
//获取book节点的所有属性集合
NamedNodeMap attrs = book.getAttributes();
System.out.println("第 " + (i + 1) + "本书共有" + attrs.getLength() + "个属性");
//遍历book的属性
for (int j = 0; j < attrs.getLength(); j++) {
//通过item(index)方法获取book节点的某一个属性
Node attr = attrs.item(j);
//获取属性名
System.out.print("属性名:" + attr.getNodeName());
//获取属性值
System.out.println("--属性值" + attr.getNodeValue());
}
//解析book节点的子节点
NodeList childNodes = book.getChildNodes();
//遍历childNodes获取每个节点的节点名和节点值
System.out.println("第" + (i + 1) + "本书共有" +
childNodes.getLength() + "个子节点");
for (int k = 0; k < childNodes.getLength(); k++) {
//区分出text类型的node以及element类型的node
if (childNodes.item(k).getNodeType() == Node.ELEMENT_NODE) {
//获取了element类型节点的节点名
System.out.print("第" + (k + 1) + "个节点的节点名:"
+ childNodes.item(k).getNodeName());
//获取了element类型节点的节点值
System.out.println("--节点值是:" + childNodes.item(k).getFirstChild().getNodeValue());
//System.out.println("--节点值是:" + childNodes.item(k).getTextContent());
}
}
System.out.println("======================结束遍历第" + (i + 1) + "本书的内容=================");
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2、测试代码
void contextLoad14() {
DOMUtil domUtil = new DOMUtil();
String sb ="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<bookstore>\n" +
" <book id=\"1\">\n" +
" <name>冰与火之歌</name>\n" +
" <author>乔治马丁</author>\n" +
" <year>2014</year>\n" +
" <price>89</price>\n" +
" </book>\n" +
" <book id=\"2\">\n" +
" <name>安徒生童话</name>\n" +
" <year>2004</year>\n" +
" <price>77</price>\n" +
" <language>English</language>\n" +
" </book> \n" +
"</bookstore>\n";
domUtil.DomXml(sb);
}
3、效果
4、xml测试文本
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book id="1">
<name>冰与火之歌</name>
<author>乔治马丁</author>
<year>2014</year>
<price>89</price>
</book>
<book id="2">
<name>安徒生童话</name>
<year>2004</year>
<price>77</price>
<language>English</language>
</book>
</bookstore>
二、使用Jsoup
1、依赖
<!-- Jsoup依赖-->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.13.1</version>
</dependency>
2、测试代码
void contextLoad15() {
String sb = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<bookstore>\n" +
" <book id=\"1\">\n" +
" <name>冰与火之歌</name>\n" +
" <author>乔治马丁</author>\n" +
" <year>2014</year>\n" +
" <price>89</price>\n" +
" </book>\n" +
" <book id=\"2\">\n" +
" <name>安徒生童话</name>\n" +
" <year>2004</year>\n" +
" <price>77</price>\n" +
" <language>English</language>\n" +
" </book> \n" +
"</bookstore>\n";
Document doc = Jsoup.parse(sb);
//获取所有book属性的集合
Elements elements = doc.select("book");
System.out.println("一共有" + elements.size() + "本书");
for (int i = 0; i < elements.size(); i++) {
System.out.println("第 " + (i + 1) + "本书共有" + elements.get(i).childrenSize() + "个属性");
for (int k = 0; k < elements.get(i).childrenSize(); k++) {
System.out.print("第" + (k + 1) + "个属性名:" + elements.get(i).child(k).tagName());
System.out.println("--属性值是:" + elements.get(i).child(k).text());
}
}
}
3、效果