SAXBuilder saxBuilder = new SAXBuilder();
FileInputStream inputStream = new FileInputStream("D:\\bookstore.xml");
Document document = saxBuilder.build(inputStream);
Element rootElement = document.getRootElement();
List<Element> bookList = rootElement.getChildren();
List<Attribute> attrList = book.getAttributes();
// 属性名称
node.getName();
// 属性值
node.getValue();
// document为Document对象
Element rootElement = document.getRootElement();
// rootElement为Element对象
List<Element> bookList = rootElement.getChildren();
// book为Element对象
List<Attribute> attrList = book.getAttributes()
Element titleElement = book.getChild("title");
// titleElement为title节点
titleElement.getName();
titleElement.getValue()
<author>Scott Meyers</author>
package com.qunar.xml;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
/**
* JDOM解析XML文档
* @author sjf0115
*
*/
public class JDOMXMLCode {
public static void main(String[] args) {
try {
// 创建SAXBuilder对象
SAXBuilder saxBuilder = new SAXBuilder();
// 创建输入流对象并将XML文档加载到输入流中
FileInputStream inputStream = new FileInputStream("D:\\bookstore.xml");
// 通过saxBuilder对象的build方法将输入流加载到saxBuilder中
Document document = saxBuilder.build(inputStream);
// 通过document对象获取XML文档的根节点
Element rootElement = document.getRootElement();
// 根据根节点获取根节点下的子节点集合
List<Element> bookList = rootElement.getChildren();
// 遍历子节点
for (Element book : bookList) {
System.out.println("开始解析一本书...");
// 解析属性
List<Attribute> attrList = book.getAttributes();
for (Attribute attribute : attrList) {
// 属性名称
System.out.print("---" + attribute.getName() + ":");
// 属性值
System.out.println(attribute.getValue());
}//for
// 获取book节点下的子节点
List<Element> bookChildren = book.getChildren();
for (Element bookChild : bookChildren) {
// 节点名称
System.out.print("------" + bookChild.getName() + ":");
// 节点值
System.out.println(bookChild.getValue());
}//for
System.out.println("结束解析一本书...");
}//for
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
开始解析一本书...
---category:Java
------title:Java多线程编程核心技术
------author:高洪岩
------year:2015
------price:69.00
结束解析一本书...
开始解析一本书...
---category:C++
------title:Effective C++: 55 Specific Ways to Improve Your Programs and Designs
------author:Scott Meyers
------year:2006
------price:58.00
结束解析一本书...
开始解析一本书...
---category:Web
------title:Learning XML
------author:Erik T. Ray
------year:2016
------price:39.95
结束解析一本书...
|
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(new File("D:\\bookstore.xml"));
Element bookstore = document.getRootElement();
Iterator iterator = bookstore.elementIterator();
while (iterator.hasNext()) {
// 获取下一个子节点
Element book = (Element)iterator.next();
}
// 获取book的属性节点集合
List<Attribute> bookAttr = book.attributes();
// 遍历book属性节点
for (Attribute attribute : bookAttr) {
// 获取book的属性节点的属性名称以及属性值
System.out.println("name:" + attribute.getName() + " value:" + attribute.getValue());
}//for
Iterator ite = book.elementIterator();
// 遍历book节点的子节点
while(ite.hasNext()){
// book节点的子节点
Element bookChild = (Element)ite.next();
System.out.println("name:" + bookChild.getName() + " value:" + bookChild.getStringValue());
//System.out.println("name:" + bookChild.getName() + " value:" + bookChild.getText());
}//while
package com.qunar.xml;
import java.io.File;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class DOM4JXMLCode {
public static void main(String[] args) {
try {
// 创建SAXReader对象
SAXReader saxReader = new SAXReader();
// 通过SaxReader对象的read方法加载XML文档获取Document对象
Document document = saxReader.read(new File("D:\\bookstore.xml"));
// 通过Document对象获取根节点
Element bookstore = document.getRootElement();
// 通过Element对象的elementIterator方法获取迭代器
Iterator iterator = bookstore.elementIterator();
// 遍历根节点的子节点
while (iterator.hasNext()) {
System.out.println("开始解析一本书...");
// 获取下一个元素节点
Element book = (Element)iterator.next();
// 获取book的属性节点集合
List<Attribute> bookAttr = book.attributes();
// 遍历book属性节点
for (Attribute attribute : bookAttr) {
// 获取book的属性节点的属性名称以及属性值
System.out.println("name:" + attribute.getName() + " value:" + attribute.getValue());
}//for
Iterator ite = book.elementIterator();
// 遍历book节点的子节点
while(ite.hasNext()){
// book节点的子节点
Element bookChild = (Element)ite.next();
System.out.println("name:" + bookChild.getName() + " value:" + bookChild.getStringValue());
//System.out.println("name:" + bookChild.getName() + " value:" + bookChild.getText());
}//while
System.out.println("结束解析一本书...");
}
} catch (DocumentException e) {
e.printStackTrace();
}
}
}
开始解析一本书...
name:category value:Java
name:title value:Java多线程编程核心技术
name:author value:高洪岩
name:year value:2015
name:price value:69.00
结束解析一本书...
开始解析一本书...
name:category value:C++
name:title value:Effective C++: 55 Specific Ways to Improve Your Programs and Designs
name:author value:Scott Meyers
name:year value:2006
name:price value:58.00
结束解析一本书...
开始解析一本书...
name:category value:Web
name:title value:Learning XML
name:author value:Erik T. Ray
name:year value:2016
name:price value:39.95
结束解析一本书...
|