Package org.appng.api
Class XPathProcessor
- java.lang.Object
-
- org.appng.api.XPathProcessor
-
public class XPathProcessor extends Object
AnXPathProcessor
is used the create aDocument
from anURL
orInputStream
and then to extractNodeList
s,Node
s,Element
s,Attr
ibutes etc. from thisDocument
.
It also allows to create newElement
s,Attr
ibutes,CDATASection
s andText
s.- Author:
- Matthias Müller
-
-
Constructor Summary
Constructors Constructor Description XPathProcessor(InputStream is)
Create a newXPathProcessor
from the givenInputStream
.XPathProcessor(String url)
Create a newXPathProcessor
from the given URL.XPathProcessor(URL url)
Create a newXPathProcessor
from the given URL.XPathProcessor(Document document)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Node
addAttribute(Node node, String name, String value)
Boolean
getBoolean(String xpathExpression)
Parses the givenxpathExpression
to retrieve aBoolean
.Boolean
getBoolean(Node node, String xpathExpression)
Parses the givenxpathExpression
to retrieve aBoolean
.Document
getDocument()
Element
getElement(String xpathExpression)
Parses the givenxpathExpression
to retrieve anElement
.Element
getElement(Node node, String xpathExpression)
Parses the givenxpathExpression
to retrieve anElement
.Node
getNode(String xpathExpression)
Parses the givenxpathExpression
to retrieve aNode
.Node
getNode(Node node, String xpathExpression)
Parses the givenxpathExpression
to retrieve aNode
.NodeList
getNodes(String xpathExpression)
Parses the givenxpathExpression
to retrieve aNodeList
.NodeList
getNodes(Node node, String xpathExpression)
Parses the givenxpathExpression
to retrieve aNodeList
.Number
getNumber(String xpathExpression)
Parses the givenxpathExpression
to retrieve aNumber
.Number
getNumber(Node node, String xpathExpression)
Parses the givenxpathExpression
to retrieve aNumber
.String
getString(String xpathExpression)
Parses the givenxpathExpression
to retrieve aString
.String
getString(Node node, String xpathExpression)
Parses the givenxpathExpression
to retrieve aString
.String
getXml()
void
getXml(OutputStream outputStream)
String
getXml(Node node)
Returns the XML-fragment represented by the givenNode
.String
getXml(NodeList nodes)
Returns the XML-fragment represented by the givenNodeList
.void
getXml(Node node, OutputStream outputStream)
Writes XML-fragment represented by the givenNode
into the givenOutputStream
.Attr
newAttribute(String name, String value)
Creates a newAttr
ibute.CDATASection
newCDATA(String tagName)
Creates a newCDATASection
.Element
newElement(String tagName)
Creates a newElement
.Text
newText(String tagName)
Creates a newText
.void
setNamespace(String prefix, String namespace)
Sets the namespace and the prefix to use for that namespace.
Example:
-
-
-
Constructor Detail
-
XPathProcessor
public XPathProcessor(String url) throws IOException
Create a newXPathProcessor
from the given URL.- Parameters:
url
- the URL- Throws:
IOException
- if an error occurs while reading the XML-document from the URL
-
XPathProcessor
public XPathProcessor(URL url) throws IOException
Create a newXPathProcessor
from the given URL.- Parameters:
url
- the URL- Throws:
IOException
- if an error occurs while reading the XML-document from theURL
-
XPathProcessor
public XPathProcessor(InputStream is) throws IOException
Create a newXPathProcessor
from the givenInputStream
.- Parameters:
is
- theInputStream
- Throws:
IOException
- if an error occurs while reading the XML-document from theInputStream
-
XPathProcessor
public XPathProcessor(Document document) throws IOException
- Throws:
IOException
-
-
Method Detail
-
setNamespace
public void setNamespace(String prefix, String namespace)
Sets the namespace and the prefix to use for that namespace.
Example:XPathProcessor xpath = ...; xpath.setNamespace("appng", "http://www.appng.org/schema/platform"); xpath.getNode("/appng:platform//appng:action[@id=\"foo\"]")
- Parameters:
prefix
- the prefixnamespace
- the namespace
-
getXml
public String getXml(Node node)
Returns the XML-fragment represented by the givenNode
.- Parameters:
node
- theNode
- Returns:
- the XML-fragment
-
getXml
public void getXml(Node node, OutputStream outputStream)
Writes XML-fragment represented by the givenNode
into the givenOutputStream
.- Parameters:
node
- theNode
outputStream
- theOutputStream
-
getXml
public void getXml(OutputStream outputStream)
- Parameters:
outputStream
- theOutputStream
.
-
getXml
public String getXml(NodeList nodes)
Returns the XML-fragment represented by the givenNodeList
.- Parameters:
nodes
- theNodeList
- Returns:
- the XML-fragment
-
getString
public String getString(String xpathExpression)
Parses the givenxpathExpression
to retrieve aString
.- Parameters:
xpathExpression
- the xpath-expression- Returns:
- the
String
retrieved from thexpathExpression
(may benull
), ornull
if the expression could not be parsed.
-
getString
public String getString(Node node, String xpathExpression)
Parses the givenxpathExpression
to retrieve aString
.
-
getBoolean
public Boolean getBoolean(String xpathExpression)
Parses the givenxpathExpression
to retrieve aBoolean
.- Parameters:
xpathExpression
- the xpath-expression- Returns:
- the
Boolean
retrieved from thexpathExpression
(may benull
), ornull
if the expression could not be parsed.
-
getBoolean
public Boolean getBoolean(Node node, String xpathExpression)
Parses the givenxpathExpression
to retrieve aBoolean
.
-
getNumber
public Number getNumber(String xpathExpression)
Parses the givenxpathExpression
to retrieve aNumber
.- Parameters:
xpathExpression
- the xpath-expression- Returns:
- the
Number
retrieved from thexpathExpression
(may benull
), ornull
if the expression could not be parsed.
-
getNumber
public Number getNumber(Node node, String xpathExpression)
Parses the givenxpathExpression
to retrieve aNumber
.
-
getNode
public Node getNode(String xpathExpression)
Parses the givenxpathExpression
to retrieve aNode
.- Parameters:
xpathExpression
- the xpath-expression- Returns:
- the
Node
retrieved from thexpathExpression
(may benull
), ornull
if the expression could not be parsed.
-
getNode
public Node getNode(Node node, String xpathExpression)
Parses the givenxpathExpression
to retrieve aNode
.
-
getElement
public Element getElement(String xpathExpression)
Parses the givenxpathExpression
to retrieve anElement
.- Parameters:
xpathExpression
- the xpath-expression- Returns:
- the
Element
retrieved from thexpathExpression
(may benull
), ornull
if the expression could not be parsed.
-
getElement
public Element getElement(Node node, String xpathExpression)
Parses the givenxpathExpression
to retrieve anElement
.
-
getNodes
public NodeList getNodes(String xpathExpression)
Parses the givenxpathExpression
to retrieve aNodeList
.- Parameters:
xpathExpression
- the xpath-expression- Returns:
- the
NodeList
retrieved from thexpathExpression
(may benull
), ornull
if the expression could not be parsed.
-
getNodes
public NodeList getNodes(Node node, String xpathExpression)
Parses the givenxpathExpression
to retrieve aNodeList
.
-
newAttribute
public Attr newAttribute(String name, String value)
Creates a newAttr
ibute.- Parameters:
name
- the name of the attributevalue
- the value of the attribute- Returns:
- the new
Attr
ibute.
-
newElement
public Element newElement(String tagName)
Creates a newElement
.- Parameters:
tagName
- the tag-name for the element- Returns:
- the new
Element
-
newCDATA
public CDATASection newCDATA(String tagName)
Creates a newCDATASection
.- Parameters:
tagName
- the tag-name for the CDATA-section- Returns:
- the new
CDATASection
-
newText
public Text newText(String tagName)
Creates a newText
.- Parameters:
tagName
- the tag-name for the text- Returns:
- the new
Text
-
addAttribute
public Node addAttribute(Node node, String name, String value)
- Parameters:
node
- the Node to add theAttr
ibute toname
- the name of theAttr
ibute to addvalue
- the value of theAttr
ibute to add- Returns:
- the
node
with the addedAttr
ibute - See Also:
newAttribute(String, String)
-
-