天天看點

dom4j(Version 1.6.1)快速入門

 dom4j(Version 1.6.1)快速入門

導讀:

  使用疊代器(Iterators)

  我們可以通過多種方法來操作XML文檔,這些方法傳回java裡标準的疊代器(Iterators)。例如:

  public void bar(Document document) throws DocumentException {

  Element root = document.getRootElement();

  //疊代根元素下面的所有子元素

  for ( Iterator i = root.elementIterator(); i.hasNext(); ) {

  Element element = (Element) i.next();

  //處理代碼

  }

  //疊代根元素下面名稱為"foo"的子元素

  for ( Iterator i = root.elementIterator( "foo" ); i.hasNext(); ) {

  Element foo = (Element) i.next();

  //處理代碼

  }

  // 疊代根元素的屬性attributes)元素

  for ( Iterator i = root.attributeIterator(); i.hasNext(); ) {

  Attribute attribute = (Attribute) i.next();

  // do something

  }

  }

  強大的XPath導航

  在dom4j中XPath可以表示出在XML樹狀結構中的Document或者任意的節點(Node)(例如:Attribute,Element 或者 ProcessingInstruction等)。它可以使在文檔中複雜的操作僅通過一行代碼就可以完成。例如:

  public void bar(Document document) {

  List list = document.selectNodes( "//foo/bar" );

  Node node = document.selectSingleNode( "//foo/bar/author" );

  String name = node.valueOf( "@name" );

  }

  如果你想得到一個XHTML文檔中的所有超文本連結(hypertext links)你可以使用下面的代碼:

  public void findLinks(Document document) throws DocumentException {

  List list = document.selectNodes( "//a/@href" );

  for (Iterator iter = list.iterator(); iter.hasNext(); ) {

  Attribute attribute = (Attribute) iter.next();

  String url = attribute.getValue();

  }

  }

  如果你需要關于XPath語言的任何幫助,我們強烈推薦這個站點Zvon tutorial他會通過一個一個的例子引導你學習。

  快速周遊(Fast Looping)

  如果你不得不周遊一個非常大的XML文檔,然後才去執行,我們建議你使用快速周遊方法(fast looping method),它可以避免為每一個循環的節點建立一個疊代器對象,如下所示:

  public void treeWalk(Document document) {

  treeWalk( document.getRootElement() );

  }

  public void treeWalk(Element element) {

  for ( int i = 0, size = element.nodeCount(); i   Node node = element.node(i);

  if ( node instanceof Element ) {

  treeWalk( (Element) node );

  }

  else {

  // do something....

  }

  }

  }

  生成一個新的XML文檔對象

  在dom4j中你可能常常希望用程式生成一個XML文檔對象,下面的程式為你進行了示範:

  import org.dom4j.Document;

  import org.dom4j.DocumentHelper;

  import org.dom4j.Element;

  public class Foo {

  public Document createDocument() {

  Document document = DocumentHelper.createDocument();

  Element root = document.addElement( "root" );

  Element author1 = root.addElement( "author" )

  .addAttribute( "name", "James" )

  .addAttribute( "location", "UK" )

  .addText( "James Strachan" );

  Element author2 = root.addElement( "author" )

  .addAttribute( "name", "Bob" )

  .addAttribute( "location", "US" )

  .addText( "Bob McWhirter" );

  return document;

  }

  }

  将一個文檔對象寫入檔案中

  将一個文檔對象寫入Writer對象的一個簡單快速的途徑是通過write()方法。

  FileWriter out = new FileWriter( "foo.xml" );

  document.write( out );

  如果你想改變輸出檔案的排版格式,比如你想要一個漂亮的格式或者是一個緊湊的格式,或者你想用Writer 對象或者OutputStream 對象來操作,那麼你可以使用XMLWriter 類。

  import org.dom4j.Document;

  import org.dom4j.io.OutputFormat;

  import org.dom4j.io.XMLWriter;

  public class Foo {

  public void write(Document document) throws IOException {

  // 寫入檔案

  XMLWriter writer = new XMLWriter(

  new FileWriter( "output.xml" )

  );

  writer.write( document );

  writer.close();

  // 以一種優雅的格式寫入System.out對象

  OutputFormat format = OutputFormat.createPrettyPrint();

  writer = new XMLWriter( System.out, format );

  writer.write( document );

  // 以一種緊湊的格式寫入System.out對象

  format = OutputFormat.createCompactFormat();

  writer = new XMLWriter( System.out, format );

  writer.write( document );

  }

  }

  轉化為字元串,或者從字元串轉化

  如果你有一個文檔(Document)對象或者任何一個節點(Node)對象的引用(reference),象屬性(Attribute)或者元素(Element),你可以通過asXML()方法把它轉化為一個預設的XML字元串:

  Document document = ...;

  String text = document.asXML();

  如果你有一些XML内容的字元串表示,你可以通過DocumentHelper.parseText()方法将它重新轉化為文檔(Document)對象:

  String text = "  Document document = DocumentHelper.parseText(text);

  通過XSLT樣式化文檔(Document)

  使用Sun公司提供的JAXP API将XSLT 應用到文檔(Document)上是很簡單的。它允許你使用任何的XSLT引擎(例如:Xalan或SAXON等)來開發。下面是一個使用JAXP建立一個轉化器(transformer),然後将它應用到文檔(Document)上的例子:

  import javax.xml.transform.Transformer;

  import javax.xml.transform.TransformerFactory;

  import org.dom4j.Document;

  import org.dom4j.io.DocumentResult;

  import org.dom4j.io.DocumentSource;

  public class Foo {

  public Document styleDocument(

  Document document,

  String stylesheet

  ) throws Exception {

  // 使用 JAXP 加載轉化器

  TransformerFactory factory = TransformerFactory.newInstance();

  Transformer transformer = factory.newTransformer(

  new StreamSource( stylesheet )

  );

  // 現在來樣式化一個文檔(Document)

  DocumentSource source = new DocumentSource( document );

  DocumentResult result = new DocumentResult();

  transformer.transform( source, result );

  // 傳回經過樣式化的文檔(Document)

  Document transformedDoc = result.getDocument();

  return transformedDoc;

  }

  }

本文轉自

http://www.360doc.com/showWeb/0/0/17051.aspx

上一篇: dom4j 使用
下一篇: Dom4j總結