-->
<web-app>
??? <servlet>
??????? <servlet-name>snoop</servlet-name>
??????? <servlet-class>SnoopServlet</servlet-class>
??? </servlet>
??? <servlet>
??????? <servlet-name>file </servlet-name>
??????? <servlet-class>ViewFile</servlet-class>
??????? <init-param>
??????????? <param-name>initial</param-name>
??????????? <param-value>1000</param-value>
??????????? <description>The initial value for the counter? <!-- optional --></description>
??????? </init-param>
??? </servlet>
??? <servlet-mapping>
??????? <servlet-name>mv</servlet-name>
??????? <url-pattern>*.wm</url-pattern>
??? </servlet-mapping>
??? <distributed/>
??? <security-role>
????? <role-name>manager</role-name>
????? <role-name>director</role-name>
????? <role-name>president</role-name>
??? </security-role>
</web-app>
處理程序:
import java.io.*;
import java.util.*;?
public class XPathReader {?????
??? public static void main(String[] args) throws IOException, JDOMException {
??????? if (args.length != 1) {
??????????? System.err.println("Usage: java XPathReader web.xml");
??????????? return;
??????? }
??????? String filename = args[0];//從命令行輸入web.xml
??????? PrintStream out = System.out;
??????? SAXBuilder builder = new SAXBuilder();
??????? Document doc = builder.build(new File(filename));//得到Document對象
?
?
??????? // Print servlet information
??????? XPath servletPath = XPath.newInstance("http://servlet");//,選擇任意路徑下servlet元素
??????? List servlets = servletPath.selectNodes(doc);//返回所有的servlet元素。
??????? out.println("This WAR has "+ servlets.size() +" registered servlets:");
??????? Iterator i = servlets.iterator();
??????? while (i.hasNext()) {//輸出servlet信息
??????????? Element servlet = (Element) i.next();
??????????? out.print("\t" + servlet.getChild("servlet-name")
??????????????????????????????????? .getTextTrim() +
????????????????????? " for " + servlet.getChild("servlet-class")
?????????????????????????????????????? .getTextTrim());
??????????? List initParams = servlet.getChildren("init-param");
??????????? out.println(" (it has " + initParams.size() + " init params)");?
??????? }?????????????
??????? // Print security role information
??????? XPath rolePath = XPath.newInstance("http://security-role/role-name/text()");
??????? List roleNames = rolePath.selectNodes(doc);//得到所有的角色名
??????? if (roleNames.size() == 0) {
??????????? out.println("This WAR contains no roles");
??????? } else {
??????????? out.println("This WAR contains " + roleNames.size() + " roles:");
??????????? i = roleNames.iterator();
??????????? while (i.hasNext()) {//輸出角色名
??????????????? out.println("\t" + ((Text)i.next()).getTextTrim());
??????????? }
??????? }
??? }????
}
?
?
輸出結果:
C:\java>java?? XPathReader web.xml
This WAR has 2 registered servlets:
??????? snoop for SnoopServlet (it has 0 init params)
??????? file for ViewFile (it has 1 init params)
This WAR contains 3 roles:
??????? manager
??????? director
??????? president
?
?
6、數據輸入要用到XML文檔要通過org.jdom.input包,反過來需要org.jdom.output。如前面所說,關是看API文檔就能夠使用。
我們的例子讀入XML文件exampleA.xml,加入一條處理指令,修改第一本書的價格和作者,并添加一條屬性,然后寫入文件exampleB.xml:
//exampleA.xml
<?xml version="1.0" encoding="GBK"?>
<bookList>
<book>
<name>Java編程入門</name>
<author>張三</author>
<publishDate>2002-6-6</publishDate>
<price>35.0</price>
</book>
<book>
<name>XML在Java中的應用</name>
<author>李四</author>
<publishDate>2002-9-16</publishDate>
<price>92.0</price>
</book>
</bookList>
//testJDOM.java
import org.jdom.*;
import org.jdom.output.*;
import org.jdom.input.*;
import java.io.*;
public class TestJDOM{
public static void main(String args[])throws Exception{
SAXBuilder sb = new SAXBuilder();
//從文件構造一個Document,因為XML文件中已經指定了編碼,所以這里不必了
Document doc = sb.build(new FileInputStream("exampleA.xml"));
ProcessingInstruction pi = new ProcessingInstruction//加入一條處理指令
("xml-stylesheet","href=\"bookList.html.xsl\" type=\"text/xsl\"");
doc.addContent(pi);
Element root = doc.getRootElement(); //得到根元素
java.util.List books = root.getChildren(); //得到根元素所有子元素的集合
Element book = (Element)books.get(0); //得到第一個book元素
//為第一本書添加一條屬性
Attribute a = new Attribute("hot","true");
book.setAttribute(a);
Element author = book.getChild("author"); //得到指定的字元素
author.setText("王五"); //將作者改為王五
//或 Text t = new Text("王五");book.addContent(t);
Element price = book.getChild("price"); //得到指定的字元素
//修改價格,比較郁悶的是我們必須自己轉換數據類型,而這正是JAXB的優勢
author.setText(Float.toString(50.0f));
String indent = " ";
boolean newLines = true;
XMLOutputter outp = new XMLOutputter(indent,newLines,"GBK");
outp.output(doc, new FileOutputStream("exampleB.xml"));
}
};
執行結果exampleB.xml:
<?xml version="1.0" encoding="GBK"?>
<bookList>
<book hot=”true”>
<name>Java編程入門</name>
<author>50.0</author>
<publishDate>2002-6-6</publishDate>
<price>35.0</price>
</book>
<book>
<name>XML在Java中的應用</name>
<author>李四</author>
<publishDate>2002-9-16</publishDate>
<price>92.0</price>
</book>
</bookList>
<?xml-stylesheet href="bookList.html.xsl" type="text/xsl"?>
在默認情況下,JDOM的Element類的getText()這類的方法不會過濾空白字符,如果你需要過濾,用setTextTrim() 。