使用dom4j和使用Element.selectSingleNode出錯解決

來源:互聯網
上載者:User
  Element root = document.getRootElement();  
   
      List   recordenvlist = document.selectNodes("//gml:envelope");     //會出錯的代碼
      Element el=(Element)root.selectSingleNode("info[@id='"+id+"']");//或這句代碼
錯誤提示為: NoClassDefFoundError:org/jaxen........ org.dom4j.DocumentFactory.createXPath 原因:dom4j的xpath api依賴jaxen完成 解決方案:在系統中加入 jaxen.jar. jaxen.jar下載路 徑: http://jaxen.codehaus.org/releases.html

---------------------------------------------------------參考-IBM:http://www.ibm.com/developerworks/cn/xml/x-dom4j.html---------------------------------------------------------------------

dom4j API 包含一個解析 XML 文檔的工具。本文中將使用這個解析器建立一個樣本 XML 文檔。清單 1 顯示了這個樣本 XML 文檔,catalog.xml。 清單 1. 樣本 XML 文檔(catalog.xml) 

<?xml version="1.0" encoding="UTF-8"?> <catalog> <!--An XML Catalog--> <?target instruction?>  <journal title="XML Zone"                   publisher="IBM developerWorks"> <article level="Intermediate" date="December-2001"> <title>Java configuration with XML Schema</title>  <author>      <firstname>Marcello</firstname>      <lastname>Vitaletti</lastname>  </author>  </article>  </journal> </catalog>

然後使用同一個解析器修改 catalog.xml,清單 2 是修改後的 XML 文檔,catalog-modified.xml。 清單 2. 修改後的 XML 文檔(catalog-modified.xml) 

<?xml version="1.0" encoding="UTF-8"?> <catalog> <!--An XML catalog--> <?target instruction?>  <journal title="XML Zone"                   publisher="IBM developerWorks"> <article level="Introductory" date="October-2002"> <title>Create flexible and extensible XML schemas</title>  <author>      <firstname>Ayesha</firstname>      <lastname>Malik</lastname>  </author>   </article>  </journal> </catalog>

與 W3C DOM API 相比,使用 dom4j 所包含的解析器的好處是 dom4j 擁有本地的 XPath 支援。DOM 解析器不支援使用 XPath 選擇節點。

本文包括以下幾個部分: 預先設定 建立文檔 修改文檔

預先設定

這個解析器可以從 http://dom4j.org 擷取。通過設定使 de>dom4j-1.4/dom4j-full.jarde> 能夠在 classpath 中訪問,該檔案中包括 dom4j 類、XPath 引擎以及 SAX 和 DOM 介面。如果已經使用了 JAXP 解析器中包含的 SAX 和 DOM 介面,向 classpath 中增加 de>dom4j-1.4/dom4j.jarde> 。 de>dom4j.jar de> 包括 dom4j 類和 XPath 引擎,但是不含 SAX 與 DOM 介面。



 

建立文檔

本節討論使用 dom4j API 建立 XML 文檔的過程,並建立樣本 XML 文檔 catalog.xml。

使用 import 語句匯入 dom4j API 類:

import org.dom4j.Document;import org.dom4j.DocumentHelper;import org.dom4j.Element;

使用 de>DocumentHelperde> 類建立一個文檔執行個體。 de>DocumentHelperde> 是產生 XML 文檔節點的 dom4j API 工廠類。

 Document document = DocumentHelper.createDocument();

使用 de>addElement()de> 方法建立根項目 de>catalogde> 。 de> addElement()de> 用於向 XML 文檔中增加元素。

Element catalogElement = document.addElement("catalog");

在 de>catalogde> 元素中使用 de>addComment()de> 方法添加註釋“An XML catalog”。

 catalogElement.addComment("An XML catalog");

在 de>catalogde> 元素中使用 de>addProcessingInstruction()de> 方法增加一個處理指示。

catalogElement.addProcessingInstruction("target","text");

在 de>catalogde> 元素中使用 de>addElement()de> 方法增加 de>journalde> 元素。

Element journalElement =  catalogElement.addElement("journal");

使用 de>addAttribute()de> 方法向 de>journalde> 元素添加 de>titlede> 和 de>publisherde> 屬性。

journalElement.addAttribute("title", "XML Zone");         journalElement.addAttribute("publisher", "IBM developerWorks");

向 de>articlede> 元素中添加 de>journalde> 元素。

Element articleElement=journalElement.addElement("article");

為 de>articlede> 元素增加 de>levelde> 和 de>datede> 屬性。

articleElement.addAttribute("level", "Intermediate");      articleElement.addAttribute("date", "December-2001");

向 de>articlede> 元素中增加 de>titlede> 元素。

Element titleElement=articleElement.addElement("title");

使用 de>setText()de> 方法設定 de>articlede> 元素的文本。

titleElement.setText("Java configuration with XML Schema");

在 de>articlede> 元素中增加 de>authorde> 元素。

Element authorElement=articleElement.addElement("author");

在 de>authorde> 元素中增加 de>firstnamede> 元素並設定該元素的文本。

Element  firstNameElement=authorElement.addElement("firstname");     firstNameElement.setText("Marcello");

在 de>authorde> 元素中增加 de>lastnamede> 元素並設定該元素的文本。

Element lastNameElement=authorElement.addElement("lastname");     lastNameElement.setText("Vitaletti");

可以使用 de>addDocType() de> 方法添加文件類型說明。

document.addDocType("catalog", null,"file://c:/Dtds/catalog.dtd");

這樣就向 XML 文檔中增加文件類型說明:

<!DOCTYPE catalog SYSTEM "file://c:/Dtds/catalog.dtd">

如果文檔要使用文件類型定義(DTD)文檔驗證則必須有 Doctype。

XML 聲明 de><?xml version="1.0" encoding="UTF-8"?>de> 自動添加到 XML 文檔中。

清單 3 所示的例子程式 XmlDom4J.java 用於建立 XML 文檔 catalog.xml。 清單 3. 產生 XML 文檔 catalog.xml 的程式(XmlDom4J.java) 

import org.dom4j.Document;import org.dom4j.DocumentHelper;import org.dom4j.Element;import org.dom4j.io.XMLWriter;import java.io.*;public class XmlDom4J{public void generateDocument(){Document document = DocumentHelper.createDocument();     Element catalogElement = document.addElement("catalog");     catalogElement.addComment("An XML Catalog");     catalogElement.addProcessingInstruction("target","text");     Element journalElement =  catalogElement.addElement("journal");     journalElement.addAttribute("title", "XML Zone");     journalElement.addAttribute("publisher", "IBM developerWorks");     Element articleElement=journalElement.addElement("article");     articleElement.addAttribute("level", "Intermediate");     articleElement.addAttribute("date", "December-2001");     Element  titleElement=articleElement.addElement("title");     titleElement.setText("Java configuration with XML Schema");     Element authorElement=articleElement.addElement("author");     Element  firstNameElement=authorElement.addElement("firstname");     firstNameElement.setText("Marcello");     Element lastNameElement=authorElement.addElement("lastname");     lastNameElement.setText("Vitaletti");     document.addDocType("catalog",                           null,"file://c:/Dtds/catalog.dtd");    try{    XMLWriter output = new XMLWriter(            new FileWriter( new File("c:/catalog/catalog.xml") ));        output.write( document );        output.close();        }     catch(IOException e){System.out.println(e.getMessage());}}public static void main(String[] argv){XmlDom4J dom4j=new XmlDom4J();dom4j.generateDocument();}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.