Package com.mediaworx.xmlutils
Class XmlHelper
java.lang.Object
com.mediaworx.xmlutils.XmlHelper
Class used to create helper objects that simplify parsing and modifying XML files. To retrieve single nodes or
multiple nodes from the XML XPath is used, see the
Java API documentation on XPath
for more information.
- Author:
- Kai Widmann, mediaworx Berlin AG
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringDefault encoding that is used if no encoding is given -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidparses and appends the content of a file as a child node to the given parent nodevoidappendNode(Node parent, Node newChild) Appends a new child node to a parent node.static voidcleanEmptyTextNodes(Node parentNode) Removes text nodes that are empty or contain whitespace only if the parent node has at least one child of any of the following types: ELEMENT, CDATA, COMMENT.intgetIntValueForXpath(Node ancestorNode, String xPath) Retrieves the content of the node at the given XPath as int.getNodeListForXPath(Node ancestorNode, String xPath) Retrieves the NodeList for the given XPath from the given ancestor Node.getSingleNodeForXPath(Node ancestorNode, String xPath) Retrieves a single node at a given XPath from the given ancestor node.getStringValueForXpath(Node ancestorNode, String xPath) Retrieves the String content of a node at the given XPath.getXmlStringFromDocument(Document document, String[] cdataElements) Converts the document to a formatted XML String (indentation level is 4) using default encoding (UTF-8).getXmlStringFromDocument(Document document, String[] cdataElements, String encoding) Converts the document to a formatted XML String (indentation level is 4) using the given encoding.Parses the XML content of the file at the given path using the default encoding (UTF-8).Parses the XML content of the file at the given path using the default encoding (UTF-8).Parses the XML content of the file at the given path using the default encoding (UTF-8).Parses the XML content of the file at the given path using the default encoding (UTF-8).Parses the XML content of the file at the given path using the default encoding (UTF-8).Parses the XML content of the file at the given path using the given encoding.
-
Field Details
-
DEFAULT_ENCODING
Default encoding that is used if no encoding is given- See Also:
-
-
Constructor Details
-
XmlHelper
Creates and initializes a new XmlHelper instance.- Throws:
ParserConfigurationException- if for some reason the DocumentBuilder used to parse the XML can't be initialized
-
-
Method Details
-
parseFile
Parses the XML content of the file at the given path using the default encoding (UTF-8). Empty text nodes or text noes containing whitespace only are removed.- Parameters:
path- the XML file's path- Returns:
- the parsed XML document
- Throws:
IOException- if there's a problem accessing the fileSAXException- if the file content can't be parsed
-
parseFile
public Document parseFile(String path, Map<String, String> replacements) throws IOException, SAXExceptionParses the XML content of the file at the given path using the default encoding (UTF-8). Empty text nodes or text noes containing whitespace only are removed. If a replacement map is provided, each key in the map is replaced by the corresponding value in the file's content.- Parameters:
path- the XML file's pathreplacements- Map containing replacement strings, key: string to be replaced, value: replacement string (if the map is null, no replacements are made)- Returns:
- the parsed XML document
- Throws:
IOException- if there's a problem accessing the fileSAXException- if the file content can't be parsed
-
parseFile
public Document parseFile(String path, Map<String, String> replacements, String encoding) throws IOException, SAXExceptionParses the XML content of the file at the given path using the given encoding. Empty text nodes or text noes containing whitespace only are removed. If a replacement map is provided, each key in the map is replaced by the corresponding value in the file's content.- Parameters:
path- the XML file's pathreplacements- Map containing replacement strings, key: string to be replaced, value: replacement string (if the map is null, no replacements are made)encoding- the encoding to be used to parse the file (must be a valid encoding like "UTF-8")- Returns:
- the parsed XML document
- Throws:
IOException- if there's a problem accessing the fileSAXException- if the file content can't be parsed
-
parseFile
Parses the XML content of the file at the given path using the default encoding (UTF-8). Empty text nodes or text noes containing whitespace only are removed.- Parameters:
file- the file containing the XML- Returns:
- the parsed XML document
- Throws:
IOException- if there's a problem accessing the fileSAXException- if the file content can't be parsed
-
parseFile
public Document parseFile(File file, Map<String, String> replacements) throws IOException, SAXExceptionParses the XML content of the file at the given path using the default encoding (UTF-8). Empty text nodes or text noes containing whitespace only are removed. If a replacement map is provided, each key in the map is replaced by the corresponding value in the file's content.- Parameters:
file- the file containing the XMLreplacements- Map containing replacement strings, key: string to be replaced, value: replacement string (if the map is null, no replacements are made)- Returns:
- the parsed XML document
- Throws:
IOException- if there's a problem accessing the fileSAXException- if the file content can't be parsed
-
parseFile
public Document parseFile(File file, Map<String, String> replacements, String encoding) throws IOException, SAXExceptionParses the XML content of the file at the given path using the default encoding (UTF-8). Empty text nodes or text nodes containing whitespace only are removed. If a replacement map is provided, each key in the map is replaced by the corresponding value in the file's content.- Parameters:
file- the file containing the XMLreplacements- Map containing replacement strings, key: string to be replaced, value: replacement string (if the map is null, no replacements are made)encoding- the encoding to be used to parse the file (must be a valid encoding like "UTF-8")- Returns:
- the parsed XML document
- Throws:
IOException- if there's a problem accessing the fileSAXException- if the file content can't be parsed
-
getNodeListForXPath
public NodeList getNodeListForXPath(Node ancestorNode, String xPath) throws XPathExpressionException Retrieves the NodeList for the given XPath from the given ancestor Node.- Parameters:
ancestorNode- the node from which the NodeList is to be readxPath- the XPath (relative to the ancestor node)- Returns:
- the NodeList for the given XPath
- Throws:
XPathExpressionException- if the given XPath can't be evaluated (e.g. because it does not exist)
-
getSingleNodeForXPath
Retrieves a single node at a given XPath from the given ancestor node.- Parameters:
ancestorNode- the node from which the Node is to be readxPath- the XPath (relative to the ancestor node)- Returns:
- the Node for the given XPath
- Throws:
XPathExpressionException- if the given XPath can't be evaluated (e.g. because it does not exist or because it does not point to a single node)
-
getStringValueForXpath
public String getStringValueForXpath(Node ancestorNode, String xPath) throws XPathExpressionException Retrieves the String content of a node at the given XPath.- Parameters:
ancestorNode- the parent node from which the Node content is to be readxPath- the XPath (relative to the ancestor node)- Returns:
- the String content of the node at the given XPath
- Throws:
XPathExpressionException- if the given XPath can't be evaluated (e.g. because it does not exist or because it does not point to a single node)
-
getIntValueForXpath
public int getIntValueForXpath(Node ancestorNode, String xPath) throws XPathExpressionException, NumberFormatException Retrieves the content of the node at the given XPath as int.- Parameters:
ancestorNode- the parent node from which the Node content is to be readxPath- the XPath (relative to the ancestor node)- Returns:
- the content of the node at the given XPath as int
- Throws:
XPathExpressionException- if the given XPath can't be evaluated (e.g. because it does not exist or because it does not point to a single node)NumberFormatException- if the content of the node at the XPath can't be converted to int
-
appendNode
Appends a new child node to a parent node.- Parameters:
parent- the parent nodenewChild- the child node to be appended
-
appendFileAsNode
public void appendFileAsNode(Node parent, String newChildFilePath, Map<String, String> replacements) throws IOException, SAXExceptionparses and appends the content of a file as a child node to the given parent node- Parameters:
parent- the parent nodenewChildFilePath- the path to the file whose content is to be added as a child nodereplacements- Map containing replacement strings, key: string to be replaced, value: replacement string- Throws:
IOException- if there's a problem accessing the fileSAXException- if the file can't be parsed
-
getXmlStringFromDocument
Converts the document to a formatted XML String (indentation level is 4) using default encoding (UTF-8).- Parameters:
document- The document to be converted to StringcdataElements- String array containing the names of all elements that are to be added within CDATA sections- Returns:
- the String representation of the given Document
-
getXmlStringFromDocument
Converts the document to a formatted XML String (indentation level is 4) using the given encoding.- Parameters:
document- The document to be converted to StringcdataElements- String array containing the names of all elements that are to be added within CDATA sectionsencoding- encoding to be used (added in the XML declaration)- Returns:
- the String representation of the given Document
-
cleanEmptyTextNodes
Removes text nodes that are empty or contain whitespace only if the parent node has at least one child of any of the following types: ELEMENT, CDATA, COMMENT. This is used to improve the XML format when using a transformer to do the formatting (whitespace nodes are interfering with indentation and line breaks). This method was modeled after a method by "user2401669" found on StackOverflow.
-