獲取Document對象的幾種基本方式:java
1>讀取XML文件,獲取Document對象node
SAXReader reader = new SAXReader(); Document document = reader.read(new File("csdn.xml"));
2>解析XML格式的文本獲取Document對象spring
String text = "<csdn></csdn>"; Document document = DocumentHelper.parseText(text);
3>本身建立Document對象this
Document document = DocumentHelper.createDocument(); //建立根節點 Element root = document.addElement("csdn");
document對象中的基本方法:
編碼
getRootElement() --Returns the root Elementfor this document
spa
setRootElement(Element rootElement) --Sets the root element for this document getXMLEncoding() element屬性方法:code
addAttribute(String name, String value) --Adds the attribute value of the given local nameorm
addText(String text)--Adds a new Text node with the given text to this elementxml
attribute(int index) --Returns the attribute at the specified indexGets the對象
attribute(String name) --Returns the attribute with the given name
attributes() --Returns the Attributeinstances this element contains as a backed Listso that the attributes may be modified directly using the Listinterface.
attributeValue(String name) --This returns the attribute value for the attribute with the given name and any namespace or null if there is no such attribute or the empty string if the attribute value is empty.
element(String name) --Returns the first element for the given local name and any namespace.
elementIterator() --Returns an iterator over all this elements child elements
elements() --Returns the elements contained in this element.
elements(String name) --Returns the elements contained in this element with the given local name and any namespace.
remove(Attribute attribute) -Removes the given Attribute from this element.
setAttributeValue(String name, String value) --Deprecated. As of version 0.5. Please use addAttribute(String,String) instead
基本方法:
1.獲取文檔的根節點. Element root = document.getRootElement(); 2.取得某個節點的子節點. Element element=node.element(「四大名著"); 3.取得節點的文字 String text=node.getText(); 4.取得某節點下全部名爲「csdn」的子節點,並進行遍歷. List nodes = rootElm.elements("csdn"); for (Iterator it = nodes.iterator(); it.hasNext();) { Element elm = (Element) it.next(); // do something } 5.對某節點下的全部子節點進行遍歷. for(Iterator it=root.elementIterator();it.hasNext();){ Element element = (Element) it.next(); // do something } 6.在某節點下添加子節點 Element elm = newElm.addElement("朝代"); 7.設置節點文字. elm.setText("明朝"); 8.刪除某節點.//childElement是待刪除的節點,parentElement是其父節點 parentElement.remove(childElment); 9.添加一個CDATA節點.Element contentElm = infoElm.addElement("content");contentElm.addCDATA(「cdata區域」); 節點對象的屬性方法操做 1.取得某節點下的某屬性 Element root=document.getRootElement(); //屬性名name Attribute attribute=root.attribute("id"); 2.取得屬性的文字 String text=attribute.getText(); 3.刪除某屬性 Attribute attribute=root.attribute("size"); root.remove(attribute); 4.遍歷某節點的全部屬性 Element root=document.getRootElement(); for(Iterator it=root.attributeIterator();it.hasNext();){ Attribute attribute = (Attribute) it.next(); String text=attribute.getText(); System.out.println(text); } 5.設置某節點的屬性和文字. newMemberElm.addAttribute("name", "sitinspring"); 6.設置屬性的文字 Attribute attribute=root.attribute("name"); attribute.setText("csdn");
將document對象寫入文檔中:
1.文檔中全爲英文,不設置編碼,直接寫入的形式. XMLWriter writer = new XMLWriter(new FileWriter("ot.xml")); writer.write(document); writer.close(); 2.文檔中含有中文,設置編碼格式寫入的形式. OutputFormat format = OutputFormat.createPrettyPrint();// 建立文件輸出的時候,自動縮進的格式 format.setEncoding("UTF-8");//設置編碼 XMLWriter writer = new XMLWriter(newFileWriter("output.xml"),format); writer.write(document); writer.close();