public final class DomUtils extends Object
| Modifier and Type | Method and Description |
|---|---|
static void |
addAttribute(Element elem,
String attrName,
String attrValue)
Adds an attribute to the element.
|
static boolean |
checkElement(Element elem,
String name,
String namespace)
Returns true if the element matches the name and namespace.
|
static void |
checkNotNullOrEmpty(Document xmlDoc,
String name)
Throws an exception if the document is null or contains no nodes.
|
static Element |
getChildElement(Element element,
String namespace,
String tagName)
Get the child
Element of the Element with the namespace and
tagName. |
static List<Element> |
getChildElements(Element element,
String namespace,
String tagName)
Get the child
Elements of the Element with the namespace
and tagName. |
static List<Element> |
getChildElements(Node node)
Extract all child DOM
Elements from the DOM Node. |
static Element |
getFirstChildElement(Node node)
Returns the first element from a nodes child nodes.
|
static boolean |
isDocumentEmpty(Document xmlDoc)
Returns 'true' if the XML document contains no nodes, otherwise 'false'.
|
static Document |
newDocument()
Creates a new empty XML document.
|
static Document |
newDocument(Node rootNode)
Creates a new XML document using the node as the root.
|
static Document |
parse(File file)
Loads an XML document from file.
|
static Document |
parse(Reader reader)
Loads an XML document from a reader.
|
static Document |
parse(Reader reader,
ErrorHandler errorHandler)
Loads an XML document from a reader.
|
static void |
removeFormatting(Document xmlDoc)
Removes the formatting whitespaces, namely removes whitespace between child
elements and removes leading and trailing whitespaces in elements with
textual content.
|
static void |
removeFormatting(Element xmlElem)
Removes the formatting whitespaces, namely removes whitespace between child
elements and removes leading and trailing whitespaces in elements with
textual content.
|
static void |
serialise(Document xmlDoc,
File file)
Serialises an XML
Document to a file. |
static void |
serialise(Document xmlDoc,
Writer writer)
Serialises an XML
Document to a character stream. |
static String |
serialiseToString(Document xmlDoc)
Returns a serialised XML representation of the XML
Document. |
static String |
serialiseToString(Element xmlElem)
Returns a serialised XML representation of the XML
Document. |
public static Document newDocument() throws ParserConfigurationException
ParserConfigurationException - Exception if any parser problem.public static Document newDocument(Node rootNode) throws ParserConfigurationException
rootNode - Root node. Cannot be null.ParserConfigurationException - Exception if any parser problem.public static Document parse(File file) throws ParserConfigurationException, IOException, SAXException
file - File path to the XML file. Cannot be null.ParserConfigurationException - Exception if any parser problem.IOException - Exception if problems reading file.SAXException - Exception if problems with SAX.public static Document parse(Reader reader) throws ParserConfigurationException, IOException, SAXException
reader - Input character stream containing the XML content. Cannot be null.ParserConfigurationException - Exception if any parser problem.IOException - Exception if problems reading file.SAXException - Exception if problems with SAX.public static Document parse(Reader reader, ErrorHandler errorHandler) throws ParserConfigurationException, SAXException, IOException
reader - Input character stream containing the XML content. Cannot be null.errorHandler - Callback handler for errors from parser. Optional.ParserConfigurationException - Exception if any parser problem.IOException - Exception if problems reading file.SAXException - Exception if problems with SAX.public static String serialiseToString(Element xmlElem) throws TransformerException, IOException
Document.xmlElem - An XML Element. Cannot be null.Document.TransformerException - Exception if any transformation problems.IOException - Exception if problems writing.public static String serialiseToString(Document xmlDoc) throws TransformerException, IOException
Document.xmlDoc - An XML Document. Cannot be null.Document.TransformerException - Exception if any transformation problems.IOException - Exception if problems writing.public static void serialise(Document xmlDoc, File file) throws TransformerException, IOException
Document to a file.xmlDoc - An XML Document. Cannot be null.file - The file to serialise the the XML Document to. Cannot be null.TransformerException - Exception if any transformation problems.IOException - Exception if problems writing.public static void serialise(Document xmlDoc, Writer writer) throws TransformerException, IOException
Document to a character stream.xmlDoc - An XML Document. Cannot be null.writer - A character stream to which the XML Document is serialised. Cannot
be null.TransformerException - Exception if any transformation problems.IOException - Exception if any problems writing.public static Element getFirstChildElement(Node node)
node - Node that has child nodes. Cannot be null.public static List<Element> getChildElements(Node node)
Elements from the DOM Node.node - The DOM Node. Cannot be null.Elements of the Node.public static Element getChildElement(Element element, String namespace, String tagName)
Element of the Element with the namespace and
tagName.element - The source Element. Cannot be null.namespace - The namespace of the child Element. Cannot be null nor
blank.tagName - The tagName of the child Element. Cannot be null nor
blank.Element with the given namespace and tagName.IllegalArgumentException - If the element does not have exactly one child element with the
required namespace and tagName.public static List<Element> getChildElements(Element element, String namespace, String tagName)
Elements of the Element with the namespace
and tagName.element - The source Element. Cannot be null.namespace - The namespace of the child Elements. Cannot be null nor
blank.tagName - The tagName of the child Elements. Cannot be null nor
blank.Elements with the given namespace and
tagName.IllegalArgumentException - If the element does not have at least one child element with the
required namespace and tagName.public static void addAttribute(Element elem, String attrName, String attrValue)
elem - Element to add the attribute to. Cannot be null.attrName - Name of the attribute. Cannot be null nor blank.attrValue - Value of the attribute. Cannot be null nor blank.public static boolean isDocumentEmpty(Document xmlDoc)
xmlDoc - XML document. Cannot be null.public static void checkNotNullOrEmpty(Document xmlDoc, String name)
xmlDoc - XML document to check.name - Name of the instance. Cannot be null.public static boolean checkElement(Element elem, String name, String namespace)
elem - Element. Cannot be null.name - Name of the element. Cannot be null nor blank.namespace - Namespace of the element. Cannot be null nor blank.public static void removeFormatting(Document xmlDoc)
xmlDoc - The DOM document to format. The formatting whitespaces will be
removed from this document. If the document is null, nothing
happens.public static void removeFormatting(Element xmlElem)
xmlElem - The DOM element to format. The formatting whitespaces will be
removed from this element. If the element is null, nothing
happens.Copyright © 2021. All rights reserved.