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()); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- 2017 年热门编程语言排行榜,你的语言上榜没?
- c# – CTP5 EF Code First vs. Linq-to-sql
- ruby-on-rails-3 – 在ActiveModel中使用before_
- Flashbuilder4.6 + eclipse for j2ee + Blazeds,
- ruby-on-rails – 如何使用Ruby on Rails操作DOM
- ruby-on-rails – Gemfile在除一个环境之外的所有
- c# – Windows服务安装程序无法读取App.Config文
- ruby – 为什么Enumerable#each_with_object已弃
- Oracle触发器用法实例详解
- ruby-on-rails – 使用|| =语法执行memoization有