用JDOM讀取XML示例


























































import org.jdom.*;
import org.jdom.input.*;
//import org.jdom.output.*;
import java.io.*;
import java.util.*;
public class Cute {
public static void main(String args[]) {
try {
/*
* 用無變元構(gòu)造函數(shù)構(gòu)造一個SAXBuilder對象, 用sax解析器從文件中構(gòu)造文檔,
* SAXBuilder偵聽sax事件并從內(nèi)存中建立一個相應(yīng)的文檔
*/
SAXBuilder sb = new SAXBuilder();
// 創(chuàng)建文檔
Document doc = sb.build(new FileInputStream("c:/ResultSet.xml"));
// 加入一條處理指令
ProcessingInstruction pi = new ProcessingInstruction(
"xml-stylesheet",
"href=\"bookList.html.xsl\" type=\"text/xsl\"");
// 把這條處理指令,加入文檔中
doc.addContent(pi);
// 獲得這個文檔的根元素
Element el = doc.getRootElement();
printElement(el);
/*
// 得到第一個子元素的子元素,卻完全忽略其內(nèi)容
Element book = (Element) ls.get(0);
// 給這個子元素添加一條屬性,
Attribute attr = new Attribute("hot", "true");
book.setAttribute(attr);
// 獲得這個元素的子元素(指定)以及其值
Element el2 = book.getChild("author");
// 輸出這個元素的值
System.out.println(el2.getName());
// 給這個元素的值改個名字
el2.setText("cute");
// 再獲得這個元素的子元素(指定)
Element el3 = book.getChild("price");
// 給這個值換個值
el3.setText(Float.toString(50.0f));
XMLOutputter xml = new XMLOutputter();
xml.output(doc, new FileOutputStream("cute.xml"));
*/
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
private static void printElement(Element el)
{
List ls = el.getContent();
Iterator i = ls.iterator();
while (i.hasNext()) {
Object o = i.next();
if (o instanceof Text)/*使用instanceof 來獲得所需要的內(nèi)容*/
{
Text t=(Text)o;
System.out.println("Text: " + t.getText());}
else if(o instanceof Attribute)
System.out.println("Attribute: " + o);
else if (o instanceof Element)
{
System.out.println("Element: " + ((Element) o).getName());
Element oe = (Element)o;
System.out.println("Value: "+oe.getValue());
}
}
}
}
import org.jdom.input.*;
//import org.jdom.output.*;
import java.io.*;
import java.util.*;
public class Cute {
public static void main(String args[]) {
try {
/*
* 用無變元構(gòu)造函數(shù)構(gòu)造一個SAXBuilder對象, 用sax解析器從文件中構(gòu)造文檔,
* SAXBuilder偵聽sax事件并從內(nèi)存中建立一個相應(yīng)的文檔
*/
SAXBuilder sb = new SAXBuilder();
// 創(chuàng)建文檔
Document doc = sb.build(new FileInputStream("c:/ResultSet.xml"));
// 加入一條處理指令
ProcessingInstruction pi = new ProcessingInstruction(
"xml-stylesheet",
"href=\"bookList.html.xsl\" type=\"text/xsl\"");
// 把這條處理指令,加入文檔中
doc.addContent(pi);
// 獲得這個文檔的根元素
Element el = doc.getRootElement();
printElement(el);
/*
// 得到第一個子元素的子元素,卻完全忽略其內(nèi)容
Element book = (Element) ls.get(0);
// 給這個子元素添加一條屬性,
Attribute attr = new Attribute("hot", "true");
book.setAttribute(attr);
// 獲得這個元素的子元素(指定)以及其值
Element el2 = book.getChild("author");
// 輸出這個元素的值
System.out.println(el2.getName());
// 給這個元素的值改個名字
el2.setText("cute");
// 再獲得這個元素的子元素(指定)
Element el3 = book.getChild("price");
// 給這個值換個值
el3.setText(Float.toString(50.0f));
XMLOutputter xml = new XMLOutputter();
xml.output(doc, new FileOutputStream("cute.xml"));
*/
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
private static void printElement(Element el)
{
List ls = el.getContent();
Iterator i = ls.iterator();
while (i.hasNext()) {
Object o = i.next();
if (o instanceof Text)/*使用instanceof 來獲得所需要的內(nèi)容*/
{
Text t=(Text)o;
System.out.println("Text: " + t.getText());}
else if(o instanceof Attribute)
System.out.println("Attribute: " + o);
else if (o instanceof Element)
{
System.out.println("Element: " + ((Element) o).getName());
Element oe = (Element)o;
System.out.println("Value: "+oe.getValue());
}
}
}
}