package com.log.DOM4jdemo;
import java.io.File;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.ProcessingInstruction;
import org.dom4j.VisitorSupport;
import org.dom4j.io.SAXReader;
public class DomTest {
public static void main(String[] args){
SAXReader saxReader=new SAXReader();
try{
Document doc=saxReader.read(new File("student.xml")); //student.xml位于项目的目录下面
doc.accept(new MyVisitor());
}catch (DocumentException e){
e.printStackTrace();
}
}
private static class MyVisitor extends VisitorSupport{
public void visit(Attribute node){
System.out.println("Attribute: "+node.getName()+"="+node.getValue());
}
public void visit(Element node){
if(node.isTextOnly()){
System.out.println("Element: "+node.getName()+" "+node.getText());
}else{
System.out.println("------------"+node.getName()+"-------------");
}
}
public void visit(ProcessingInstruction node){
System.out.println("PI: "+node.getTarget()+" "+node.getText());
}
}
}