??該解析器可以從 http://dom4j.org/ 獲取.
java 代碼
java 代碼
- package ?com.nikee.dom4j;???? ??
- import ?org.dom4j.Document;???? ??
- import ?org.dom4j.DocumentHelper;???? ??
- import ?org.dom4j.Element;???? ??
- import ?org.dom4j.io.OutputFormat;???? ??
- import ?org.dom4j.io.XMLWriter;???? ??
- import ?java.io.*;???? ??
- ????? ??
- public ? class ?XmlDom4J{???? ??
- ? public ? void ?generateDocument(){???? ??
- ?? //使用?DocumentHelper?類創(chuàng)建一個文檔實例。?DocumentHelper?是生成?XML?文檔節(jié)點的?dom4j?API?工廠類???? ??
- ??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()?方法增加一個處理指令???? ??
- ??catalogElement.addProcessingInstruction( "target" , "text" );???? ??
- ?????? ??
- ?? //在?catalog?元素中使用?addElement()?方法增加?journal?元素???? ??
- ??Element?journalElement?=?catalogElement.addElement( "journal" );???? ??
- ?? //使用?addAttribute()?方法向?journal?元素添加?title?和?publisher?屬性???? ??
- ??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" );???? ??
- ?????? ??
- ?? //可以使用?addDocType()?方法添加文檔類型說明???? ??
- ?? //這樣就向?XML?文檔中增加文檔類型說明:???? ??
- ??document.addDocType( "catalog" , "nikee" , "file://c:/Dtds/catalog.dtd" );???? ??
- ?? try {???? ??
- ??? //XMLWriter?output?=?new?XMLWriter(new?FileWriter(?new?File("D:/eclipse3.2/workspace/WebServices/src/com/nikee/dom4j/catalog.xml")));???? ??
- ????FileOutputStream?fos= new ?FileOutputStream( "D:/eclipse3.2/workspace/WebServices/src/com/nikee/dom4j/catalog.xml" );???? ??
- ???????OutputFormat?of= new ?OutputFormat( "????" ,? true );???? ??
- ???????XMLWriter?xw= new ?XMLWriter(fos,?of);???? ??
- ???????xw.write(?document?);???? ??
- ???????xw.close();???? ??
- ??}???? ??
- ?? catch (IOException?e)???? ??
- ??{???? ??
- ???System.out.println(e.getMessage());???? ??
- ??}???? ??
- ?}???? ??
- ????? ??
- ? public ? static ? void ?main(String[]?argv){???? ??
- ??XmlDom4J?dom4j= new ?XmlDom4J();???? ??
- ??dom4j.generateDocument();???? ??
- ?}???? ??
- }???? ??
xml 代碼
- <!--sp--> xml? version = "1.0" ? encoding = "UTF-8" ?> ??? ??
- <!--CTYPE?catalog?PUBLIC?"nikee"?"file://c:/Dtds/catalog.dtd"</sp--> > ??? ??
- ??? ??
- ? < catalog > ??? ??
- ???? <!--An?XML?Catalog--><!--sp--> target?text ?> ??? ??
- ??? ??
- ???? < journal ? title = "XML?Zone" ? publisher = "IBM?developerWorks" > ??? ??
- ????? < article ? level = "Intermediate" ? date = "December-2001" > ??? ??
- ????????? < title > Java?configuration?with?XML?Schematitle > ??? ??
- ????????? < author > ??? ??
- ????????????? < firstname > Marcellofirstname > ??? ??
- ????????????? < lastname > Vitalettilastname > ??? ??
- ?????????author > ??? ??
- ?????article > ??? ??
- ????journal > ??? ??
- ?catalog > ??
XmlDom4J.java 用于創(chuàng)建 XML 文檔 catalog.xml
java 代碼
java 代碼
- package?com.nikee.dom4j;???? ??
- ??? ??
- import?org.dom4j.Document;???? ??
- import?org.dom4j.Element;???? ??
- import?org.dom4j.Attribute;???? ??
- import?java.util.List;???? ??
- import?java.util.Iterator;???? ??
- ??? ??
- import?org.dom4j.io.OutputFormat;???? ??
- import?org.dom4j.io.XMLWriter;???? ??
- import?java.io.*;???? ??
- ??? ??
- import?org.dom4j.DocumentException;???? ??
- import?org.dom4j.io.SAXReader;????? ??
- ????? ??
- public?class?Dom4Jparser{???? ??
- ????public?Dom4Jparser(){???? ??
- ????????//construction???? ??
- ????}???? ??
- ???????? ??
- ????public?void?modifyDocument(File?inputXml){???? ??
- ????????try{???? ??
- ????????????//使用?SAXReader?解析?XML?文檔?catalog.xml???? ??
- ????????????SAXReader?saxReader?=?new?SAXReader();???? ??
- ????????????Document?document?=?saxReader.read(inputXml);???? ??
- ???????????????? ??
- ????????????//使用?XPath?表達式從?article?元素中獲得?level?節(jié)點列表。如果?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");????? ??
- ????????????}???? ??
- ???????????????? ??
- ????????????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");???? ??
- ????????????}???? ??
- ???????????????? ??
- ????????????//獲取?article?元素列表,從?article?元素中的?title?元素得到一個迭代器,并修改?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");???? ??
- ????????????????}???? ??
- ????????????}???? ??
- ???????????????? ??
- ???????????????? ??
- ????????????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");???? ??
- ????????????????}???? ??
- ????????????}???? ??
- ???????????????? ??
- ????????????FileOutputStream?fos=new?FileOutputStream("D:/eclipse3.2/workspace/WebServices/src/com/nikee/dom4j/catalog-modified.xml");???? ??
- ????????????OutputFormat?of=new?OutputFormat("???",true);???? ??
- ????????????XMLWriter?xw=new?XMLWriter(fos,?of);???? ??
- ????????????xw.write(?document?);???? ??
- ????????????xw.close();???? ??
- ????????}???? ??
- ????????catch(DocumentException?e)???? ??
- ????????{???? ??
- ????????????e.printStackTrace();???? ??
- ????????}???? ??
- ????????catch(IOException?e){???? ??
- ????????????e.printStackTrace();???? ??
- ????????}???? ??
- ????}???? ??
- ???????? ??
- ????public?static?void?main(String?argv[]){???? ??
- ????????Dom4Jparser?dom4jParser=new?Dom4Jparser();???? ??
- ????????dom4jParser.modifyDocument(new?File("D:/eclipse3.2/workspace/WebServices/src/com/nikee/dom4j/catalog.xml"));???? ??
- ????}???? ??
- }???? ??
xml 代碼
- <!--sp--> xml? version = "1.0" ? encoding = "UTF-8" ?> ??? ??
- ??? ??
- <!--<!DOCTYPE?catalog?PUBLIC?"nikee"?"file://c:/Dtds/catalog.dtd">--> ??? ??
- < catalog > ??? ??
- <!--An?XML?Catalog--><!--sp--> target?text ?> ??? ??
- ? < journal ? title = "XML?Zone" ? publisher = "IBM?developerWorks" > ????? ??
- ??? < article ? level = "Introductory" ? date = "October-2002" > ??????? ??
- ?????? < title > Create?flexible?and?extensible?XML?schematitle > ??? ??
- ?????? < author > ???????? ??
- ????????? < firstname > Ayeshafirstname > ??? ??
- ????????? < lastname > Maliklastname > ??? ??
- ??????author > ??? ??
- ???article > ??? ??
- ?journal > ??? ??
- catalog > ????
問題總結(jié):
1.當catalog.xml有<!--CTYPE catalog PUBLIC "nikee" "file://c:/Dtds/catalog.dt-->這一句,Dom4Jparser.java修改 XML 文檔 catalog.xml為catalog-modified.xml時,發(fā)生異常org.dom4j.DocumentException: c Nested exception: c.
原因:我自己沒有此 file://c:/Dtds/catalog.dtd .
解決辦法:刪掉此行.
2.除了加入dom4j.jar包外,必須加入jaxen.jar包,否則會發(fā)生異常.jaxen包可在 http://jaxen.org/ 下載.