Common XPath APIs

Source: Internet
Author: User

Use of xpath APIs
XPath expressions are much easier to write than tedious Document Object Model (DOM) code. To extract information from an XML document, the quickest and easiest way is to embed an XPATH expression in a Java program. The javax. xml. XPath package is introduced in the Java version, which is a library used for xpath document query independent of the XML object model.
Common XPath API classes and interfaces are as follows:
XPath Interface
Xpathfactory class
Xpathvariableresolver interface and xpathfunctionresolver Interface
Xpathexpression Interface
The xpathvariableresolver interface and xpathfunctionresolver interface are mainly used to define and use XPath extension functions in programs.
Xpathfactory class
The xpathfactory instance can be used to create an XPATH object. This class has only one protected empty constructor. The following methods are commonly used:
Abstract void setxpathfunctionresolver (xpathfunctionresolver resolver): Creates a default function parser.
Static xpathfactory newinstance (): gets the new xpathfactory instance using the default Object Model (DOM.
Abstract void setxpathvariableresolver (xpathvariableresolver resolver): Creates a default variable parser.
Abstract Boolean isobjectmodelsupported (string objectmodel): whether the xpathfactory supports the specified object model.
In actual applications, the newinstance () static method is often used to obtain an instance of this class,
XPath Interface
Provides access to the XPath computing environment and expressions. XPath objects are not thread-safe and cannot be loaded repeatedly. That is to say, the application is responsible for ensuring that no threads can use one XPath object at any time.
There are several common methods:
Void reset (): resets this XPath to its initial configuration.
Xpathexpression compile (string expression): compile an XPATH expression.
Void setxpathfunctionresolver (xpathfunctionresolver resolver): Creates a function parser.
Void setnamespacecontext (namespacecontext nscontext): Create a namespace context.
Void setxpathvariableresolver (xpathvariableresolver resolver): Creates a variable parser.
Xpathexpression Interface
The xpathexpression interface provides access to compiled XPath expressions. If the expression contains a variable, the value is found through the xpathvariableresolver () method. If the variable parser is undefined or the parser returns NULL, an exception is thrown. If the expression contains a function reference, the function is found through the xpathfunctionresolver () method. If the function parser is undefined or the parser returns NULL for the function, an exception is thrown.
The longest method used for this interface is the evaluate () method, as follows:
String evaluate (inputsource source): calculates the compiled XPath expression of the specified inputsource and returns the result as a string.
String evaluate (Object item): calculates the specified compiled XPath expression and returns it as a string.
Object evaluate (Object item, QNAME returntype): calculates the specified XPath expression and returns the results of the specified type.
Object evaluate (inputsource source, QNAME returntype): calculates the XPath expression compiled by the specified inputsource and returns the result of the specified type.
XPath API usage example
Import javax. xml. parsers .*;
Import javax. xml. XPath .*;
Import org. W3C. Dom .*;
Public class testxpath2 {
Public static void main (string [] ARGs) throws exception {
Documentbuilderfactory domfactory =
Documentbuilderfactory. newinstance ();
Documentbuilder builder = domfactory. newdocumentbuilder ();
Document Doc = builder. parse ("cdcatalog. xml ");
// Obtain an xpathfactory instance
Xpathfactory factory = xpathfactory. newinstance ();
XPath = factory. newxpath ();
// Use the XPath Function
Xpathexpression expr = XPath. Compile ("sum (// CD/price )");
String result = expr. Evaluate (DOC );
System. Out. println (result );
}
}
Use the sum () function of the XPath function library in the XPath expression.
XPath Data Type
XPath 1.0 has only four basic data types:
Number (numeric)
Node-Set)
Boolean (Boolean)
String (string type)
The correspondence between XPath Data Types in Java is as follows:
When calculating an XPATH expression, use the evaluate () method of the xpathexpression interface. This method can have two parameters. The second parameter is used to specify the desired return type. The value of this parameter is a static field named in xpathconstants. As follows:
Xpathconstants. Boolean
Xpathconstants. nodeset
Xpathconstants. Number
Xpathconstants. String
Xpathconstants. String
Xpathconstants. node does not match the XPath type. It is mainly applicable when the result of an XPATH expression has only one node. If the XPath expression returns multiple nodes but the type is xpathconstants. node, the evaluate () method returns the first node in document order. If the result of an XPATH expression is an empty set but the specified type is xpathconstants. node, the evaluate () method returns NULL.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.