JDOM 包 可以从 http://www.jdom.org/dist/binary/ 下载。
TestJDOM.java: ---------------------------------------------------------------------- package com.hanweb.jcms; import org.jdom.*; import org.jdom.output.*; import org.jdom.JDOMException; import org.jdom.input.SAXBuilder; import JAVA.io.IOException; import JAVA.util.*; /** * @author longware * @version 1.0 */ public class TestJDOM{ public static void main(String[] args) { TestJDOM jd = new TestJDOM(); //jd.write(); jd.read(); } public void read() { SAXBuilder builder = new SAXBuilder(); try { Document doc = builder.build("C:/longware.xml"); Element root = doc.getRootElement(); listChildren(root, 0); System.out.println("XML well-formed."); } catch (JDOMException e) { System.out.println("XML not well-formed."); System.out.println(e.getMessage()); } catch (IOException e) { System.out.println("Could not check "); System.out.println(" because " + e.getMessage()); } } public static void listChildren(Element current, int depth) { printSpaces(depth); String eName = current.getName(); String id = eName.equals("SmsApp") ? "(id=" + current.getAttributeValue("id") + ")" : ""; String val = !eName.equals("SmsApp") ? "[" + current.getTextTrim() + "]" : ""; System.out.println(eName + id + val); List children = current.getChildren(); Iterator iterator = children.iterator(); while (iterator.hasNext()) { Element child = (Element) iterator.next(); listChildren(child, depth + 1); } } private static void printSpaces(int n) { for (int i = 0; i < n; i++) { System.out.print(' '); } } public void write() { Element WebService = new Element("WebService"); Element SmsApp = new Element("SmsApp"); SmsApp.setAttribute("id", "6"); Element WsURL = new Element("WsURL"); WsURL.setText("http://blog.csdn.net/ljcao/Rss.aspx"); Element UnitNo = new Element("UnitNo"); UnitNo.setText("0000"); Element Appid = new Element("Appid"); Appid.setText("8"); Element UserName = new Element("UserName"); UserName.setText("admin"); Element Password = new Element("Password"); Password.setText("123456"); Element OuterIP = new Element("OuterIP"); OuterIP.setText("222.111.93.1"); Element SmssType = new Element("SmssType"); SmssType.setText("1"); WebService.addContent(SmsApp); SmsApp.addContent(WsURL); SmsApp.addContent(UnitNo); SmsApp.addContent(Appid); SmsApp.addContent(UserName); SmsApp.addContent(Password); SmsApp.addContent(OuterIP); SmsApp.addContent(SmssType); XMLOutputter outputter = new XMLOutputter(); Format f = Format.getPrettyFormat(); f.setEncoding("GBK"); f.setIndent(" "); f.setLineSeparator("\r\n"); try { outputter.setFormat(f); outputter.output(WebService, System.out); } catch (IOException e) { System.err.println(e); } } } longware.xml: ---------------------------------------------------------------------- <?xml version="1.0" encoding="GBK"?> <WebService> <SmsApp id="6"> <WsURL>http://blog.csdn.net/ljcao/Rss.aspx</WsURL> <UnitNo>0000</UnitNo> <Appid>1</Appid> <UserName>admin</UserName> <Password>123456</Password> <OuterIP>222.23.56.1</OuterIP> <SmssType>1</SmssType> </SmsApp> <SmsApp id="8"> <WsURL>http://www.jdom.org/dist/binary/</WsURL> <UnitNo>0000</UnitNo> <Appid>2</Appid> <UserName>test</UserName> <Password>654467</Password> <OuterIP>111.136.43.1</OuterIP> <SmssType>0</SmssType> </SmsApp> </WebService>