微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

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

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

publicclass VisitorTest {

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());

}

publicvoid visit(Element node) {

if(node.isTextOnly()){

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

}else{

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

publicvoid visit(ProcessingInstruction node) {

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

3

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

* 边解析边处理

*

publicclass ElementHandler {

SAXReader saxReader=new SAXReader();

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

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

} catch (DocumentException e) {

// Todo Auto-generated catch block

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());//创建一个处理程序元素的对象

publicvoid onEnd(ElementPath arg0) {

arg0.removeHandler("name");

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

publicclass NameHander implements ElementHandler {

publicvoid onStart(ElementPath arg0) {

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

publicvoid onEnd(ElementPath arg0) {

Element elt=arg0.getCurrent();

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

}

原文地址:https://www.jb51.cc/xml/300452.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。