package com.xiangshuai.dom4jjx;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.junit.Test;
public class Dom4j {
public static void main(String[] args) throws DocumentException {
SAXReader saxReader = new SAXReader();
Document document = saxReader.read("src/Book.xml");
Element root = document.getRootElement();
Element ele =(Element) root.elements().get(0);
Element element = ele.element("作者");
System.out.println(element.getName()+" ="+element.getText());
}
@Test
public void test1() throws DocumentException{
SAXReader saxReader = new SAXReader();
Document document = saxReader.read("src/Book.xml");
Element root = document.getRootElement();
treeWalk(root);
}
private void treeWalk(Element ele){
System.out.println(ele.getName());
for(int i=0;i<ele.nodeCount();i++){
Node node = ele.node(i);
if(node instanceof Element){
treeWalk((Element)node);
}else if(node instanceof Attribute){
Attribute attribute=(Attribute) node;
System.out.print(attribute.getName()+"="+attribute.getValue());
}
else {
System.out.println(" "+node.getText());
}
}
}
}