Package org.openjdk.jmc.common.util
Class XmlToolkit
- java.lang.Object
- 
- org.openjdk.jmc.common.util.XmlToolkit
 
- 
 public final class XmlToolkit extends Object Class containing helper methods for XML processing.
- 
- 
Method SummaryAll Methods Static Methods Concrete Methods Modifier and Type Method Description static DocumentBuilderFactorycreateDocumentBuildFactoryInstance()Create a document builder factory with safe settings.static ElementcreateElement(Element parentNode, String tagName)Adds a child element with the name tagName to the parent and returns the new child element.static DocumentcreateNewDocument(String rootElementName)Creates a new empty XML document.static SAXParserFactorycreateSAXParserFactory()Create a SAX parser factory with safe settings.static TransformerFactorycreateTransformerFactory()Create a transformer factory with safe settings.static StringescapeAll(String s)static StringescapeTagContent(String s)static ElementgetChildElementOrNull(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 ElementgetOrCreateElement(Element parentNode, String tagName)Returns the child element with the specified tag name of the specified parent node.static StringgetSetting(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 StringgetStringValue(Element element)Returns the content between the tags of the element, for example <tag>hello </tag> where the value is "hello".static DocumentloadDocumentFromFile(File file)Loads an XML document from the specified file.static DocumentloadDocumentFromStream(InputStream stream)Loads an XML document from the specified stream.static DocumentloadDocumentFromString(String doc)Loads an XML document from its string representation.static StringprettyPrint(Element node)Pretty prints an XML document to a string, starting from the specified element.static voidprettyPrint(Element node, Writer wrt)Pretty prints an XML document to a writer, starting from the specified element.static voidsetSetting(Element parentNode, String settingName, String settingValue)Sets the value of a a "setting" element.static voidsetStringValue(Element element, String value)Sets the text value as a text node child of a an element.static voidstoreDocumentToFile(Document doc, File file)Stores an XML document in a file.static StringstoreDocumentToString(Document doc)Stores an XML document as a string.
 
- 
- 
- 
Method Detail- 
getOrCreateElementpublic static Element getOrCreateElement(Element parentNode, String tagName) Returns the child element with the specified tag name of the specified parent node. If no such child element is found, a new element with the specified tag name is created and returned.- Parameters:
- parentNode- parent node for the wanted element
- tagName- name of the wanted element
- Returns:
- the child element
- See Also:
- getChildElementOrNull(Element, String)
 
 - 
createElementpublic static Element createElement(Element parentNode, String tagName) Adds a child element with the name tagName to the parent and returns the new child element.- Parameters:
- parentNode- parent node to add the new element to
- tagName- the name of the new child element
- Returns:
- the new child element
 
 - 
setSettingpublic static void setSetting(Element parentNode, String settingName, String settingValue) Sets the value of a a "setting" element. If it already exists it will be updated. If it does not exist it will be created. If the setting element already exists, then there must not be any child elements to it other than a text value.- Parameters:
- parentNode- parent node of the setting element
- settingName- tag name of the setting element
- settingValue- the value to set
 
 - 
createNewDocumentpublic static Document createNewDocument(String rootElementName) throws IOException Creates a new empty XML document.- Parameters:
- rootElementName- the name of the root element
- Returns:
- an empty document
- Throws:
- IOException- if there is a problem creating the XML document
 
 - 
createSAXParserFactorypublic static SAXParserFactory createSAXParserFactory() throws SAXNotRecognizedException, SAXNotSupportedException, ParserConfigurationException Create a SAX parser factory with safe settings.- Returns:
- a new SAX parser factory
- Throws:
- SAXNotRecognizedException
- SAXNotSupportedException
- ParserConfigurationException
 
 - 
createDocumentBuildFactoryInstancepublic static DocumentBuilderFactory createDocumentBuildFactoryInstance() throws ParserConfigurationException Create a document builder factory with safe settings.- Returns:
- a new document builder factory
- Throws:
- ParserConfigurationException
 
 - 
createTransformerFactorypublic static TransformerFactory createTransformerFactory() throws TransformerFactoryConfigurationError, TransformerConfigurationException Create a transformer factory with safe settings.- Returns:
- a new transformer factory
- Throws:
- TransformerFactoryConfigurationError
- TransformerConfigurationException
 
 - 
setStringValuepublic static void setStringValue(Element element, String value) Sets the text value as a text node child of a an element. The element must not have any other child elements.- Parameters:
- element- the element to set the text value for
- value- the new value to set
 
 - 
getSettingpublic 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.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. - Parameters:
- parent- the parent element whose children to search for the settings node.
- settingName- name of the setting
- defaultValue- default value to return if setting is empty
- Returns:
- see above.
 
 - 
getStringValuepublic static String getStringValue(Element element) Returns the content between the tags of the element, for example <tag>hello </tag> where the value is "hello". If the element itself or its child is null, null will be returned. This method will only return a non-null String if the child node of the element is a text node.- Parameters:
- element- the element from which to extract the text node.
- Returns:
- the String value of the text node.
 
 - 
prettyPrintpublic static String prettyPrint(Element node) Pretty prints an XML document to a string, starting from the specified element.- Parameters:
- node- node from which to start pretty printing
- Returns:
- a string containing the pretty printed document
 
 - 
prettyPrintpublic static void prettyPrint(Element node, Writer wrt) Pretty prints an XML document to a writer, starting from the specified element.- Parameters:
- node- node from which to start pretty printing
- wrt- writer to write the document to
 
 - 
getChildElementsByTagpublic static List<Element> getChildElementsByTag(Node contextNode, String tag) Returns all the children from a node with a tag matching the tag argument.- Parameters:
- contextNode- node whose children to search
- tag- the tag to search for
- Returns:
- A list of elements with the found nodes. Will return an empty list if no matching element could be found.
 
 - 
loadDocumentFromFilepublic static Document loadDocumentFromFile(File file) throws SAXException, IOException Loads an XML document from the specified file.- Parameters:
- file- the file from which to read the document
- Returns:
- the parsed XML document
- Throws:
- SAXException- if the document could not be parsed
- IOException- if the stream could not be read
 
 - 
loadDocumentFromStreampublic static Document loadDocumentFromStream(InputStream stream) throws SAXException, IOException Loads an XML document from the specified stream.- Parameters:
- stream- the input stream from which to read the document
- Returns:
- the parsed XML document
- Throws:
- SAXException- if the document could not be parsed
- IOException- if the stream could not be read
 
 - 
loadDocumentFromStringpublic static Document loadDocumentFromString(String doc) throws SAXException Loads an XML document from its string representation.- Parameters:
- doc- the string to read from
- Returns:
- the parsed XML document
- Throws:
- SAXException- if the document could not be parsed
- NullPointerException- if the input string is null
 
 - 
storeDocumentToFilepublic static void storeDocumentToFile(Document doc, File file) throws IOException Stores an XML document in a file.- Parameters:
- doc- the XML document to store
- file- the file to store it in
- Throws:
- IOException- if the file could not written
 
 - 
storeDocumentToStringpublic static String storeDocumentToString(Document doc) Stores an XML document as a string.- Parameters:
- doc- the XML document to store
- Returns:
- the XML document as a string
 
 - 
getChildElementOrNullpublic static Element getChildElementOrNull(Element parent, String name) Returns the child element with the specified tag name of the specified parent node. If no such child element is found,nullis returned.- Parameters:
- parent- parent node for the wanted element
- name- name of the wanted element
- Returns:
- the child element, or nullif no such element exists
- See Also:
- getOrCreateElement(Element, String)
 
 
- 
 
-