public final class XmlToolkit extends Object
| Modifier and Type | Method and Description |
|---|---|
static DocumentBuilderFactory |
createDocumentBuildFactoryInstance()
Create a document builder factory with safe settings.
|
static Element |
createElement(Element parentNode,
String tagName)
Adds a child element with the name tagName to the parent and returns the new child element.
|
static Document |
createNewDocument(String rootElementName)
Creates a new empty XML document.
|
static SAXParserFactory |
createSAXParserFactory()
Create a SAX parser factory with safe settings.
|
static TransformerFactory |
createTransformerFactory()
Create a transformer factory with safe settings.
|
static String |
escapeAll(String s) |
static String |
escapeTagContent(String s) |
static Element |
getChildElementOrNull(Element parent,
String name)
Returns the child element with the specified tag name of the specified parent node.
|
static List<Element> |
getChildElementsByTag(Node contextNode,
String tag)
Returns all the children from a node with a tag matching the tag argument.
|
static Element |
getOrCreateElement(Element parentNode,
String tagName)
Returns the child element with the specified tag name of the specified parent node.
|
static String |
getSetting(Element parent,
String settingName,
String defaultValue)
Returns the value of the setting with the specified name or a default value if the setting
had no value.
|
static String |
getStringValue(Element element)
Returns the content between the tags of the element, for example <tag>hello
</tag> where the value is "hello".
|
static Document |
loadDocumentFromFile(File file)
Loads an XML document from the specified file.
|
static Document |
loadDocumentFromStream(InputStream stream)
Loads an XML document from the specified stream.
|
static Document |
loadDocumentFromString(String doc)
Loads an XML document from its string representation.
|
static String |
prettyPrint(Element node)
Pretty prints an XML document to a string, starting from the specified element.
|
static void |
prettyPrint(Element node,
Writer wrt)
Pretty prints an XML document to a writer, starting from the specified element.
|
static void |
setSetting(Element parentNode,
String settingName,
String settingValue)
Sets the value of a a "setting" element.
|
static void |
setStringValue(Element element,
String value)
Sets the text value as a text node child of a an element.
|
static void |
storeDocumentToFile(Document doc,
File file)
Stores an XML document in a file.
|
static String |
storeDocumentToString(Document doc)
Stores an XML document as a string.
|
public static Element getOrCreateElement(Element parentNode, String tagName)
parentNode - parent node for the wanted elementtagName - name of the wanted elementgetChildElementOrNull(Element, String)public static Element createElement(Element parentNode, String tagName)
parentNode - parent node to add the new element totagName - the name of the new child elementpublic static void setSetting(Element parentNode, String settingName, String settingValue)
parentNode - parent node of the setting elementsettingName - tag name of the setting elementsettingValue - the value to setpublic static Document createNewDocument(String rootElementName) throws IOException
rootElementName - the name of the root elementIOException - if there is a problem creating the XML documentpublic static SAXParserFactory createSAXParserFactory() throws SAXNotRecognizedException, SAXNotSupportedException, ParserConfigurationException
SAXNotRecognizedExceptionSAXNotSupportedExceptionParserConfigurationExceptionpublic static DocumentBuilderFactory createDocumentBuildFactoryInstance() throws ParserConfigurationException
ParserConfigurationExceptionpublic static TransformerFactory createTransformerFactory() throws TransformerFactoryConfigurationError, TransformerConfigurationException
TransformerFactoryConfigurationErrorTransformerConfigurationExceptionpublic static void setStringValue(Element element, String value)
element - the element to set the text value forvalue - the new value to setpublic static String getSetting(Element parent, String settingName, String defaultValue)
Since everything should have a default value, no other version of get setting exists. This method implicitly builds the setting node with a default value if none is found.
parent - the parent element whose children to search for the settings node.settingName - name of the settingdefaultValue - default value to return if setting is emptypublic static String getStringValue(Element element)
element - the element from which to extract the text node.public static String prettyPrint(Element node)
node - node from which to start pretty printingpublic static void prettyPrint(Element node, Writer wrt)
node - node from which to start pretty printingwrt - writer to write the document topublic static List<Element> getChildElementsByTag(Node contextNode, String tag)
contextNode - node whose children to searchtag - the tag to search forpublic static Document loadDocumentFromFile(File file) throws SAXException, IOException
file - the file from which to read the documentSAXException - if the document could not be parsedIOException - if the stream could not be readpublic static Document loadDocumentFromStream(InputStream stream) throws SAXException, IOException
stream - the input stream from which to read the documentSAXException - if the document could not be parsedIOException - if the stream could not be readpublic static Document loadDocumentFromString(String doc) throws SAXException
doc - the string to read fromSAXException - if the document could not be parsedNullPointerException - if the input string is nullpublic static void storeDocumentToFile(Document doc, File file) throws IOException
doc - the XML document to storefile - the file to store it inIOException - if the file could not writtenpublic static String storeDocumentToString(Document doc)
doc - the XML document to storepublic static Element getChildElementOrNull(Element parent, String name)
null is returned.parent - parent node for the wanted elementname - name of the wanted elementnull if no such element existsgetOrCreateElement(Element, String)Copyright © 2019. All rights reserved.