




全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
ParsingXML或许你想要做的第一件事情就是解析一个某种类型的XML文档,用dom4j很容易做到。请看下面的示范代码:.URL;importorg.dom4j.Document;importorg.dom4j.DocumentException;importorg.dom4j.io.SAXReader;publicclassFoopublicDocumentparse(URLurl)throwsDocumentExceptionSAXReaderreader=newSAXReader();Documentdocument=reader.read(url);returndocument;使用迭代器(Iterators)我们可以通过多种方法来操作XML文档,这些方法返回java里标准的迭代器(Iterators)。例如:publicvoidbar(Documentdocument)throwsDocumentExceptionElementroot=document.getRootElement();/迭代根元素下面的所有子元素for(Iteratori=root.elementIterator();i.hasNext();)Elementelement=(Element)i.next();/处理代码/迭代根元素下面名称为foo的子元素for(Iteratori=root.elementIterator(foo);i.hasNext();)Elementfoo=(Element)i.next();/处理代码/迭代根元素的属性attributes)元素for(Iteratori=root.attributeIterator();i.hasNext();)Attributeattribute=(Attribute)i.next();/dosomething强大的XPath导航在dom4j中XPath可以表示出在XML树状结构中的Document或者任意的节点(Node)(例如:Attribute,Element或者ProcessingInstruction等)。它可以使在文档中复杂的操作仅通过一行代码就可以完成。例如:publicvoidbar(Documentdocument)Listlist=document.selectNodes(/foo/bar);Nodenode=document.selectSingleNode(/foo/bar/author);Stringname=node.valueOf(name);如果你想得到一个XHTML文档中的所有超文本链接(hypertextlinks)你可以使用下面的代码:publicvoidfindLinks(Documentdocument)throwsDocumentExceptionListlist=document.selectNodes(/a/href);for(Iteratoriter=list.iterator();iter.hasNext();)Attributeattribute=(Attribute)iter.next();Stringurl=attribute.getValue();如果你需要关于XPath语言的任何帮助,我们强烈推荐这个站点Zvontutorial他会通过一个一个的例子引导你学习。快速遍历(FastLooping)如果你不得不遍历一个非常大的XML文档,然后才去执行,我们建议你使用快速遍历方法(fastloopingmethod),它可以避免为每一个循环的节点创建一个迭代器对象,如下所示:publicvoidtreeWalk(Documentdocument)treeWalk(document.getRootElement();publicvoidtreeWalk(Elementelement)for(inti=0,size=element.nodeCount();isize;i+)Nodenode=element.node(i);if(nodeinstanceofElement)treeWalk(Element)node);else/dosomething.生成一个新的XML文档对象在dom4j中你可能常常希望用程序生成一个XML文档对象,下面的程序为你进行了示范:importorg.dom4j.Document;importorg.dom4j.DocumentHelper;importorg.dom4j.Element;publicclassFoopublicDocumentcreateDocument()Documentdocument=DocumentHelper.createDocument();Elementroot=document.addElement(root);Elementauthor1=root.addElement(author).addAttribute(name,James).addAttribute(location,UK).addText(JamesStrachan);Elementauthor2=root.addElement(author).addAttribute(name,Bob).addAttribute(location,US).addText(BobMcWhirter);returndocument;将一个文档对象写入文件中将一个文档对象写入Writer对象的一个简单快速的途径是通过write()方法。FileWriterout=newFileWriter(foo.xml);document.write(out);如果你想改变输出文件的排版格式,比如你想要一个漂亮的格式或者是一个紧凑的格式,或者你想用Writer对象或者OutputStream对象来操作,那么你可以使用XMLWriter类。importorg.dom4j.Document;importorg.dom4j.io.OutputFormat;importorg.dom4j.io.XMLWriter;publicclassFoopublicvoidwrite(Documentdocument)throwsIOException/写入文件XMLWriterwriter=newXMLWriter(newFileWriter(output.xml);writer.write(document);writer.close();/以一种优雅的格式写入System.out对象OutputFormatformat=OutputFormat.createPrettyPrint();writer=newXMLWriter(System.out,format);writer.write(document);/以一种紧凑的格式写入System.out对象format=OutputFormat.createCompactFormat();writer=newXMLWriter(System.out,format);writer.write(document);转化为字符串,或者从字符串转化如果你有一个文档(Document)对象或者任何一个节点(Node)对象的引用(reference),象属性(Attribute)或者元素(Element),你可以通过asXML()方法把它转化为一个默认的XML字符串:Documentdocument=.;Stringtext=document.asXML();如果你有一些XML内容的字符串表示,你可以通过DocumentHelper.parseText()方法将它重新转化为文档(Document)对象:Stringtext=James;Documentdocument=DocumentHelper.parseText(text);通过XSLT样式化文档(Document)使用Sun公司提供的JAXPAPI将XSLT应用到文档(Document)上是很简单的。它允许你使用任何的XSLT引擎(例如:Xalan或SAXON等)来开发。下面是一个使用JAXP创建一个转化器(transformer),然后将它应用到文档(Document)上的例子:importjavax.xml.transform.Transformer;importjavax.xml.transform.TransformerFactory;importorg.dom4j.Document;importorg.dom4j.io.DocumentResult;importorg.dom4j.io.DocumentSource;publicclassFoopublicDocumentstyleDocument(Documentdocument,Stringstylesheet)throwsException/使用JAXP加载转化器TransformerFactoryfactory=TransformerFactory.newInstance();Transformertransformer=factory.newTransformer(newStreamSource(stylesheet);/现在来样式化一个文档(Document)DocumentS
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 【正版授权】 ISO/IEC/IEEE 8802-1Q:2024/Amd 38:2025 EN Telecommunications and exchange between information technology systems - Requirements for local and metropolitan area networks - Par
- 高中地球自转课件动态
- 资金管理与使用审批流程
- 高三家长会教学课件
- 离婚抚养费及子女教育、医疗、生活费用协议
- 公共停车场与物业管理公司合作协议模板
- 离婚赡养费支付协议书范本集锦
- 骶椎CT断层解剖图解课件
- 如何正确应对各种心理困扰
- 航海船舶安全管理制度
- 洁净室区甲醛熏蒸消毒标准操作规程
- 4.1 整式(第1课时 单项式) 课件 七年级数学上册 (人教版2024)
- 中国急性缺血性卒中诊治指南(2023)解读
- 常熟理工学院图书馆考试完整题库
- 招聘诚信承诺书
- (高清版)JTG D81-2017 公路交通安全设施设计规范
- 装配式混凝土检查井施工及验收规程
- 2024小红书无货源精细化铺货实战课程
- 任正非的创业故事
- 学生实习家长知情同意书(完美版)
- 涉警网络负面舆情应对与处置策略
评论
0/150
提交评论