欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
使用 domj4 API 創(chuàng )建與修改 XML 文檔

使用 domj4 API 創(chuàng )建與修改 XML 文檔

dom4j 是一種解析 XML 文檔的開(kāi)放源代碼 XML 框架。本文介紹如何使用包含在 dom4j 中的解析器創(chuàng )建并修改 XML 文檔。

dom4j API 包含一個(gè)解析 XML 文檔的工具。本文中將使用這個(gè)解析器創(chuàng )建一個(gè)示例 XML 文檔。清單 1 顯示了這個(gè)示例 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>

然后使用同一個(gè)解析器修改 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 選擇節點(diǎn)。

本文包括以下幾個(gè)部分:

  • 預先設置
  • 創(chuàng )建文檔
  • 修改文檔

預先設置
這個(gè)解析器可以從 http://dom4j.org/ 獲取。通過(guò)設置使 dom4j-1.4/dom4j-full.jar 能夠在 classpath 中訪(fǎng)問(wèn),該文件中包括 dom4j 類(lèi)、XPath 引擎以及 SAX 和 DOM 接口。如果已經(jīng)使用了 JAXP 解析器中包含的 SAX 和 DOM 接口,向 classpath 中增加 dom4j-1.4/dom4j.jar。dom4j.jar 包括 dom4j 類(lèi)和 XPath 引擎,但是不含 SAX 與 DOM 接口。

創(chuàng )建文檔
本節討論使用 dom4j API 創(chuàng )建 XML 文檔的過(guò)程,并創(chuàng )建示例 XML 文檔 catalog.xml。

使用 import 語(yǔ)句導入 dom4j API 類(lèi):

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

使用 DocumentHelper 類(lèi)創(chuàng )建一個(gè)文檔實(shí)例。DocumentHelper 是生成 XML 文檔節點(diǎn)的 dom4j API 工廠(chǎng)類(lèi)。

 Document document = DocumentHelper.createDocument();

使用 addElement() 方法創(chuàng )建根元素 catalog。 addElement() 用于向 XML 文檔中增加元素。

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

catalog 元素中使用 addComment() 方法添加注釋“An XML catalog”。

 catalogElement.addComment("An XML catalog");

catalog 元素中使用 addProcessingInstruction() 方法增加一個(gè)處理指令。

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

catalog 元素中使用 addElement() 方法增加 journal 元素。

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

使用 addAttribute() 方法向 journal 元素添加 titlepublisher 屬性。

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

article 元素中添加 journal 元素。

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

article 元素增加 leveldate 屬性。

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

article 元素中增加 title 元素。

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

使用 setText() 方法設置 article 元素的文本。

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

article 元素中增加 author 元素。

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

author 元素中增加 firstname 元素并設置該元素的文本。

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

author 元素中增加 lastname 元素并設置該元素的文本。

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

這樣就向 XML 文檔中增加文檔類(lèi)型說(shuō)明:

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

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

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

清單 3 所示的例子程序 XmlDom4J.java 用于創(chuàng )建 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 org.dom4j.io.OutputFormat;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://f:/catalog.dtd");    try{    	OutputFormat format = OutputFormat.createPrettyPrint();	XMLWriter output = new XMLWriter(
new FileWriter( new File("f:/dom4j/catalog.xml") ), format);        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();}}

這一節討論了創(chuàng )建 XML 文檔的過(guò)程,下一節將介紹使用 dom4j API 修改這里創(chuàng )建的 XML 文檔。

修改文檔
這一節說(shuō)明如何使用 dom4j API 修改示例 XML 文檔 catalog.xml。

使用 SAXReader 解析 XML 文檔 catalog.xml:

SAXReader saxReader = new SAXReader(); Document document = saxReader.read(inputXml);

SAXReader 包含在 org.dom4j.io 包中。

inputXml 是從 c:/catalog/catalog.xml 創(chuàng )建的 java.io.File。使用 XPath 表達式從 article 元素中獲得 level 節點(diǎn)列表。如果 level 屬性值是“Intermediate”則改為“Introductory”。

List list = document.selectNodes("http://article/@level" );      Iterator iter=list.iterator();        while(iter.hasNext()){            Attribute attribute=(Attribute)iter.next();               if(attribute.getValue().equals("Intermediate"))               attribute.setValue("Introductory");        }

獲取 article 元素列表,從 article 元素中的 title 元素得到一個(gè)迭代器,并修改 title 元素的文本。

list = document.selectNodes("http://article" );     iter=list.iterator();   while(iter.hasNext()){       Element element=(Element)iter.next();      Iterator iterator=element.elementIterator("title");   while(iterator.hasNext()){   Element titleElement=(Element)iterator.next();   if(titleElement.getText().equals("Java configuration with XML Schema"))     titleElement.setText("Create flexible and extensible XML schema");    }}

通過(guò)和 title 元素類(lèi)似的過(guò)程修改 author 元素。

清單 4 所示的示例程序 Dom4JParser.java 用于把 catalog.xml 文檔修改成 catalog-modified.xml 文檔。

清單 4. 用于修改 catalog.xml 的程序(Dom4Jparser.java)

import org.dom4j.Document;import org.dom4j.Element;import org.dom4j.Attribute;import java.util.List;import java.util.Iterator;import org.dom4j.io.XMLWriter;import java.io.*;import org.dom4j.DocumentException;import org.dom4j.io.SAXReader; public class Dom4JParser{ public void modifyDocument(File inputXml){  try{   SAXReader saxReader = new SAXReader();   Document document = saxReader.read(inputXml);   List list = document.selectNodes("http://article/@level" );   Iterator iter=list.iterator();   while(iter.hasNext()){    Attribute attribute=(Attribute)iter.next();    if(attribute.getValue().equals("Intermediate"))      attribute.setValue("Introductory");        }      list = document.selectNodes("http://article/@date" );   iter=list.iterator();   while(iter.hasNext()){    Attribute attribute=(Attribute)iter.next();    if(attribute.getValue().equals("December-2001"))      attribute.setValue("October-2002");       }   list = document.selectNodes("http://article" );   iter=list.iterator();   while(iter.hasNext()){    Element element=(Element)iter.next();    Iterator iterator=element.elementIterator("title");      while(iterator.hasNext()){        Element titleElement=(Element)iterator.next();        if(titleElement.getText().equals("Java configuration with XML      Schema"))        titleElement.setText("Create flexible and extensible XML schema");                                          }                                }    list = document.selectNodes("http://article/author" );    iter=list.iterator();     while(iter.hasNext()){     Element element=(Element)iter.next();     Iterator iterator=element.elementIterator("firstname");     while(iterator.hasNext()){      Element firstNameElement=(Element)iterator.next();      if(firstNameElement.getText().equals("Marcello"))      firstNameElement.setText("Ayesha");                                     }                              }    list = document.selectNodes("http://article/author" );    iter=list.iterator();     while(iter.hasNext()){      Element element=(Element)iter.next();      Iterator iterator=element.elementIterator("lastname");     while(iterator.hasNext()){      Element lastNameElement=(Element)iterator.next();      if(lastNameElement.getText().equals("Vitaletti"))      lastNameElement.setText("Malik");                                  }                               }     XMLWriter output = new XMLWriter(      new FileWriter( new File("f:/dom4j/catalog-modified.xml") ));     output.write( document );     output.close();   }   catch(DocumentException e)                 {                  System.out.println(e.getMessage());                            }  catch(IOException e){                       System.out.println(e.getMessage());                    } } public static void main(String[] argv){  Dom4JParser dom4jParser=new Dom4JParser();  dom4jParser.modifyDocument(new File("f:/dom4j/catalog.xml"));                                        }   }

這一節說(shuō)明了如何使用 dom4j 中的解析器修改示例 XML 文檔。這個(gè)解析器不使用 DTD 或者模式驗證 XML 文檔。如果 XML 文檔需要驗證,可以解釋用 dom4j 與 JAXP SAX 解析器。

本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
使用 dom4j 解析 XML
dom4j讀取某個(gè)元素的某個(gè)屬性
利用開(kāi)源項目Dom4J生成包含中文的XML文檔 - Passer‘s歷史的天空 - Don...
Dom4j的使用(全而好的文章) - xhy0422 - JavaEye技術(shù)網(wǎng)站
DOM4J解析XML文檔、Document對象、節點(diǎn)對象節點(diǎn)對象屬性、將文檔寫(xiě)入XML文件
Dom4J解析xml文檔
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久