(一)創建Document的基本操作
/** * XML基本操作 */ public void BaseOperation(){ //創建一個document Document document=DocumentHelper.createDocument(); //創建根結點 Element root=document.addElement("root"); //為根結點添加一個book節點 Element book1=root.addElement("book"); //為book1添加屬性type book1.addAttribute("type","science"); //為book1添加name子節點 Element name1=book1.addElement("Name"); //并設置其name為"Java" name1.setText("Java"); //為book1創建一個price節點,并設其價格為100 book1.addElement("price").setText("100");//為根結點添加第二個book節點,并設置該book節點的type屬性 Element book2=root.addElement("book").addAttribute("type","science"); //為book1添加name子節點 Element name2=book2.addElement("Name"); //并設置其name為"Oracle" name2.setText("Oracle"); //為book1創建一個price節點,并設其價格為200 book2.addElement("price").setText("200");//輸出xml System.out.println(document.asXML()); }
調用BaseOperation,輸出結果為:
<?xml version="1.0" encoding="UTF-8"?> <root> <book type="science"> <Name>Java</Name> <price>100</price> </book> <book type="science"> <Name>Oracle</Name> <price>200</price> </book> </root>
(二)根據一個符合Document格式的字符串來生成一個Document
/**將字符串轉化為Document * @param str 輸入的字符串 * @return 生成的document * @throws DocumentException */ public Document parserStrtoDocument(String str) throws DocumentException{Document document=DocumentHelper.parseText(str);return document; }
調用示例:
String str="<root><book type='science'><Name>Java</Name><price>100</price></book></root>"; Document document = parserStrtoDocument(str); System.out.println(document.asXML());
輸出結果為:
<?xml version="1.0" encoding="UTF-8"?> <root> <book type="science"> <Name>Java</Name> <price>100</price> </book> </root>
(三)取得xml節點屬性的基本方法
/** * 取得xml的節點和屬性的值 * @throws DocumentException */ public void getBaseInfofromDocument() throws DocumentException{ String str="<root><book type='science'><Name>Java</Name><price>100</price></book></root>"; //生成一個Document Document document = DocumentHelper.parseText(str); //取得根結點 Element root=document.getRootElement(); //取得book節點 Element book=root.element("book"); //取得book節點的type屬性的值 String type=book.attributeValue("type"); //取得Name節點 Element name=book.element("Name"); //取得書名 String bookname=name.getText(); //取得書的價錢 int price=Integer.parseInt(book.element("price").getText());//輸出書目信息 System.out.println("書名:" bookname); System.out.println("所屬類別:" type); System.out.println("價格:" price); }
調用getBaseInfofromDocument,輸出結果為:
書名:Java
所屬類別:science
價格:100
(四)利用迭代,xpath取得節點及其屬性值
/**利用迭代,xpath取得xml的節點及其屬性值 * @throws DocumentException */ public void getComplexInfofromDocument() throws DocumentException{String str="<root><book type='science'><Name>Java</Name><price>100</price></book>" "<book type='science'><Name>Oracle</Name><price>120</price></book>""<book type='society'><Name>Society security</Name><price>130</price></book>" "<author><name>chb</name></author></root>"; //生成一個Document Document document = DocumentHelper.parseText(str);//提取類型為"society"的書 //此處需要添加支持xpath的jar包,詳細見備注 Element society_book=(Element)document.selectSingleNode("/root/book[@type='society']"); System.out.println(society_book.asXML());//提取價格節點的列表 System.out.println("-----------價格列表-------------"); List price=document.selectNodes("//price"); for(int i=0;i<price.size();i ){ Element elem_price=(Element)price.get(i); System.out.println(elem_price.getText()); }//循環根結點下的所有節點,若當前節點為book,則輸出這本書的詳細信息 System.out.println("-------------書目詳情------------"); System.out.println("書名\t\t類別\t\t價格"); Element root=document.getRootElement(); Iterator iterator=root.elementIterator(); while(iterator.hasNext()){ Element element=(Element)iterator.next(); if(element.getName().equals("book")){ System.out.print(element.element("Name").getText() "\t"); System.out.print(element.attributeValue("type") "\t\t"); System.out.print(element.element("price").getText() "\n"); } }//查找作者姓名 Element author=(Element)document.selectSingleNode("//author"); System.out.println("---------" author.element("name").getText() "----------"); //提取作者的所有書目名稱 Iterator iterator_book=root.elementIterator("book"); while(iterator_book.hasNext()){ Element book=(Element)iterator_book.next(); System.out.print(book.element("Name").getText() "\t"); }//屬性迭代 System.out.println("\n-------屬性迭代--------"); String str1="<book type='science' name='Java' price='100'/>"; Document document1=DocumentHelper.parseText(str1); //開始迭代 Iterator iterator_attribute=document1.getRootElement().attributeIterator(); while(iterator_attribute.hasNext()){ //提取當前屬性 Attribute attribute=(Attribute)iterator_attribute.next(); System.out.println(attribute.getName() ":" attribute.getValue()); } }
調用getComplexInfofromDocument,輸出結果為:
<book type="society"><Name>Society security</Name><price>130</price></book> -----------價格列表------------- 100 120 130 -------------書目詳情------------ 書名 類別 價格 Java science 100 Oracle science 120 Society security society 130 ---------chb---------- Java Oracle Society security -------屬性迭代-------- type:science name:Java price:100
備注:調用該方法之前,應該先向工程中添加支持xpath的jar包,否則,會出現以下錯誤:
java.lang.NoClassDefFoundError: org/jaxen/JaxenException
at org.dom4j.DocumentFactory.createXPath(DocumentFactory.java:230)
at org.dom4j.tree.AbstractNode.createXPath(AbstractNode.java:207)
at org.dom4j.tree.AbstractNode.selectSingleNode(AbstractNode.java:183)
at xml_chb.dom4j_chb.getComplexInfofromDocument(dom4j_chb.java:82)
at xml_chb.dom4j_chb.main(dom4j_chb.java:92)
Exception in thread "main"