XML由於其可移植性,已經成為應用開發中必不可少的環節。我們經常會把應用程式的一些設定檔(屬性檔案)寫成XML的格式(當然,也可以用property檔案而不用XML檔案),應用程式通過XML的訪問類來對其進行操作。對XML進行操作可以通過若干種方法,如:SAX, DOM, JDOM, JAXP等,JDOM由於其比較簡單實用而被開發人員普遍使用。
本文主要分兩部分,第一部分介紹如何把XML檔案中的配置讀入應用程式中,第二部分介紹如何使用JDOM將配置輸出到XML檔案中。
以下是一段XML設定檔,檔案名稱為contents.xml
<?xml version="1.0"?><br /><book><br /> <title>Java and XML</title><br /> <contents><br /> <chapter title="Introduction"><br /> <topic>XML Matters</topic><br /> <topic>What's Important</topic><br /> <topic>The Essentials</topic><br /> <topic>What's Next?</topic><br /> </chapter><br /> <chapter title="Nuts and Bolts"><br /> <topic>The Basics</topic><br /> <topic>Constraints</topic><br /> <topic>Transformations</topic><br /> <topic>And More...</topic><br /> <topic>What's Next?</topic><br /> </chapter><br /> </contents><br /></book> 下面的程式通過使用JDOM中SAXBuilder類對contents.xml進行訪問操作,把各個元素顯示在輸出console上,程式名為:SAXBuilderTest.java
import java.io.File;<br />import java.util.Iterator;<br />import java.util.List;<br />import org.jdom.Document;<br />import org.jdom.Element;<br />import org.jdom.input.SAXBuilder;<br />public class SAXBuilderTest {<br /> private static String titlename;<br /> private String chapter;<br /> private String topic;<br /> public static void main(String[] args) {<br /> try {<br /> SAXBuilder builder = new SAXBuilder();<br /> Document document = builder.build(new File("contents.xml"));<br /> Element root = document. getRootElement_r();<br /> Element title = root. getChild_r("title");<br /> titlename = title. getText_r();<br /> System.out.println("BookTitle: " + titlename);<br /> Element contents = root. getChild_r("contents");<br /> List chapters = contents. getChildren_r("chapter");<br /> Iterator it = chapters.iterator();<br /> while (it.hasNext()) {<br /> Element chapter = (Element) it.next();<br /> String chaptertitle = chapter. getAttributeValue_r("title");<br /> System.out.println("ChapterTitle: " + chaptertitle);<br /> List topics = chapter. getChildren_r("topic");<br /> Iterator iterator = topics.iterator();<br /> while (iterator.hasNext()) {<br /> Element topic = (Element) iterator.next();<br /> String topicname = topic. getText_r();<br /> System.out.println("TopicName: " + topicname);<br /> }<br /> }<br /> } catch (Exception ex) {<br /> }<br /> }<br />} 程式首先初始化一個SAXBuilder對象,通過其build()方法構建Document對象。注意:JDOM本身沒有對XML檔案的分析器parser,它是利用JAXP中的parser進行XML分析的。
然後通過 getRootElement_r()獲得頂層的元素root(XML檔案中book標籤),繼而通過 getChild_r()擷取各元素,然後通過getText取得常值內容。 getChildren_r()得到的是List類型的對象,通過Iterator類對其進行迭代。程式啟動並執行結果如下:
BookTitle: Java and XML<br />ChapterTitle: Introduction<br />TopicName: XML Matters<br />TopicName: What's Important<br />TopicName: The Essentials<br />TopicName: What's Next?<br />ChapterTitle: Nuts and Bolts<br />TopicName: The Basics<br />TopicName: Constraints<br />TopicName: Transformations<br />TopicName: And More...<br />TopicName: What's Next? 經常,我們需要把應用程式裡的配置資料進行輸出到檔案中,下面舉例說明如何利用JDOM將配置輸出到XML檔案中。
下面的例子先把應用程式中要使用到的配置從一個設定檔中讀入,然後輸出到XML檔案中。
設定檔名為:conf.properties,內容如下
BookTitle = Java and XML<br />ChapterTitle = Introduction<br />TopicName = XML Matters 程式名為:XMLOutputterTest.java
import java.io.FileInputStream;<br />import java.io.FileOutputStream;<br />import java.util.Enumeration;<br />import java.util.Properties;<br />import org.jdom.Document;<br />import org.jdom.Element;<br />import org.jdom.output.XMLOutputter;<br />public class XMLOutputterTest {<br /> public static void main(String[] args) {<br /> try {<br /> FileInputStream inputstream = new FileInputStream("conf.properties");<br /> Properties prop = new Properties();<br /> prop.load(inputstream);<br /> Enumeration emu = prop.propertyNames();<br /> Element root = new Element("properties");<br /> root.addContent("/n");<br /> Document doc = new Document(root);<br /> while (emu.hasMoreElements()) {<br /> String propertyName = (String) emu.nextElement();<br /> String propertyValue = prop. getProperty_r(propertyName);<br /> Element element = new Element(propertyName);<br /> element.setText(propertyValue);<br /> root.addContent(element);<br /> root.addContent("/n");<br /> }<br /> XMLOutputter outputter = new XMLOutputter();<br /> FileOutputStream fileoutput = new FileOutputStream("output.xml");<br /> outputter.output(doc, fileoutput);<br /> } catch (Exception ex) {<br /> }<br /> }<br />} 程式先讀入conf.properties檔案,通過propertyNames()得出Enumeration迭代對象,進而獲得propertyName並得到propertyValue,建立Element類型root對象,並以此建立Document類對象,然後通過addContent()添加到root對象中。最後建立XMLOutputter類對象,通過output()輸出到XML檔案中。