Package com.thecoderscorner.menu.persist
Class XMLDOMHelper
java.lang.Object
com.thecoderscorner.menu.persist.XMLDOMHelper
A series of static helper method that make dealing with XML easier, this is because many areas of tcMenu deal with XML
documents, and these functions are generally useful.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic ElementappendElementWithNameValue(Element parent, String name, Object value) Append a new element into the parent provided with the name given.static ElementelementWithName(Element elem, String child) Gets an element with a given name or nullstatic StringgetAttributeOrDefault(Element elem, String val, Object def) Gets the text of an attribute or a default value.static StringgetAttrOrNull(Element ele, String attr) Gets the text of attribute if it is available, or nullgetChildElementsWithName(Element ele, String name) Find all child elements with a given namestatic intintegerOfElementByName(Element element, String child, int def) Gets the int value associated with the named child, or the default if it does not exist or can't be parsed.static DocumentloadDocumentFromData(String data) Load an XML document from a string of datastatic DocumentloadDocumentFromPath(Path filePath) Load an XML document from a file system pathstatic DocumentloadDocumentStream(InputStream stream) Load an XML document from a streamstatic DocumentnewDocumentRoot(String name) Creates a new document that has a root element with the name givenstatic StringtextOfElementByName(Element elem, String child) Get the text of an element with the name child.static <T> List<T>Transforms all elements that match `childName` under either `root` if eleName is null, or under root/eleName if eleName is provided.static <T> List<T>transformElements(Element root, String childName, Function<Element, T> transform) Transforms all elements that match `childName` under `root`, there is another overload that provides indirection.static voidwriteXml(Document doc, OutputStream output) Writes an XML document without reformatting it, this is good if you are working with a DOM that was previously loaded from a stream, where it may be already formatted.static voidwriteXml(Document doc, OutputStream output, boolean prettyPrint) Writes an XML document to a stream
-
Constructor Details
-
XMLDOMHelper
public XMLDOMHelper()
-
-
Method Details
-
loadDocumentFromPath
public static Document loadDocumentFromPath(Path filePath) throws IOException, ParserConfigurationException, SAXException Load an XML document from a file system path- Parameters:
filePath- the path to load from- Returns:
- an XML document if successful, otherwise throws
- Throws:
IOException- for any IO problemsParserConfigurationException- if the document does not parseSAXException- if the document does not parse
-
loadDocumentFromData
public static Document loadDocumentFromData(String data) throws IOException, ParserConfigurationException, SAXException Load an XML document from a string of data- Parameters:
data- the data to load from- Returns:
- an XML document if successful, otherwise throws
- Throws:
IOException- for any IO problemsParserConfigurationException- if the document does not parseSAXException- if the document does not parse
-
loadDocumentStream
public static Document loadDocumentStream(InputStream stream) throws ParserConfigurationException, IOException, SAXException Load an XML document from a stream- Parameters:
stream- the steam to load from- Returns:
- an XML document if successful, otherwise throws
- Throws:
IOException- for any IO problemsParserConfigurationException- if the document does not parseSAXException- if the document does not parse
-
getChildElementsWithName
Find all child elements with a given name- Parameters:
ele- the parent elementname- the name to search for- Returns:
- a list of elements directly under ele that are named `name`
-
textOfElementByName
Get the text of an element with the name child. Saves looking up the element and then getting text.- Parameters:
elem- the parent elementchild- the name of the child to get the text of- Returns:
- the text content or blank.
-
integerOfElementByName
Gets the int value associated with the named child, or the default if it does not exist or can't be parsed.- Parameters:
element- the parent elementchild- the name of the child elementdef- the default value in case of problem
-
elementWithName
Gets an element with a given name or null- Parameters:
elem- the parent elementchild- the child to locate- Returns:
- the element or null
-
getAttrOrNull
Gets the text of attribute if it is available, or null- Parameters:
ele- the elementattr- the attribute name- Returns:
- the text of the attribute or null
-
getAttributeOrDefault
Gets the text of an attribute or a default value.- Parameters:
elem- the elementval- the attribute namedef- the default value- Returns:
- the attribute text or otherwise the default
-
transformElements
public static <T> List<T> transformElements(Element root, String eleName, String childName, Function<Element, T> transform) Transforms all elements that match `childName` under either `root` if eleName is null, or under root/eleName if eleName is provided. You provide the transformation function that takes an Element and returns the desired result- Type Parameters:
T- can be any type you wish to return in your transformation- Parameters:
root- the root element to start ateleName- optionally, another level of indirection (can be null) EG root/eleNamechildName- the items to filter fortransform- the transformation to apply - returns T, takes an element- Returns:
- the transformed list or an empty list.
-
transformElements
public static <T> List<T> transformElements(Element root, String childName, Function<Element, T> transform) Transforms all elements that match `childName` under `root`, there is another overload that provides indirection. You provide the transformation function that takes an Element and returns the desired result.- Type Parameters:
T- can be any type you wish to return in your transformation- Parameters:
root- the root element to start atchildName- the items to filter fortransform- the transformation to apply - returns T, takes an element- Returns:
- the transformed list or an empty list.
-
writeXml
Writes an XML document without reformatting it, this is good if you are working with a DOM that was previously loaded from a stream, where it may be already formatted.- Parameters:
doc- the documentoutput- the output stream to write to- Throws:
TransformerException- if the XML cannot be transformed
-
writeXml
public static void writeXml(Document doc, OutputStream output, boolean prettyPrint) throws TransformerException Writes an XML document to a stream- Parameters:
doc- the documentoutput- the stream to write toprettyPrint- if the document should be formatted- Throws:
TransformerException- if it cannot be written
-
newDocumentRoot
Creates a new document that has a root element with the name given- Parameters:
name- the root element name- Returns:
- a document with root node attached
- Throws:
ParserConfigurationException- if the document cannot be created
-
appendElementWithNameValue
Append a new element into the parent provided with the name given.- Parameters:
parent- the parent where the new element is to be placedname- the name of the new elementvalue- the value to set as text (can be null)- Returns:
- the newly created element
-