public class XmlTools extends Object
Modifier and Type | Field and Description |
---|---|
static org.dom4j.io.OutputFormat |
PRETTY_PRINT_OUTPUT_FORMAT |
Constructor and Description |
---|
XmlTools() |
Modifier and Type | Method and Description |
---|---|
static void |
closeQuietly(Closeable c)
Attempt to close a resource, swallowing any Exceptions thrown.
|
protected static org.dom4j.io.OutputFormat |
defaultPrettyPrintOutputFormat()
Returns a default output format for pretty printing an XML document.
|
static org.dom4j.Document |
fileToXMLDocument(File file)
Returns a dom4j XML document, when provided with a file
containing a well-formed XML document.
|
static String |
formatXML(org.dom4j.Document doc,
org.dom4j.io.OutputFormat outformat)
Returns a String representation of an XML document,
formatted according to a specified output format.
|
static String |
getElementValue(org.dom4j.Document doc,
String xpathExpr)
Returns the (text node) value of a specified element in a dom4j XML document.
|
static void |
main(String[] args) |
static String |
payloadJSONtoXML(String payload) |
static String |
payloadJSONtoXML(String payload,
String rootName) |
static String |
payloadXMLtoJSON_RestReplay(String payload) |
static String |
payloadXMLtoJSON(String payload) |
static String |
payloadXMLtoJSON(String payload,
String removeRoot)
For example, use removeRoot="root", which will look for an XML root element named "root",
or pass "" to get everything, including the root node itself.
|
static String |
prettyPrint(org.dom4j.Document document) |
static String |
prettyPrint(org.dom4j.Document document,
String indentString) |
static String |
prettyPrint(String xml) |
static String |
prettyPrintXML(org.dom4j.Document doc)
Returns a pretty printed String representation of an XML document.
|
static org.dom4j.Document |
setAttributeValue(org.dom4j.Document doc,
String xpathExpr,
String attributeName,
String attributeValue)
Sets the value of a specified attribute in a dom4j XML document.
|
static org.dom4j.Document |
setElementValue(org.dom4j.Document doc,
String xpathExpr,
String elementValue)
Sets the (text node) value of a specified element in a dom4j XML document.
|
static org.dom4j.Document |
textToXMLDocument(String xmlStr)
Returns a dom4j XML document, when provided with a String
representation of that XML document.
|
static void |
xmlDocumentToFile(org.dom4j.Document doc,
File file)
Writes a dom4j XML document to a file on disk.
|
public static final org.dom4j.io.OutputFormat PRETTY_PRINT_OUTPUT_FORMAT
protected static org.dom4j.io.OutputFormat defaultPrettyPrintOutputFormat()
public static String prettyPrintXML(org.dom4j.Document doc)
doc
- A dom4j XML Document.public static String formatXML(org.dom4j.Document doc, org.dom4j.io.OutputFormat outformat) throws Exception
doc
- A dom4j XML Document.outformat
- A dom4j output format.Exception
- if an error occurs in printing
the XML document to a String.public static org.dom4j.Document textToXMLDocument(String xmlStr) throws Exception
xmlStr
- A String representation of an XML document.Exception
public static org.dom4j.Document fileToXMLDocument(File file) throws Exception
file
- A file containing a well-formed XML document.Exception
public static void xmlDocumentToFile(org.dom4j.Document doc, File file) throws Exception
doc
- A dom4j XML document.file
- A file.Exception
public static void closeQuietly(Closeable c)
c
- A closeable resource.public static String getElementValue(org.dom4j.Document doc, String xpathExpr)
doc
- A dom4j XML document.xpathExpr
- An XPath expression intended to match a single element
in the XML document, in the default namespace.public static org.dom4j.Document setElementValue(org.dom4j.Document doc, String xpathExpr, String elementValue)
doc
- A dom4j XML document.xpathExpr
- An XPath expression intended to match a single element
in the XML document, in the default namespace.elementValue
- The value that the element should contain. If this
provided value is null, the element's value will be set to an empty string.public static org.dom4j.Document setAttributeValue(org.dom4j.Document doc, String xpathExpr, String attributeName, String attributeValue)
doc
- A dom4j XML document.xpathExpr
- An XPath expression intended to match a single element
in the XML document, in the default namespace.attributeName
- An attribute name.attributeValue
- The value that the attribute should contain.public static String prettyPrint(org.dom4j.Document document)
public static String payloadXMLtoJSON(String payload, String removeRoot)
public static void main(String[] args)
Copyright © 2016. All rights reserved.