From Wikipedia, the free encyclopedia.
Table of contents<script type="text/javascript">showTocToggle("show","hide")</script> [showhide] |
|
This article explains how to use the XPath API of Xalan to access XML data from a Java application. This method requires only few lines of code, even if the criteria for selecting data are complex. When using J2SE 1.4, no external libraries are required.
XPath is a standard for addressing parts of an XML document. For example, the XPath expression
report[@severity="warning"][5] selects the fifth <report> element that has a severity attribute with value "warning".
Among other things, XPath is used in XSL Transformations (XSLT) to specify the content that a template should be applied to.
Xalan is an implementation of an XSLT processor. As such, it also implements XPath.
Xalan is included in J2SE 1.4.
Xalan exposes its XPath implementation through the API defined in the org.apache.xpath package. A good starting point for learning the API is the class XPathAPI.
In this example, we will create a small Java application that prints the article titles from Slashdot's RSS feed.
The Slashdot RSS feed can be found at http://slashdot.org/index.rss. After removing some parts that are not relevant for this example, it looks like this:
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns="http://purl.org/rss/1.0/"
xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel rdf:about="http://slashdot.org/">
<title>Slashdot</title>
<link>http://slashdot.org/</link>
<description>News for nerds, stuff that matters</description>
<items>
<rdf:Seq>
<rdf:li rdf:resource="http://slashdot.org/article.pl?sid=04/05/08/2254227" />
<rdf:li rdf:resource="http://slashdot.org/article.pl?sid=04/05/08/2210224" />
<rdf:li rdf:resource="http://slashdot.org/article.pl?sid=04/05/08/1747258" />
</rdf:Seq>
</items>
</channel>
<item rdf:about="http://slashdot.org/article.pl?sid=04/05/08/2254227">
<title>What's Being Done About Nuclear Security</title>
<link>http://slashdot.org/article.pl?sid=04/05/08/2254227</link>
<description>KrisCowboy writes "Wired.com has an interesting article ... </description>
<dc:subject>security</dc:subject>
</item>
<item rdf:about="http://slashdot.org/article.pl?sid=04/05/08/2210224">
<title>Cyber-Soap Returns From The Dead</title>
<link>http://slashdot.org/article.pl?sid=04/05/08/2210224</link>
<description>An anonymous reader submits "Back in 1995, an experimental ...</description>
<dc:subject>ent</dc:subject>
</item>
<item rdf:about="http://slashdot.org/article.pl?sid=04/05/08/1747258">
<title>Phatbot Author Arrested In Germany</title>
<link>http://slashdot.org/article.pl?sid=04/05/08/1747258</link>
<description>Tacito writes "After arresting the author of Sasser, the ...</description>
<dc:subject>security</dc:subject>
</item>
</rdf:RDF>
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.xpath.XPathAPI;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
public class XPathDemo {
// URL of Slashdot's RSS feed.
private static final String URL = "http://slashdot.org/index.rss";
// XPath expression that selects text content of titles of articles.
private static final String XPATH = "RDF/item/title/text()";
public static void main(String[] args) throws Exception {
// Parse feed into DOM tree.
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
Document feed = factory.newDocumentBuilder().parse(URL);
// Select article titles into DOM node list.
NodeList titles = XPathAPI.selectNodeList(feed, XPATH);
// Iterate over node list and print article titles.
for (int i = 0; i < titles.getLength(); i++) {
System.out.println(titles.item(i).getNodeValue());
}
}
}
The source code compiles with J2SE 1.4. No additional libraries are required.
What's Being Done About Nuclear Security
Cyber-Soap Returns From The Dead
Phatbot Author Arrested In Germany
First, the application parses the RSS document into a Document Object Model (DOM) tree. This is done using the J2SE classes DocumentBuilderFactory and DocumentBuilder.
Next, the application uses the Xalan method selectNodeList to select a subset of the DOM nodes. This method takes two inputs:
- The DOM node tree from which to select.
- The XPath expression (a string) that describes which nodes to select.
There are several static select methods in XPathAPI. They differ in the way they return the selected nodes and in the way they handle namespaces.
Lastly, the application iterates over the selected DOM nodes and prints their values.
In this example, we will print only the titles of those articles that have the "security" subject.
We only need to change one line of code:
// XPath expression selecting article titles.
private static final String XPATH = "RDF/item[subject='security']/title/text()";
What's Being Done About Nuclear Security
Phatbot Author Arrested In Germany
We were able to express the additional criterion "select only those articles with subject 'security'" by modifying the XPath expression. We did not need to change any Java code.
- Only few lines of Java code are required, because even complex selection criteria can be expressed in XPath, rather than Java code.
- All required classes are included in J2SE 1.4.
- The Xalan API is not officially part of J2SE 1.4. It is not guaranteed to be included in future J2SE releases.
分享到:
相关推荐
XalanCustomXpathSample 此示例演示如何使用 Xalan 开发自定义 Xpath
serializer.jar,xalan.jar,xalan-2.7.0.jar,xercesImpl.jar,xsltc.jarxml-apis.jar,解决tomcat和jdk1.6以上Provider org.apache.xalan.processor.TransformerFactoryImpl not found,或者 Error filterStart
单独构建struts2web应用程序的时候,如果没有spring的支持,需要这个包
包含翻译后的API文档:xalan-2.7.2-javadoc-API文档-中文(简体)版.zip; Maven坐标:xalan:xalan:2.7.2; 标签:xalan、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,...
包含翻译后的API文档:xalan-2.7.2-javadoc-API文档-中文(简体)-英语-对照版.zip; Maven坐标:xalan:xalan:2.7.2; 标签:xalan、中英对照文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index...
Xalan-Java 2.7.0-api
当项目报关于XML文件无法解析的错误, 将三个包拷贝到tomcat...Xerces是XML解析器,Xalan是格式化器,xml-apis实际上是JAXP。一般App Server都会带上,JDK1.4也包含了解析器,不过不是Xerces,是Crimson,效率比较差。
标签:apache-xml-xalan.jar.zip,apache,xml,xalan,jar.zip包下载,依赖包
serializer.jar、xalan.jar、xercesImpl.jar和xml-apis.jar Provider org.apache.xalan.processor.Transforme
xalan 系列最新全部jar包和文档 ...包括serializer.jar、xalan.jar、xercesImpl.jar、xml-apis.jar、xsltc.jar 解决Provider org.apache.xalan.processor.TransformerFactoryImpl not found错误。
serializer.jar、xalan.jar、xercesImpl.jar和xml-apis.jar相关jar包
xalan.jar xalan.jar
Xalan-C++ (named after a rare musical instrument) implements the W3C Recommendation 16 November 1999 XSL Transformations (XSLT) Version 1.0 and the XML Path Language (XPath) Version 1.0. XSLT is the ...
xml-apis.jar
Xalan XSL Transformer xalan jar 2.7.2 xalan jar 2.7.2 xalan jar 2.7.2
xalan.jarxalan.jarxalan.jarxalan.jar
本实例主要是通过json-libjar包中的工具类进行操作,简单实现了xml字符串和json字符串之间的转化,xml文件和json文件的转化。而且还兼容jsonObject和jsonArray两种格式,自己摸索,记录一下以便学习。
xalan 系列最新全部jar包...xalan jar包(包括serializer.jar、xalan.jar、xercesImpl.jar、xml-apis.jar、xsltc.jar) 可以用于解决: Provider org.apache.xalan.processor.TransformerFactoryImpl not found错误。
xalan系列jar包xalan系列jar包xalan系列jar包