隨筆 - 31  文章 - 2  trackbacks - 0
          dom4j 是一種解析 XML 文檔的開放源代碼 XML 框架,與 W3C DOM API 相比,使用 dom4j 所包含的解析器的好處是 dom4j 擁有本地的 XPath 支持.DOM 解析器不支持使用 XPath 選擇節點.

          ??該解析器可以從 http://dom4j.org/ 獲取.
          java 代碼

          java 代碼
          1. package ?com.nikee.dom4j;???? ??
          2. import ?org.dom4j.Document;???? ??
          3. import ?org.dom4j.DocumentHelper;???? ??
          4. import ?org.dom4j.Element;???? ??
          5. import ?org.dom4j.io.OutputFormat;???? ??
          6. import ?org.dom4j.io.XMLWriter;???? ??
          7. import ?java.io.*;???? ??
          8. ????? ??
          9. public ? class ?XmlDom4J{???? ??
          10. ? public ? void ?generateDocument(){???? ??
          11. ?? //使用?DocumentHelper?類創建一個文檔實例。?DocumentHelper?是生成?XML?文檔節點的?dom4j?API?工廠類???? ??
          12. ??Document?document?=?DocumentHelper.createDocument();???? ??
          13. ?????? ??
          14. ?? //使用?addElement()方法創建根元素catalog?,?addElement()用于向?XML?文檔中增加元素???? ??
          15. ??Element?catalogElement?=?document.addElement( "catalog" );???? ??
          16. ?????? ??
          17. ?? //在?catalog?元素中使用?addComment()?方法添加注釋"An?XML?catalog"???? ??
          18. ??catalogElement.addComment( "An?XML?Catalog" );???? ??
          19. ?????? ??
          20. ?? //在?catalog?元素中使用?addProcessingInstruction()?方法增加一個處理指令???? ??
          21. ??catalogElement.addProcessingInstruction( "target" , "text" );???? ??
          22. ?????? ??
          23. ?? //在?catalog?元素中使用?addElement()?方法增加?journal?元素???? ??
          24. ??Element?journalElement?=?catalogElement.addElement( "journal" );???? ??
          25. ?? //使用?addAttribute()?方法向?journal?元素添加?title?和?publisher?屬性???? ??
          26. ??journalElement.addAttribute( "title" ,? "XML?Zone" );???? ??
          27. ??journalElement.addAttribute( "publisher" ,? "IBM?developerWorks" );???? ??
          28. ?????? ??
          29. ?????? ??
          30. ??Element?articleElement=journalElement.addElement( "article" );???? ??
          31. ??articleElement.addAttribute( "level" ,? "Intermediate" );???? ??
          32. ??articleElement.addAttribute( "date" ,? "December-2001" );???? ??
          33. ?????? ??
          34. ??Element?titleElement=articleElement.addElement( "title" );???? ??
          35. ??titleElement.setText( "Java?configuration?with?XML?Schema" );???? ??
          36. ?????? ??
          37. ??Element?authorElement=articleElement.addElement( "author" );???? ??
          38. ??Element?firstNameElement=authorElement.addElement( "firstname" );???? ??
          39. ??firstNameElement.setText( "Marcello" );???? ??
          40. ??Element?lastNameElement=authorElement.addElement( "lastname" );???? ??
          41. ??lastNameElement.setText( "Vitaletti" );???? ??
          42. ?????? ??
          43. ?? //可以使用?addDocType()?方法添加文檔類型說明???? ??
          44. ?? //這樣就向?XML?文檔中增加文檔類型說明:???? ??
          45. ??document.addDocType( "catalog" , "nikee" , "file://c:/Dtds/catalog.dtd" );???? ??
          46. ?? try {???? ??
          47. ??? //XMLWriter?output?=?new?XMLWriter(new?FileWriter(?new?File("D:/eclipse3.2/workspace/WebServices/src/com/nikee/dom4j/catalog.xml")));???? ??
          48. ????FileOutputStream?fos= new ?FileOutputStream( "D:/eclipse3.2/workspace/WebServices/src/com/nikee/dom4j/catalog.xml" );???? ??
          49. ???????OutputFormat?of= new ?OutputFormat( "????" ,? true );???? ??
          50. ???????XMLWriter?xw= new ?XMLWriter(fos,?of);???? ??
          51. ???????xw.write(?document?);???? ??
          52. ???????xw.close();???? ??
          53. ??}???? ??
          54. ?? catch (IOException?e)???? ??
          55. ??{???? ??
          56. ???System.out.println(e.getMessage());???? ??
          57. ??}???? ??
          58. ?}???? ??
          59. ????? ??
          60. ? public ? static ? void ?main(String[]?argv){???? ??
          61. ??XmlDom4J?dom4j= new ?XmlDom4J();???? ??
          62. ??dom4j.generateDocument();???? ??
          63. ?}???? ??
          64. }???? ??

          xml 代碼

          1. <!--sp--> xml? version = "1.0" ? encoding = "UTF-8" ?> ??? ??
          2. <!--CTYPE?catalog?PUBLIC?"nikee"?"file://c:/Dtds/catalog.dtd"</sp--> > ??? ??
          3. ??? ??
          4. ? < catalog > ??? ??
          5. ???? <!--An?XML?Catalog--><!--sp--> target?text ?> ??? ??
          6. ??? ??
          7. ???? < journal ? title = "XML?Zone" ? publisher = "IBM?developerWorks" > ??? ??
          8. ????? < article ? level = "Intermediate" ? date = "December-2001" > ??? ??
          9. ????????? < title > Java?configuration?with?XML?Schematitle > ??? ??
          10. ????????? < author > ??? ??
          11. ????????????? < firstname > Marcellofirstname > ??? ??
          12. ????????????? < lastname > Vitalettilastname > ??? ??
          13. ?????????author > ??? ??
          14. ?????article > ??? ??
          15. ????journal > ??? ??
          16. ?catalog > ??

          XmlDom4J.java 用于創建 XML 文檔 catalog.xml

          java 代碼
          java 代碼
          1. package?com.nikee.dom4j;???? ??
          2. ??? ??
          3. import?org.dom4j.Document;???? ??
          4. import?org.dom4j.Element;???? ??
          5. import?org.dom4j.Attribute;???? ??
          6. import?java.util.List;???? ??
          7. import?java.util.Iterator;???? ??
          8. ??? ??
          9. import?org.dom4j.io.OutputFormat;???? ??
          10. import?org.dom4j.io.XMLWriter;???? ??
          11. import?java.io.*;???? ??
          12. ??? ??
          13. import?org.dom4j.DocumentException;???? ??
          14. import?org.dom4j.io.SAXReader;????? ??
          15. ????? ??
          16. public?class?Dom4Jparser{???? ??
          17. ????public?Dom4Jparser(){???? ??
          18. ????????//construction???? ??
          19. ????}???? ??
          20. ???????? ??
          21. ????public?void?modifyDocument(File?inputXml){???? ??
          22. ????????try{???? ??
          23. ????????????//使用?SAXReader?解析?XML?文檔?catalog.xml???? ??
          24. ????????????SAXReader?saxReader?=?new?SAXReader();???? ??
          25. ????????????Document?document?=?saxReader.read(inputXml);???? ??
          26. ???????????????? ??
          27. ????????????//使用?XPath?表達式從?article?元素中獲得?level?節點列表。如果?level?屬性值是"Intermediate"則改為"Introductory"???? ??
          28. ????????????List?list?=?document.selectNodes("http://article/@level");???? ??
          29. ????????????Iterator?iter=list.iterator();???? ??
          30. ????????????while(iter.hasNext()){???? ??
          31. ????????????????Attribute?attribute=(Attribute)iter.next();???? ??
          32. ????????????????if(attribute.getValue().equals("Intermediate"))???? ??
          33. ????????????????????attribute.setValue("Introductory");????? ??
          34. ????????????}???? ??
          35. ???????????????? ??
          36. ????????????list?=?document.selectNodes("http://article/@date"?);???? ??
          37. ????????????iter=list.iterator();???? ??
          38. ????????????while(iter.hasNext()){???? ??
          39. ????????????????Attribute?attribute=(Attribute)iter.next();???? ??
          40. ????????????????if(attribute.getValue().equals("December-2001"))???? ??
          41. ????????????????????attribute.setValue("October-2002");???? ??
          42. ????????????}???? ??
          43. ???????????????? ??
          44. ????????????//獲取?article?元素列表,從?article?元素中的?title?元素得到一個迭代器,并修改?title?元素的文本???? ??
          45. ????????????list?=?document.selectNodes("http://article"?);???? ??
          46. ????????????iter=list.iterator();???? ??
          47. ????????????while(iter.hasNext()){???? ??
          48. ????????????????Element?element=(Element)iter.next();???? ??
          49. ????????????????Iterator?iterator=element.elementIterator("title");???? ??
          50. ????????????????while(iterator.hasNext()){???? ??
          51. ????????????????????Element?titleElement=(Element)iterator.next();???? ??
          52. ????????????????????if(titleElement.getText().equals("Java?configuration?with?XML?Schema"))???? ??
          53. ????????????????????????titleElement.setText("Create?flexible?and?extensible?XML?schema");???? ??
          54. ????????????????}???? ??
          55. ????????????}???? ??
          56. ???????????????? ??
          57. ???????????????? ??
          58. ????????????list?=?document.selectNodes("http://article/author"?);???? ??
          59. ????????????iter=list.iterator();???? ??
          60. ????????????while(iter.hasNext()){???? ??
          61. ????????????????Element?element=(Element)iter.next();???? ??
          62. ????????????????Iterator?iterator=element.elementIterator("firstname");???? ??
          63. ????????????????while(iterator.hasNext()){???? ??
          64. ????????????????????Element?firstNameElement=(Element)iterator.next();???? ??
          65. ????????????????????if(firstNameElement.getText().equals("Marcello"))???? ??
          66. ????????????????????????firstNameElement.setText("Ayesha");???? ??
          67. ????????????????}???? ??
          68. ????????????}???? ??
          69. ???????????????? ??
          70. ????????????list?=?document.selectNodes("http://article/author"?);???? ??
          71. ????????????iter=list.iterator();???? ??
          72. ????????????while(iter.hasNext()){???? ??
          73. ????????????????Element?element=(Element)iter.next();???? ??
          74. ????????????????Iterator?iterator=element.elementIterator("lastname");???? ??
          75. ????????????????while(iterator.hasNext()){???? ??
          76. ????????????????????Element?lastNameElement=(Element)iterator.next();???? ??
          77. ????????????????????if(lastNameElement.getText().equals("Vitaletti"))???? ??
          78. ????????????????????????lastNameElement.setText("Malik");???? ??
          79. ????????????????}???? ??
          80. ????????????}???? ??
          81. ???????????????? ??
          82. ????????????FileOutputStream?fos=new?FileOutputStream("D:/eclipse3.2/workspace/WebServices/src/com/nikee/dom4j/catalog-modified.xml");???? ??
          83. ????????????OutputFormat?of=new?OutputFormat("???",true);???? ??
          84. ????????????XMLWriter?xw=new?XMLWriter(fos,?of);???? ??
          85. ????????????xw.write(?document?);???? ??
          86. ????????????xw.close();???? ??
          87. ????????}???? ??
          88. ????????catch(DocumentException?e)???? ??
          89. ????????{???? ??
          90. ????????????e.printStackTrace();???? ??
          91. ????????}???? ??
          92. ????????catch(IOException?e){???? ??
          93. ????????????e.printStackTrace();???? ??
          94. ????????}???? ??
          95. ????}???? ??
          96. ???????? ??
          97. ????public?static?void?main(String?argv[]){???? ??
          98. ????????Dom4Jparser?dom4jParser=new?Dom4Jparser();???? ??
          99. ????????dom4jParser.modifyDocument(new?File("D:/eclipse3.2/workspace/WebServices/src/com/nikee/dom4j/catalog.xml"));???? ??
          100. ????}???? ??
          101. }???? ??
          xml 代碼
          xml 代碼
          1. <!--sp--> xml? version = "1.0" ? encoding = "UTF-8" ?> ??? ??
          2. ??? ??
          3. <!--<!DOCTYPE?catalog?PUBLIC?"nikee"?"file://c:/Dtds/catalog.dtd">--> ??? ??
          4. < catalog > ??? ??
          5. <!--An?XML?Catalog--><!--sp--> target?text ?> ??? ??
          6. ? < journal ? title = "XML?Zone" ? publisher = "IBM?developerWorks" > ????? ??
          7. ??? < article ? level = "Introductory" ? date = "October-2002" > ??????? ??
          8. ?????? < title > Create?flexible?and?extensible?XML?schematitle > ??? ??
          9. ?????? < author > ???????? ??
          10. ????????? < firstname > Ayeshafirstname > ??? ??
          11. ????????? < lastname > Maliklastname > ??? ??
          12. ??????author > ??? ??
          13. ???article > ??? ??
          14. ?journal > ??? ??
          15. catalog > ????
          Dom4Jparser.java用于修改 XML 文檔 catalog.xml為catalog-modified.xml

          問題總結:

          1.當catalog.xml有<!--CTYPE catalog PUBLIC "nikee" "file://c:/Dtds/catalog.dt-->這一句,Dom4Jparser.java修改 XML 文檔 catalog.xml為catalog-modified.xml時,發生異常org.dom4j.DocumentException: c Nested exception: c.

          原因:我自己沒有此 file://c:/Dtds/catalog.dtd .

          解決辦法:刪掉此行.

          2.除了加入dom4j.jar包外,必須加入jaxen.jar包,否則會發生異常.jaxen包可在 http://jaxen.org/ 下載.

          posted on 2007-11-02 18:00 緣來如此 閱讀(6988) 評論(0)  編輯  收藏 所屬分類: XML

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 南召县| 凉山| 逊克县| 新干县| 县级市| 长宁区| 宜兴市| 锦屏县| 水城县| 双峰县| 聊城市| 容城县| 日喀则市| 许昌县| 清苑县| 清新县| 阿瓦提县| 五指山市| 霍林郭勒市| 阜平县| 郧西县| 西和县| 蒙阴县| 西安市| 乌拉特中旗| 姚安县| 苗栗市| 河津市| 东乌珠穆沁旗| 合作市| 库车县| 定西市| 隆子县| 西和县| 民丰县| 资兴市| 萍乡市| 衡阳市| 金湖县| 酉阳| 儋州市|