public class XmlUtils extends Object
| Modifier and Type | Method and Description |
|---|---|
static Node |
createTextNode(String tagName,
Object contents)
Creates an XML node from the
default document whose only child
is a text node that contains the string representation of the
given object |
static String |
getAttributeValue(Node node,
String attributeName,
String defaultValue)
Returns the attribute with the given name from the given node.
|
static List<Node> |
getChildren(Node node,
String childNodeName)
Returns the children of the given node with the given name (ignoring
upper/lower case).
|
static Document |
getDefaultDocument()
Returns a default XML document
|
static Node |
getFirstChild(Node node,
String childNodeName)
Returns the first child of the given node with the given name (ignoring
upper/lower case), or
null if no such child is found. |
static String |
getRequiredAttributeValue(Node node,
String attributeName)
Returns the attribute with the given name from the given node.
|
static Node |
read(InputStream inputStream)
Creates an XML node by reading the contents of the given input stream.
|
static boolean |
readBooleanChild(Node node,
String childNodeName,
boolean defaultValue)
Read an boolean value from the first child of the given node with
the given name.
|
static <E extends Enum<E>> |
readEnumChild(Node node,
Class<E> enumClass,
String childNodeName)
Parse an enum value from the first child of the given node with
the given name
|
static int |
readIntChild(Node node,
String childNodeName,
int defaultValue)
Read an int value from the first child of the given node with
the given name.
|
static String |
toString(Node node)
Creates a formatted String representation of the given XML node,
including the XML declaration
|
static String |
toString(Node node,
int indentation,
boolean omitXmlDeclaration)
Creates a String representation of the given XML node
|
static void |
verifyNode(Node node,
String expected)
Verify that the given node is not
null, and that its
name matches the expected tag name (ignoring upper/lowercase),
and throw an XmlException if this is not the case. |
static void |
write(Node node,
OutputStream outputStream)
Writes a formatted String representation of the given XML node,
including the XML declaration, to the given output stream.
|
static void |
write(Node node,
OutputStream outputStream,
int indentation,
boolean omitXmlDeclaration)
Writes a formatted String representation of the given XML node,
including the XML declaration, to the given output stream.
|
public static Document getDefaultDocument()
public static String toString(Node node)
node - The nodeXmlException - If there was an error while writing.
This should never be the case for this method, because the
XML is written into a String, and no exceptional situation
(like IOException) can happen here.public static String toString(Node node, int indentation, boolean omitXmlDeclaration)
node - The nodeindentation - The indentation. If this is not positive, then
no indentation will be performedomitXmlDeclaration - Whether the XML declaration should be omittedXmlException - If there was an error while writing.
This should never be the case for this method, because the
XML is written into a String, and no exceptional situation
(like IOException) can happen here.public static void write(Node node, OutputStream outputStream)
node - The nodeoutputStream - The output stream to write toXmlException - If there was an error while writingpublic static void write(Node node, OutputStream outputStream, int indentation, boolean omitXmlDeclaration)
node - The nodeoutputStream - The output stream to write toindentation - The indentation. If this is not positive, then
no indentation will be performedomitXmlDeclaration - Whether the XML declaration should be omittedXmlException - If there was an error while writingpublic static Node read(InputStream inputStream) throws XmlException
inputStream - The input stream to read fromXmlException - If there was an error while readingpublic static Node createTextNode(String tagName, Object contents)
default document whose only child
is a text node that contains the string representation of the
given objecttagName - The tag name for the nodecontents - The object whose string representation will be
the contents of the text nodepublic static String getAttributeValue(Node node, String attributeName, String defaultValue)
node - The node to obtain the attribute fromattributeName - The name of the attributedefaultValue - The default value to return when the specified
attribute could not be obtainedpublic static String getRequiredAttributeValue(Node node, String attributeName)
node - The node to obtain the attribute fromattributeName - The name of the attributeXmlException - If no value of the attribute with the given
name could be obtained.public static Node getFirstChild(Node node, String childNodeName)
null if no such child is found.node - The nodechildNodeName - The child node namenullpublic static List<Node> getChildren(Node node, String childNodeName)
node - The nodechildNodeName - The child node namepublic static void verifyNode(Node node, String expected)
null, and that its
name matches the expected tag name (ignoring upper/lowercase),
and throw an XmlException if this is not the case.node - The nodeexpected - The expected tag nameXmlException - If the node is null, or the
node name does not match the expected namepublic static int readIntChild(Node node, String childNodeName, int defaultValue)
node - The nodechildNodeName - The child node namedefaultValue - The default valueXmlException - If the given node was null, or no
int value could be parsedpublic static boolean readBooleanChild(Node node, String childNodeName, boolean defaultValue)
node - The nodechildNodeName - The child node namedefaultValue - The default valueXmlException - If the given node was nullpublic static <E extends Enum<E>> E readEnumChild(Node node, Class<E> enumClass, String childNodeName)
E - The enum typenode - The nodeenumClass - The enum classchildNodeName - The child node nameXmlException - If the given node was null, or no
enum value could be parsedCopyright © 2020. All rights reserved.