Package com.storedobject.common
Class XML
java.lang.Object
com.storedobject.common.XML
-
Constructor Summary
ConstructorsConstructorDescriptionXML()
XML represents an XML document and provides various methods for manipulating and extracting information from the XML structure.XML
(InputStream stream) Creates a new XML object by setting the contents of the XML from the given input stream.Constructs an XML object using the provided Reader.Represents an XML document.XML class represents an XML document.XML constructor with a Document object that represents the XML structure. -
Method Summary
Modifier and TypeMethodDescriptionboolean
Extract the boolean value of a path.boolean
Extract the boolean value of a path.copy()
Creates a new XML object with the same content as the current object.protected void
customizeBuilder
(DocumentBuilder documentBuilder) Customizes the DocumentBuilder by providing additional configuration options.protected void
customizeBuilderFactory
(DocumentBuilderFactory documentBuilderFactory) Customize the DocumentBuilderFactory before creating a new DocumentBuilder instance.protected void
customizePathFactory
(XPathFactory pathFactory) Customize the XPathFactory used by the XML class.Deprecated.Deprecated.UsegetText(Node, String)
instead.Retrieves the document.getNamespacePrefix
(String uri) Returns the namespace prefix associated with the given URI.getNamespaceURI
(String prefix) Retrieves the namespace URI associated with the given prefix.Retrieves the Node object corresponding to the given XPath expression.Retrieves the specific node based on the given XPath expression.Extract the numeric value of a path.Extract the numeric value of a path under a given node.Extract the text value of a path.Extract the text value of a path under a given node.void
Sets the entity resolver of the document builder to ignore DTDs.Returns a list of strings matching the given XPath expression.Returns a list of string values from the given XML node using the specified XPath.Retrieves a list of nodes from the XML document that match a given XPath expression.Returns a list of nodes matching the given XPath expression within the given node.void
set
(InputStream stream) Sets the input stream.void
Sets the XML document using the provided Reader object.void
Sets the value of the object using the provided XML representation.void
Sets the input stream using the given URL.void
Sets the Document object representing the XML structure.void
setNamespacePrefix
(String prefix) Sets the namespace prefix for the XML document.void
setPack
(boolean pack) Whether to pack the XML structure by removing all the unwanted text nodes or not.boolean
Set a text value to the given node if it is a "text node".boolean
Set a text value to the given node if it is a "text node".Converts the document to a formatted string representation.toPrettyString
(Node node) Converts the given XMLNode
to a pretty formatted XML string.toString()
Returns a string representation of the current object.Converts a Node object to its string representation.void
write
(OutputStream stream) Writes the content of the document to the specified output stream.void
Write the document to the given writer.void
write
(Node node, OutputStream stream) Writes the given XML node to the specified output stream.void
Write the XML content of a given Node to a Writer.
-
Constructor Details
-
XML
public XML()XML represents an XML document and provides various methods for manipulating and extracting information from the XML structure. -
XML
-
XML
Creates a new XML object by setting the contents of the XML from the given input stream.- Parameters:
stream
- The input stream containing the XML content.- Throws:
Exception
- If any exception occurs.
-
XML
-
XML
-
XML
-
-
Method Details
-
customizeBuilderFactory
Customize the DocumentBuilderFactory before creating a new DocumentBuilder instance.- Parameters:
documentBuilderFactory
- The DocumentBuilderFactory to customize.
-
customizeBuilder
Customizes the DocumentBuilder by providing additional configuration options.- Parameters:
documentBuilder
- The DocumentBuilder to customize.
-
customizePathFactory
Customize the XPathFactory used by the XML class.- Parameters:
pathFactory
- The XPathFactory to customize.
-
setPack
public void setPack(boolean pack) Whether to pack the XML structure by removing all the unwanted text nodes or not. The default value istrue
. Packing reduces human readability.- Parameters:
pack
- True/false.
-
ignoreDTDs
public void ignoreDTDs()Sets the entity resolver of the document builder to ignore DTDs. This is useful when working with XML documents that reference DTDs, but you want to ignore them during parsing. -
copy
-
set
-
set
-
set
Sets the input stream.- Parameters:
stream
- The input stream to set.- Throws:
Exception
- If an error occurs.
-
set
-
setNamespacePrefix
Sets the namespace prefix for the XML document.- Parameters:
prefix
- The desired namespace prefix. If null, the default namespace prefix will be used.
-
getNamespacePrefix
-
getNamespaceURI
-
set
-
list
Returns a list of strings matching the given XPath expression.- Parameters:
xpath
- the XPath expression to evaluate- Returns:
- an ArrayList of strings that match the given XPath expression
- Throws:
Exception
- if an error occurs while evaluating the XPath expression
-
listNodes
Retrieves a list of nodes from the XML document that match a given XPath expression.- Parameters:
xpath
- The XPath expression to match nodes in the XML document.- Returns:
- An ArrayList of nodes that match the given XPath expression.
- Throws:
Exception
- If an error occurs while retrieving the nodes.
-
getNode
Retrieves the Node object corresponding to the given XPath expression.- Parameters:
xpath
- the XPath expression to search for the Node object- Returns:
- the Node object matching the XPath expression
- Throws:
Exception
- if there is an error while retrieving the Node object
-
check
-
get
Deprecated.UsegetText(String)
instead.Extract the text value of a path.- Parameters:
xpath
- XPath.- Returns:
- Value.
- Throws:
Exception
- If any error occurs.
-
getText
-
getNumber
-
check
-
setText
Set a text value to the given node if it is a "text node". If the node is not a "text node", the value will be set to its first child if that is a "text node". Else, it will recursively go down until a "text node" is found. Please note that the recursive traversal happens only through the first children.- Parameters:
xpathToNode
- XPath's expression to reach the node.value
- Value to set.- Returns:
- True if the value was successfully set. Otherwise, false.
-
setText
Set a text value to the given node if it is a "text node". If the node is not a "text node", the value will be set to its first child if that is a "text node". Else, it will recursively go down until a "text node" is found. Please note that the recursive traversal happens only through the first children.- Parameters:
node
- Node for which value to be set.value
- Value to set.- Returns:
- True if the value was successfully set. Otherwise, false.
-
get
Deprecated.UsegetText(Node, String)
instead.Extract the text value of a path under a given node.- Parameters:
node
- Node.xpath
- XPath.- Returns:
- Value.
- Throws:
Exception
- If any error occurs.
-
getText
-
getNumber
-
list
Returns a list of string values from the given XML node using the specified XPath.- Parameters:
node
- The XML node to extract values from.xpath
- The XPath expression to be evaluated.- Returns:
- An ArrayList of string values that match the given XPath expression.
- Throws:
Exception
- If an error occurs during the XML parsing or XPath evaluation.
-
listNodes
Returns a list of nodes matching the given XPath expression within the given node.- Parameters:
node
- The node to search within.xpath
- The XPath expression to match nodes.- Returns:
- A list of nodes matching the XPath expression.
- Throws:
Exception
- if an error occurs during the evaluation of the XPath expression.
-
getNode
Retrieves the specific node based on the given XPath expression.- Parameters:
node
- The starting node from which to search for the target node.xpath
- The XPath expression used to locate the target node.- Returns:
- The target node if found, otherwise null.
- Throws:
Exception
- if an error occurs during the evaluation of the XPath expression.
-
toPrettyString
Converts the document to a formatted string representation.- Returns:
- A string representing the document in a pretty format.
-
toPrettyString
-
toString
-
toString
-
write
Writes the content of the document to the specified output stream.- Parameters:
stream
- the output stream to write the content to- Throws:
Exception
- if an error occurs while writing the content
-
write
-
write
Writes the given XML node to the specified output stream.- Parameters:
node
- the XML node to be writtenstream
- the output stream to write the XML to- Throws:
Exception
- if an error occurs during the writing process
-
write
-
getDocument
-
getText(String)
instead.