Package com.consol.citrus.util
Class XMLUtils
- java.lang.Object
-
- com.consol.citrus.util.XMLUtils
-
public final class XMLUtils extends Object
Class providing several utility methods for XML processing.- Since:
- 2006
- Author:
- Jan Szczepanski, Christoph Deppisch
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static LSInputcreateLSInput()Creates LSInput from dom implementation.static LSOutputcreateLSOutput()Creates LSOutput from dom implementation.static LSParsercreateLSParser()Creates basic parser instance.static LSSerializercreateLSSerializer()Creates basic serializer instance.static NodefindNodeByName(Document doc, String pathExpression)Searches for a node within a DOM document with a given node path expression.static StringgetNodesPathName(Node node)Returns the path expression for a given node.static CharsetgetTargetCharset(Document doc)Try to find encoding for document node.static voidinitialize(XmlConfigurer xmlConfigurer)Initializes XML utilities with custom configurer.static Map<String,String>lookupNamespaces(Node referenceNode)Look up namespace attribute declarations in the specified node and store them in a binding map, where the key is the namespace prefix and the value is the namespace uri.static StringomitXmlDeclaration(String xml)Removes leading XML declaration from xml if present.static DocumentparseMessagePayload(String messagePayload)Parse message payload with DOM implementation.static StringprettyPrint(String xml)Pretty prints a XML string.static Stringserialize(Document doc)Serializes a DOM documentstatic voidstripWhitespaceNodes(Node element)Removes text nodes that are only containing whitespace characters inside a DOM tree.
-
-
-
Method Detail
-
initialize
public static void initialize(XmlConfigurer xmlConfigurer)
Initializes XML utilities with custom configurer.- Parameters:
xmlConfigurer-
-
createLSParser
public static LSParser createLSParser()
Creates basic parser instance.- Returns:
-
createLSSerializer
public static LSSerializer createLSSerializer()
Creates basic serializer instance.- Returns:
-
createLSInput
public static LSInput createLSInput()
Creates LSInput from dom implementation.- Returns:
-
createLSOutput
public static LSOutput createLSOutput()
Creates LSOutput from dom implementation.- Returns:
-
findNodeByName
public static Node findNodeByName(Document doc, String pathExpression)
Searches for a node within a DOM document with a given node path expression. Elements are separated by '.' characters. Example: Foo.Bar.Poo- Parameters:
doc- DOM Document to search for a node.pathExpression- dot separated path expression- Returns:
- Node element found in the DOM document.
-
stripWhitespaceNodes
public static void stripWhitespaceNodes(Node element)
Removes text nodes that are only containing whitespace characters inside a DOM tree.- Parameters:
element- the root node to normalize.
-
getNodesPathName
public static String getNodesPathName(Node node)
Returns the path expression for a given node. Path expressions look like: Foo.Bar.Poo where elements are separated with a dot character.- Parameters:
node- in DOM tree.- Returns:
- the path expression representing the node in DOM tree.
-
serialize
public static String serialize(Document doc)
Serializes a DOM document- Parameters:
doc-- Returns:
- serialized XML string
- Throws:
com.consol.citrus.exceptions.CitrusRuntimeException
-
prettyPrint
public static String prettyPrint(String xml)
Pretty prints a XML string.- Parameters:
xml-- Returns:
- pretty printed XML string
- Throws:
com.consol.citrus.exceptions.CitrusRuntimeException
-
lookupNamespaces
public static Map<String,String> lookupNamespaces(Node referenceNode)
Look up namespace attribute declarations in the specified node and store them in a binding map, where the key is the namespace prefix and the value is the namespace uri.- Parameters:
referenceNode- XML node to search for namespace declarations.- Returns:
- map containing namespace prefix - namespace url pairs.
-
parseMessagePayload
public static Document parseMessagePayload(String messagePayload)
Parse message payload with DOM implementation.- Parameters:
messagePayload-- Returns:
- DOM document.
- Throws:
com.consol.citrus.exceptions.CitrusRuntimeException
-
getTargetCharset
public static Charset getTargetCharset(Document doc)
Try to find encoding for document node. Also supports Citrus default encoding set as System property.- Parameters:
doc-- Returns:
-
-