`
chimae
  • 浏览: 25351 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

Using Xalan's XPath API to Access XML Data

阅读更多

From Wikipedia, the free encyclopedia.

Table of contents<script type="text/javascript">showTocToggle("show","hide")</script> [showhide]

Abstract

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

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

Xalan is an implementation of an XSLT processor. As such, it also implements XPath.

Xalan is included in J2SE 1.4.

Xalan XPath API

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.

Example 1

In this example, we will create a small Java application that prints the article titles from Slashdot's RSS feed.

XML Document

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>

Source Code

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());
}
}
}

Compilation

The source code compiles with J2SE 1.4. No additional libraries are required.

Output

What's Being Done About Nuclear Security
Cyber-Soap Returns From The Dead
Phatbot Author Arrested In Germany

Explanation

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.

Example 2

In this example, we will print only the titles of those articles that have the "security" subject.

Source Code

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()";

Output

What's Being Done About Nuclear Security
Phatbot Author Arrested In Germany

Explanation

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.

Discussion

Advantages

  • 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.

Disatvantages

  • The Xalan API is not officially part of J2SE 1.4. It is not guaranteed to be included in future J2SE releases.


分享到:
评论

相关推荐

    xalan_custom_xpath_sample:此示例演示如何使用 Xalan 开发自定义 Xpath

    在IT领域,XPath(XML Path Language)是一种在XML文档中查找信息的语言,它允许开发者选取XML节点,如元素、属性、文本等。Xalan是Apache软件基金会开发的一个开源项目,提供了一个高性能的Java实现的XPath和XSLT...

    xalan-2.7.0.jar.zip

    Xalan-Java是Apache软件基金会开发的一个开源项目,它是一个强大的XML转换工具,主要用于实现XPath和XSLT标准。在Java环境中,Xalan-Java扮演着至关重要的角色,特别是在XML文档的解析、转换和呈现过程中。标题中的...

    serializer.jar、xalan.jar、xercesImpl.jar和xml-apis.jar四个jar

    4. **xml-apis.jar**:XML APIs(Application Programming Interfaces)库提供了与XML处理相关的接口和类,这些接口和类是多个XML规范的抽象,如DOM、SAX、XPath等。它作为一个通用的API集合,使得开发者能够在不同...

    serializer.jar,xalan.jar,xalan-2.7.0.jar,xercesImpl.jar,xsltc.jarxml-apis.jar

    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

    xalan.jar,xml支持包

    单独构建struts2web应用程序的时候,如果没有spring的支持,需要这个包

    XML解析包(xalan.jar+xerces.jar+xml-apis.jar)

    当项目报关于XML文件无法解析的错误, 将三个包拷贝到tomcat...Xerces是XML解析器,Xalan是格式化器,xml-apis实际上是JAXP。一般App Server都会带上,JDK1.4也包含了解析器,不过不是Xerces,是Crimson,效率比较差。

    xalan-2.7.2-API文档-中文版.zip

    包含翻译后的API文档:xalan-2.7.2-javadoc-API文档-中文(简体)版.zip; Maven坐标:xalan:xalan:2.7.2; 标签:xalan、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,...

    apache-xml-xalan.jar.zip

    Apache XML Xalan是一款开源的Java库,用于执行XSLT转换。XSLT是一种XML语言,用于将XML文档转换为其他格式,如HTML、PDF或纯文本。在Java环境中,Xalan是实现这一功能的常见工具,它由Apache软件基金会维护。 `...

    xalan-2.7.2-API文档-中英对照版.zip

    包含翻译后的API文档:xalan-2.7.2-javadoc-API文档-中文(简体)-英语-对照版.zip; Maven坐标:xalan:xalan:2.7.2; 标签:xalan、中英对照文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index...

    xalan-2.5.0.jar

    Xalan是Apache软件基金会开发的一款XPath、XSLT处理器,它是基于W3C的标准实现,提供了对XML文档进行转换和样式化的能力。Xalan-Java版本(如2.5.0)允许开发者将XML数据转化为HTML、PDF或其他格式,以便于数据展示...

    JSTL_XML中的xalan.jar和serializer.jar

    - `&lt;x:eval&gt;`:执行XPath表达式,获取XML节点。 4. **集成与使用**: 在使用JSTL XML标签之前,这两个库需要添加到项目的类路径中。在传统的Web应用中,这通常意味着将`xalan.jar`和`serializer.jar`放入`WEB-INF...

    Xalan-Java 2.7.0-api

    Xalan-Java 2.7.0-api

    JSTL XML标签库

    &lt;x:transform xml="${xmlData}" xslt="path/to/xsl stylesheet.xsl"&gt; &lt;x:param name="param1"&gt;value1 ``` 总结来说,JSTL XML标签库结合了XercesImpl.jar和xalan.jar这两个强大的XML处理库,为JSP开发者提供了...

    xalan系列jar包

    4. **xml-apis.jar**: 这个JAR文件包含了XML相关的API接口,如DOM、SAX和XML Schema接口。它是不同XML处理库之间共享的接口定义,使得不同的实现(如Xerces和Xalan)可以协同工作。 5. **endorsed.txt**: 这个文件...

    Java版本的XPath方式解析jar和源代码

    Java提供了多种库来支持XPath的使用,如JAXP(Java API for XML Processing)和DOM4J等。 Java中的XPath工作原理是基于XML文档的DOM(Document Object Model)树,通过XPath表达式定位到DOM树上的特定节点。XPath...

    xalan.jar下载

    对于开发者来说,这意味着可以在Java应用程序、Web应用程序或者服务器端脚本中直接调用Xalan-J的API来执行复杂的XML转换任务。例如,通过以下简单的Java代码片段,我们可以使用Xalan-J进行XML到HTML的转换: ```...

    JAVA常用JAR包jdom.jar、jsf-api.jar、jsf-impl.jar、jstl-1.2.jar、saxpath.jar、xalan.jar、xerces.jar、xml-apis.jar包

    4. **saxpath.jar**:SAXPath是基于SAX(Simple API for XML)的XPath实现,它允许开发者通过XPath表达式在XML文档中查找节点。XPath是一种强大的查询语言,可以用来选取XML文档中的元素、属性或文本,而SAXPath则为...

    xalan jar 2.7.2

    Xalan-J是Apache软件基金会开发的一个开源项目,它提供了一种强大的工具,用于执行XSL(Extensible Stylesheet Language)转换,将XML文档转换为其他格式,如HTML、PDF或纯文本。Xalan-J 2.7.2是该项目的一个版本,...

    xalan_java_

    2. 加载XML和XSL:使用Java的DOM API(如DocumentBuilderFactory和DocumentBuilder)加载XML文档,使用XsltProcessor或TransformerFactory加载XSL样式表。 3. 进行转换:创建Transformer对象,将XML和XSL传递给...

    serializer.jar、xalan.jar、xercesImpl.jar和xml-apis.jar

    serializer.jar、xalan.jar、xercesImpl.jar和xml-apis.jar Provider org.apache.xalan.processor.Transforme

Global site tag (gtag.js) - Google Analytics