JDOM生成、解析XML實例代碼
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;import org.jdom.Attribute; import org.jdom.Comment; import org.jdom.Document; import org.jdom.Element; import org.jdom.JDOMException; import org.jdom.input.SAXBuilder; import org.jdom.output.Format; import org.jdom.output.XMLOutputter; /** * * jdom生成與解析XML文檔 * */ public class JdomDemo{ Document document = new Document(); /** * 利用JDom進行xml文檔的寫入操作 */ public void createXml(File file) { // 1.創建元素 及 設置為根元素 Element employees = newElement("employees"); document.setContent(employees); // 2.創建注釋 及 設置到根元素上 Comment commet = new Comment("thisis my comment"); employees.addContent(commet); // 3.創建元素 Element element1 = newElement("employee"); // 3.1 設置元素的屬性名及屬性值 element1.setAttribute(newAttribute("id", "0001")); // 3.2 創建元素的屬性名及屬性值 Attribute nameAttr = newAttribute("name", "wanglp"); // 3.3 設置元素名及文本 Element sexEle = newElement("sex"); sexEle.setText("m"); // 設置到上層元素上 element1.addContent(sexEle); // 設置元素 Element ageEle = newElement("age"); ageEle.setText("22"); element1.addContent(ageEle); // 設置為根元素的子元素 employees.addContent(element1); // 將元素屬性設置到元素上 element1.setAttribute(nameAttr); // 3.創建元素 Element element2 = newElement("employee"); // 3.1 設置元素的屬性名及屬性值 element2.setAttribute(newAttribute("id", "0002")); // 3.2 創建元素的屬性名及屬性值 Attribute name2Attr = newAttribute("name", "fox"); // 3.3 設置元素名及文本 Element sex2Ele = newElement("sex"); sex2Ele.setText("f"); // 設置到上層元素上 element2.addContent(sex2Ele); // 設置元素 Element age2Ele = newElement("age"); age2Ele.setText("21"); element2.addContent(age2Ele); // 設置為根元素的子元素 employees.addContent(element2); // 將元素屬性設置到元素上 element2.setAttribute(name2Attr); Element element3 = new Element("employee"); element3.setText("title"); element3.addContent(newElement("name").addContent(new Element("hello"))); employees.addContent(element3); // 設置xml文檔輸出的格式 Format format =Format.getPrettyFormat(); XMLOutputter out = newXMLOutputter(format); // 將得到的xml文檔輸出到文件流中 try { out.output(document, newFileOutputStream(file)); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 利用JDom進行xml文檔的讀取操作 */ public void parserXml(File file) { // 建立解析器 SAXBuilder builder = new SAXBuilder(); try { // 將解析器與文檔關聯 document = builder.build(file); } catch (JDOMException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } // 讀取根元素 Element root =document.getRootElement(); // 輸出根元素的名字 System.out.println("<" +root.getName() + ">"); // 讀取元素集合 List<?> employeeList =root.getChildren("employee"); for (int i = 0; i <employeeList.size(); i++) { Element ele = (Element) employeeList.get(i); // 得到元素的名字 System.out.println("<"+ ele.getName() + ">"); // 讀取元素的屬性集合 List<?> empAttrList =ele.getAttributes(); for (int j = 0; j <empAttrList.size(); j++) { Attribute attrs = (Attribute)empAttrList.get(j); // 將屬性的名字和值 并 輸出 String name = attrs.getName(); String value = (String)attrs.getValue(); System.out.println(name +"=" + value); } try { Element sex =ele.getChild("sex"); System.out.println("<sex>" + sex.getText()); Element age =ele.getChild("age"); System.out.println("<age>" + age.getText()); } catch (NullPointerException e) { System.out.println(ele.getTextTrim()); Element name =ele.getChild("name"); System.out.println("<name>" + name.getName()); } System.out.println("</employee>"); } System.out.println("</employees>"); } /** * 測試 */ public static void main(String[] args) { JdomDemo jdom = new JdomDemo(); File file = newFile("E://jdom.xml"); jdom.createXml(file); jdom.parserXml(file); } } </pre>
本文由用戶 gcd8 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!