public class DomPath extends Object
For a complete description of the XPath language see XPath. DomPath uses this language to select nodes in a document.
Usage
You create a DomPath object with
DomPath dp = new DomPath("XPath expression", NamespaceMap);and use it either to retrieve all matching Nodes:
List resultNodes = dp.match(document);or to find the first matching Node:
Node result = dp.matchFirst(document);Some examples:
DomPath dp = new DomPath(".//chapter", NamespaceMap);will match all elements named "chapter"
DomPath dp = new DomPath(".//chapter/@title", NamespaceMap);will match all title attributes of all chapter elements
DomPath dp = new DomPath(".//chapter[@title = 'Introduction']", NamespaceMap);will match all chapter elements which have a title attribute with value "Introduction".
The NamespaceMap maps prefixes to namespace URIs. If you want to find all elements in a certain namespace, say "http://www.sap.com/test", you would associate a prefix, say "st", with the URI in the map:
Map nsmap = new HashMap(); nsmap.put("st", "http://www.sap.com/test"); DomPath dp = new DomPath(".//st:*", nsmap);would find all elements in the namespace "http://www.sap.com/test" in any supplied document. Note that the prefix you choose for the DomPath can be completely different from the prefix used in the matched document. The matching is done on the URI, not on the prefix.
There is a predefined, unmodifiable Map as constant in DomPath which already has some associations between prefixes and common namespace URIs. Multithreading DomPath objects are multithread-safe. So, if you have a fixed Xpath expression which you need to evaluate over and over again, make the DomPath object a static member of your class. Extension Functions not supported yet. Limitations The following lists all known deviations from XPath 1.0 standard: The axis "following", "following-sibling", "preceding", "preceding-sibling" and "namespace" are not supported. The node test "processing-instruction()" does not take a parameter. The function "id(...)" is not supported.
Copyright (c) SAP AG 2001-2003
Modifier and Type | Field and Description |
---|---|
static Map |
DEFAULT_NAMESPACE_BINDINGS
Default bindings of prefixes to namespace URIs for some common
namespaces
xml - http://www.w3.org/XML/1998/namespace
xmlns - http://www.w3.org/2000/xmlns/
xs - http://www.w3.org/2001/XMLSchema
xsi - http://www.w3.org/2001/XMLSchema-instance
D - DAV:
|
Constructor and Description |
---|
DomPath(String path,
Map namespaces)
Create a new DomPath with given XPath expression and
namespace mapping.
|
Modifier and Type | Method and Description |
---|---|
List |
delete(Node start)
Delete all matched Nodes, start from given element.
|
Node[] |
deleteNodes(Node start)
Delete all matched Nodes, start from given element.
|
List |
match(Node start)
Match the expression in the given document, start from given element.
|
boolean |
matches(Node start)
Return if node matches.
|
Node |
matchFirst(Node start)
Find the first Node which matches the expression.
|
Map |
matchFirstNames(Node start)
Matches the XPath expression against the start Node and returns
a mapping of the resulting nodes against their
Name s. |
Node[] |
matchNodes(Node start)
Match the expression in the given document, start from given element.
|
List |
matchOrDie(Node start,
int minOccur,
int maxOccur,
String message)
Like
match(org.w3c.dom.Node) , but checks for required size of result list
before returning it. |
Map |
matchUniqueNames(Node start)
Matches the XPath expression against the start Node and returns
a mapping of the resulting nodes against their
Name s. |
String |
toString()
Converts the DomPath to its String representation, merely useful
for debuggging.
|
public static final Map DEFAULT_NAMESPACE_BINDINGS
xml - http://www.w3.org/XML/1998/namespace xmlns - http://www.w3.org/2000/xmlns/ xs - http://www.w3.org/2001/XMLSchema xsi - http://www.w3.org/2001/XMLSchema-instance D - DAV:
public DomPath(String path, Map namespaces)
IllegalArgumentException
silently on
not-wellformed XPath expressions.path
- the XPath expression to match withnamespaces
- mapping of prefixes to namespace URIspublic List match(Node start)
start
- the Node from which to start matchingpublic List matchOrDie(Node start, int minOccur, int maxOccur, String message) throws DomPathAssertionFailedException
match(org.w3c.dom.Node)
, but checks for required size of result list
before returning it.start
- node from which to start matchingmessage
- no descriptionminOccur
- minimal required size of result listmaxOccur
- maximum allowed size of result listDomPathAssertionFailedException
- when number of results outside rangepublic Node[] matchNodes(Node start)
start
- the Node from which to start matchingpublic Node matchFirst(Node start)
start
- the Node to start the match fromnull
public Map matchUniqueNames(Node start) throws DomPathException
Name
s. If the Names are not unique
an expception is thrown.start
- Node to evalualte expression againstDomPathException
- if named mapping is not uniquepublic Map matchFirstNames(Node start)
Name
s. If the Names are not unique
the first occurance is returned in the Map.start
- Node to evalualte expression againstpublic List delete(Node start)
start
- the Node from which to start matchingpublic Node[] deleteNodes(Node start)
start
- the Node from which to start matchingpublic boolean matches(Node start)
start
- the Node to start the match fromAccess Rights |
---|
SC | DC | Public Part | ACH |
---|---|---|---|
[sap.com] KMC-CM
|
[sap.com] tc/km/frwk
|
api
|
EP-KM-CM
|
[sap.com] KMC-WPC
|
[sap.com] tc/kmc/wpc/wpcfacade
|
api
|
EP-PIN-WPC-WCM
|
Copyright 2018 SAP AG Complete Copyright Notice