java中导入org.jdom.Document和org.dom4j.Document冲突
请各位大神解惑,dom4j和jdom无法同时运行,jdom中引用的Document和Element都显示属于dom4j包中,同时导入org.jdom.Document和org.dom4j.Document报错
代码如下:
package com.imooc.parsertest;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
//import org.jdom.Attribute;
//import org.jdom.Element;
import com.imooc.entity.Book;
import com.imooc.handler.SAXParserHandler;
import org.junit.Test;
public class ParserTest {
public void dom4jTest(){
ArrayList<Book> booksList = new ArrayList<Book>();
// 解析books.xml文件
//创建SAXReader的对象reader
SAXReader reader = new SAXReader();
try {
//通过reader对象的read方法加载xml文件,获取document对象
Document document = reader.read(new File("src/res/books.xml"));
//通过document对象获取根节点bookstore
Element bookStore = document.getRootElement();
//通过element对象的elementIterator方法获取迭代器
Iterator it = bookStore.elementIterator();
//遍历迭代器,获取根节点中的信息(书籍)
while(it.hasNext()){
Book books = new Book();
int i = 1;
// System.out.println("=====开始遍历某一本书=====");
Element book = (Element)it.next();
//获取book的属性名以及属性值
List<Attribute> bookAttrs = book.attributes();
for(Attribute attr : bookAttrs){
// System.out.println("属性名:" + attr.getName() + "---属性值:" + attr.getValue());
if(attr.getName().equals("id")){
books.setId(attr.getValue());
}
}
//通过element对象的elementIterator方法获取迭代器
Iterator itt = book.elementIterator();
//遍历迭代器,获取子节点中的信息
while(itt.hasNext()){
Element bookChild = (Element)itt.next();
// System.out.println("节点名:" + bookChild.getName() + "--节点值:" + bookChild.getStringValue());
if(bookChild.getName().equals("name")){
books.setName(bookChild.getStringValue());
}
else if(bookChild.getName().equals("author")){
books.setAuthor(bookChild.getStringValue());
}
else if(bookChild.getName().equals("year")){
books.setYear(bookChild.getStringValue());
}
else if(bookChild.getName().equals("price")){
books.setPrice(bookChild.getStringValue());
}
else if(bookChild.getName().equals("language")){
books.setLanguage(bookChild.getStringValue());
}
}
i++;
// System.out.println("=====结束遍历某一本书=====");
booksList.add(books);
books = null;
if(i == booksList.size()){
for(Book booksl : booksList){
// System.out.println("-----开始遍历-----");
// System.out.println(booksl.getId());
// System.out.println(booksl.getName());
// System.out.println(booksl.getAuthor());
// System.out.println(booksl.getYear());
// System.out.println(booksl.getPrice());
// System.out.println(booksl.getLanguage());
// System.out.println("-----结束遍历-----");
}
}
}
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void domTest(){
// 创建一个DocumentBuilderFactory的对象
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//创建一个DocumentBuilder的对象
try {
DocumentBuilder db = dbf.newDocumentBuilder();
//通过DocumentBuilder对象的parser方法加载books.xml文件到当前项目下
org.w3c.dom.Document document = db.parse("books.xml");
//获取所有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.println("属性名:" + attr.getNodeName());
// //获取属性值
// System.out.println("属性值" + attr.getNodeValue());
}
//前提: 已经知道book节点有且只有一个id属性
// //将book节点进行强制类型转换,转换成Element类型
// Element book = (Element)bookList.item(i);
// //通过getAttribute("id")方法获取属性值
// String s = book.getAttribute("id");
// System.out.println("id的属性值为:" + s);
//解析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.println(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) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void jdomTest(){
ArrayList<Book> booksList = new ArrayList<Book>();
// 进行对books.xml文件的JDOM解析
//准备工作
//1.创建一个SAXBuileder的对象
SAXBuilder saxBuilder = new SAXBuilder();
//2.创建一个输入流
InputStream in;
try {
//将xml文件加载到输入流中来
in = new FileInputStream("src/res/books.xml");
//创建一个InputStreamReader对象并声明转换后的编码格式
// InputStreamReader isr = new InputStreamReader(in, "UTF-8");
//3.通过saxBuilder的build方法,将输入流加载到saxBuilder中
org.jdom.Document document = saxBuilder.build(in);
//4.通过document对象获取xml文件的根节点
org.jdom.Element rootElement = document.getRootElement();
//5.获取根节点下的子节点的List集合
List<Element> bookList = rootElement.getChildren();
int i = 0;//定义一个变量用于比较根节点下子节点个数
//继续进行解析
for(Element book : bookList){
//新建book对象
Book bookEntity = new Book();
// System.out.println("=====开始解析第" + (bookList.indexOf(book) + 1) +"书======");
//解析book的属性
List<Attribute> attrList = ((org.jdom.Element) book).getAttributes();
// //知道节点下属性名称时,获取节点值
// System.out.println(book.getAttributeValue("id"));
//遍历attrList(针对不清楚book节点下属性的名字及属性)
for(Attribute attr : attrList){
//获取属性名
String attrName = attr.getName();
//获取属性值
String attrValue = attr.getValue();
// System.out.println("属性名:" + attrName + "---属性值:" + attrValue);
//赋值book的id属性
if(attrName.equals("id")){
bookEntity.setId(attrValue);
}
}
//对book节点的子节点的节点名和节点值的遍历
List<Element> bookChilds = ((org.jdom.Element) book).getChildren();
for(Element child : bookChilds){
// System.out.println("节点名:" + child.getName() + "---节点值:" + child.getStringValue());
// //通过判断循给对应属性赋值
if(child.getName().equals("name")){
bookEntity.setName(child.getStringValue());
}
else if(child.getName().equals("author")){
bookEntity.setAuthor(child.getStringValue());
}
else if(child.getName().equals("year")){
bookEntity.setYear(child.getStringValue());
}
else if(child.getName().equals("price")){
bookEntity.setPrice(child.getStringValue());
}
else if(child.getName().equals("language")){
bookEntity.setLanguage(child.getStringValue());
}
}
// System.out.println("=====结束解析第" + (bookList.indexOf(book) + 1) +"书======");
i++;
//将bookEntity对象存入booksList集合中
booksList.add(bookEntity);
bookEntity = null;//清空缓存,再次接收数据
if(i == bookList.size()){//判断xml中book对象全部插入集合后再输出
//遍历输出booksList集合中存入的数据
for(Book books : booksList){
// System.out.println(books.getId());
// System.out.println(books.getName());
// if(!(books.getAuthor() == null)){
// System.out.println(books.getAuthor());
// }
// System.out.println(books.getYear());
// System.out.println(books.getPrice());
// if(!(books.getLanguage() == null)){
// System.out.println(books.getLanguage());
// }
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void saxTest(){
try {
//获取一个SAXParserFactory的实例
SAXParserFactory factory = SAXParserFactory.newInstance();
//通过factory获取SAXParser实例
SAXParser parser = factory.newSAXParser();
//创建SAXParserHandler对象
SAXParserHandler handler = new SAXParserHandler();
//调用parser的parse方法解析xml文件
parser.parse("books.xml", handler);
//输出bookList数组中保存的book对象个数
// System.out.println("---共有" + handler.getBookList().size() + "本书!");
for(Book book : handler.getBookList()){//遍历循环输出每个book对象的每个属性
// System.out.println(book.getId());
// System.out.println(book.getName());
// System.out.println(book.getAuthor());
// System.out.println(book.getYear());
// System.out.println(book.getPrice());
// System.out.println(book.getLanguage());
// System.out.println("-----finish-----");
}
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void testPerformance() throws Exception{
System.out.println("---性能测试---");
long start = System.currentTimeMillis();
dom4jTest();
System.out.println("dom4j时长:" + (System.currentTimeMillis() - start));
start = System.currentTimeMillis();
domTest();
System.out.println("dom时长:" + (System.currentTimeMillis() - start));
start = System.currentTimeMillis();
saxTest();
System.out.println("sax时长:" + (System.currentTimeMillis() - start));
start = System.currentTimeMillis();
jdomTest();
System.out.println("jdom时长:" + (System.currentTimeMillis() - start));
}
}