XML-DOM4J

1

/**

*使用DOM4J 创建XML文档并输入带控制台

* @author YZB

*/

publicclass Dom4jTest {

publicstaticvoid main(String[] args) {

// 构造Document对象

Document doc=DocumentHelper.createDocument();

// 一个XML处理指令

doc.addProcessingInstruction("xml-stylesheet"," type='text/xsl' href='student.xsl'");

    Element root=doc.addElement("students");

//创建跟节点

//Element root=DocumentHelper.createElement("students");

//doc.setRootElement(root);//注意:用这种方法会把先前添加的处理指令清除掉,但在JDOM中不会清除

//添加节点对象

Element eltstu1=root.addElement("student");

Element eltname1=eltstu1.addElement("name");

Element eltage1=eltstu1.addElement("age");

eltname1.setText("张三");

eltage1.setText("27");

Element eltstu2=root.addElement("student");

Element eltname2=eltstu2.addElement("name");

Element eltage2=eltstu2.addElement("age");

eltname2.setText("张三");

eltage2.setText("27");

//创建输出格式对象

OutputFormat outfmt=new OutputFormat("    ", true, "gb2312");

XMLWriter xmlwriter=null;

try {

//创建输入流

//PrintWriter pw=new PrintWriter(System.out);

//调用输入方法

//doc.write(pw);

//注意如果是输出到文件中要刷新缓冲区

xmlwriter=new XMLWriter(new FileWriter("Dom4jstudent.xml"),outfmt);

xmlwriter.write(doc);

} catch (IOException e) {

e.printStackTrace();

}finally{

//pw.close();

try {

xmlwriter.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

2

/**

* 使用访问者模式遍历文档树。根据不同的节点进行不同的操作

* 访问者模式通常用于处理对象树结构,树中的每一个节点对象都可以接受一个访问者对象,

* 节点对象向访问者对象传递自身,而访问者对象则反过来调用节点对象的操作

* @author YZB

*/

publicclass VisitorTest {

publicstaticvoid main(String[] args) {

SAXReader saxReader=new SAXReader();

try {

Document doc=saxReader.read(new File("student.xml"));

doc.accept(new MyVisitor());

} catch (DocumentException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

publicclass MyVisitor extends VisitorSupport{

@Override

publicvoid visit(Attribute node) {

System.out.println("Attribute:"+node.getName()+"="+node.getValue());

}

@Override

publicvoid visit(Element node) {

if(node.isTextOnly()){

System.out.println("Element"+node.getName()+" "+node.getText());

}else{

System.out.println("----------"+node.getName()+"----------");

}

}

@Override

publicvoid visit(ProcessingInstruction node) {

System.out.println("PI:"+node.getTarget()+" "+node.getText());

}

}

3

/**

* dom4j基于事件的处理,不用解析完文档,在处理,

* 边解析边处理

* @author YZB

*

*/

publicclass ElementHandler {

publicstaticvoid main(String[] args) {

SAXReader saxReader=new SAXReader();

saxReader.addHandler("/students/student", new StudentHandler());

try {

saxReader.read(new File("student.xml"));

} catch (DocumentException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

publicclass StudentHandler implements ElementHandler {

@Override

publicvoid onStart(ElementPath arg0) {

Element elt=arg0.getCurrent();//返回当前元素

System.out.println("Found studen:t"+elt.attributeValue("id"));//这将返回sn属性的值

arg0.addHandler("name", new NameHander());//创建一个处理程序元素的对象

}

@Override

publicvoid onEnd(ElementPath arg0) {

arg0.removeHandler("name");

}

//ElementHandler接口定义了一个处理程序元素的对象

publicclass NameHander implements ElementHandler {

@Override

publicvoid onStart(ElementPath arg0) {

System.out.println(arg0.getPath());//这将返回路径

}

@Override

publicvoid onEnd(ElementPath arg0) {

Element elt=arg0.getCurrent();

System.out.println(elt.getName()+":"+elt.getText());

}

}

}


你可能感兴趣的:(XML-DOM4J)