Class XMLDocument
- All Implemented Interfaces:
XML
XML
.
Objects of this class are immutable and thread-safe.
- Since:
- 0.1
- Suppressed Checkstyle violations:
- ClassFanOutComplexity (500 lines), AbbreviationAsWordInNameCheck (10 lines)
-
Constructor Summary
ConstructorDescriptionXMLDocument
(byte[] data) Public ctor, from XML as a text.XMLDocument
(File file) Public ctor, from XML in a file.XMLDocument
(InputStream stream) Public ctor, from input stream.XMLDocument
(String text) Public ctor, from XML as a text.XMLDocument
(URI uri) Public ctor, from XML in the URI.XMLDocument
(URL url) Public ctor, from XML in the URL.XMLDocument
(Path file) Public ctor, from XML in a file.XMLDocument
(Source source) Public ctor, from a source.XMLDocument
(Node node) Public ctor, from a DOM node. -
Method Summary
Modifier and TypeMethodDescriptionboolean
int
hashCode()
merge
(NamespaceContext ctx) Append this namespace context to the existing one.node()
Retrieve DOM node, represented by this wrapper.Retrieve DOM nodes from the XML response.registerNs
(String prefix, Object uri) Register additional namespace prefix for XPath.toString()
Find and return text elements or attributes matched by XPath address.
-
Constructor Details
-
XMLDocument
Public ctor, from XML as a text.The object is created with a default implementation of
NamespaceContext
, which already defines a number of namespaces, for convenience, including:xhtml: http://www.w3.org/1999/xhtml xs: http://www.w3.org/2001/XMLSchema xsi: http://www.w3.org/2001/XMLSchema-instance xsl: http://www.w3.org/1999/XSL/Transform svg: http://www.w3.org/2000/svg
In future versions we will add more namespaces (submit a ticket if you need more of them defined here).
- Parameters:
text
- XML document body
-
XMLDocument
public XMLDocument(byte[] data) Public ctor, from XML as a text.The object is created with a default implementation of
NamespaceContext
, which already defines a number of namespaces, for convenience, including:xhtml: http://www.w3.org/1999/xhtml xs: http://www.w3.org/2001/XMLSchema xsi: http://www.w3.org/2001/XMLSchema-instance xsl: http://www.w3.org/1999/XSL/Transform svg: http://www.w3.org/2000/svg
In future versions we will add more namespaces (submit a ticket if you need more of them defined here).
- Parameters:
data
- The XML body
-
XMLDocument
Public ctor, from a DOM node.The object is created with a default implementation of
NamespaceContext
, which already defines a number of namespaces, seeXMLDocument(String)
.- Parameters:
node
- DOM source- Since:
- 0.2
-
XMLDocument
Public ctor, from a source.The object is created with a default implementation of
NamespaceContext
, which already defines a number of namespaces, seeXMLDocument(String)
.An
IllegalArgumentException
is thrown if the parameter passed is not in XML format.- Parameters:
source
- Source of XML document
-
XMLDocument
Public ctor, from XML in a file.The object is created with a default implementation of
NamespaceContext
, which already defines a number of namespaces, seeXMLDocument(String)
.An
IllegalArgumentException
is thrown if the parameter passed is not in XML format.- Parameters:
file
- XML file- Throws:
FileNotFoundException
- In case of I/O problems
-
XMLDocument
Public ctor, from XML in a file.The object is created with a default implementation of
NamespaceContext
, which already defines a number of namespaces, seeXMLDocument(String)
.An
IllegalArgumentException
is thrown if the parameter passed is not in XML format.- Parameters:
file
- XML file- Throws:
FileNotFoundException
- In case of I/O problems
-
XMLDocument
Public ctor, from XML in the URL.The object is created with a default implementation of
NamespaceContext
, which already defines a number of namespaces, seeXMLDocument(String)
.An
IllegalArgumentException
is thrown if the parameter passed is not in XML format.- Parameters:
url
- The URL to load from- Throws:
IOException
- In case of I/O problems
-
XMLDocument
Public ctor, from XML in the URI.The object is created with a default implementation of
NamespaceContext
, which already defines a number of namespaces, seeXMLDocument(String)
.An
IllegalArgumentException
is thrown if the parameter passed is not in XML format.- Parameters:
uri
- The URI to load from- Throws:
IOException
- In case of I/O problems
-
XMLDocument
Public ctor, from input stream.The object is created with a default implementation of
NamespaceContext
, which already defines a number of namespaces, seeXMLDocument(String)
.An
IllegalArgumentException
is thrown if the parameter passed is not in XML format.The provided input stream will be closed automatically after getting data from it.
- Parameters:
stream
- The input stream, which will be closed automatically- Throws:
IOException
- In case of I/O problem
-
-
Method Details
-
toString
-
equals
-
hashCode
public int hashCode() -
node
Description copied from interface:XML
Retrieve DOM node, represented by this wrapper. -
xpath
Description copied from interface:XML
Find and return text elements or attributes matched by XPath address.The XPath query should point to text elements or attributes in the XML document. If any nodes of different types (elements, comments, etc.) are found in result node list - a
RuntimeException
will be thrown.Alternatively, the XPath query can be a function or expression that returns a single value instead of pointing to a set of nodes. In this case, the result will be a List containing a single String, the content of which is the result of the evaluation. If the expression result is not a String, it will be converted to a String representation and returned as such. For example, a document containing three <a> elements, the input query "count(//a)", will return a singleton List with a single string value "3".
This is a convenient method, which is used (according to our experience) in 95% of all cases. Usually you don't need to get anything else but a text value of some node or an attribute. And in most cases you are interested to get just the first value (use
xpath(..).get(0)
). But when/if you need to get more than just a plain text - useXML.nodes(String)
.The
List
returned will throwIndexOutOfBoundsException
if you try to access a node which wasn't found by this XPath query.An
IllegalArgumentException
is thrown if the parameter passed is not a valid XPath expression. -
registerNs
Description copied from interface:XML
Register additional namespace prefix for XPath.For example:
String name = new XMLDocument("...") .registerNs("ns1", "http://example.com") .registerNs("foo", "http://example.com/foo") .xpath("/ns1:root/foo:name/text()") .get(0);
A number of standard namespaces are registered by default in instances of XML. Their full list is in
XMLDocument(String)
.If a namespace prefix is already registered an
IllegalArgumentException
will be thrown.- Specified by:
registerNs
in interfaceXML
- Parameters:
prefix
- The XPath prefix to registeruri
- Namespace URI- Returns:
- A new XML document, with this additional namespace registered
-
nodes
Description copied from interface:XML
Retrieve DOM nodes from the XML response.The
List
returned will throwIndexOutOfBoundsException
if you try to access a node which wasn't found by this XPath query.An
IllegalArgumentException
is thrown if the parameter passed is not a valid XPath expression. -
merge
Description copied from interface:XML
Append this namespace context to the existing one.The existing context (inside this object) and the new one provided will be merged together. The existing context will be have higher priority.
-