public class XMLUtil extends Object implements XMLDogConstants
APP_NAME, DEBUG, EVENT_NODE_IDENTICAL, EVENT_NODE_MISMATCH, EVENT_NODE_SIMILAR
Constructor and Description |
---|
XMLUtil()
Default Constructor
|
Modifier and Type | Method and Description |
---|---|
static boolean |
areNonNullAndEqual(Object obj1,
Object obj2)
Checks if the input Objects are Non NULL and Equal
|
static boolean |
areNullorEqual(Object obj1,
Object obj2,
boolean ignoreWhitespace,
boolean ignoreCase)
Checks if both the Object arguments are equal (including if they are null)
|
static String |
generateXPath(Node node,
boolean ignoreWhitespace)
Generates XPath expression for the given node, relative to the Root of the Document
|
static String |
generateXPath(Node node,
boolean ignoreWhitespace,
boolean noValues)
Generates XPath expression with the option of the Node values and Node indexes are included
|
static String |
generateXPath(Node node,
boolean ignoreWhitespace,
boolean includeValues,
boolean noIndex)
Generates XPath expression with the option of the Node values appended
|
static String |
generateXPath(Node node,
String parentXPath,
boolean ignoreWhitespace,
boolean includeValues,
boolean noIndex)
Generates XPath expression with the option of the Node values appended
|
static String |
generateXPath(String childXPath)
Gets parent XPath from the child XPath
XPath string is obtained by stripping all the characters from the last "/" character |
static Document |
getDocument(String docPath)
Gets XML Document for a given document path
|
static String |
getNodeBasics(Node node)
Gets Nodes basic information such as Node name, type, value, namespace
and local name
|
static String |
getNodeTypeStr(int nodeType)
Gets Node type String for a given node type constant
|
static String |
getNoIndexXPath(String xPath)
Gets XPath expression without the indexes
|
static List |
getSimilarChildNodes(Node parent,
Node testNode,
boolean ignoreWhitespace)
Gets Nodes identical to a given test Node for a given parent Node
|
static List |
getSimilarChildXNodes(Node parent,
Node testNode,
boolean ignoreWhitespace)
Gets XNodes identical to a given test Node for a given parent Node
XNode in this case is used to store the position of the Node under the parent |
static String |
getText(Node node)
� * Return the text that a node contains.
|
static int |
getXPathNodeIndex(Node node,
boolean ignoreWhitespace)
Gets Node Index value for the XPath expression
e.g. |
static boolean |
isCommentNode(Node node)
Checks if the given Node is Comment Node
|
static boolean |
isStrElementNode(String elementName,
Node elementNode,
boolean ignoreCase)
Checks if Element Node is same as a Element name String
|
static boolean |
isWhitespaceTextNode(Node node)
Checks if the Node is Text Node and contains Whitespace only
|
static void |
log(String msg)
Prints msg to System.out
|
static void |
log(String msg,
Throwable t)
Prints msg and Exception to System.out
|
static void |
main(String[] args)
Main method used for debugging purpose only
|
static boolean |
nodesEqual(Node node1,
Node node2,
boolean ignoreWhitespace)
Checks if two Nodes are equal
Compares Nodes just by their Name, Type, Value and Namespace generically |
static boolean |
nodesSimilar(Node node1,
Node node2,
boolean ignoreWhitespace)
Checks if two Nodes are Similar
Compares Nodes just by their Name, Type, local name and Namespace generically |
protected static String |
normalize(String s,
boolean canonical)
Normalizes a given string
|
static String |
print(Node node,
boolean canonical,
boolean normalize)
Gets String representation of a given Node
|
static void |
printNodeBasics(Node node)
Prints a given Node
For debugging purpose only |
static Node |
removeChildren(Node node)
Removes all the children of a given Node
|
static void |
removeLeafNode(Node node)
Removes the Leaf Node
|
static String |
replaceElementText(String xmlFile,
Map elements,
boolean all,
boolean overwrite)
Replaces Elements subtree with the text value in the XML document
IMPORTANT!! This method removes entire subtree of all the Elements and puts the text as a child node. |
static String |
replaceElementText(String xmlFile,
String elementTag,
List values,
boolean overwrite)
Replaces Elements subtree with the text value in the XML document
IMPORTANT!! This method removes entire subtree of all the Elements and puts the text as a child node. |
static String |
replaceElementText(String xmlFile,
String tagName,
String value,
boolean all,
boolean overwrite)
Replaces Elements subtree with the text value in the XML document
IMPORTANT!! This method removes entire subtree of the Element and puts the text as a child node. |
protected static Attr[] |
sortAttributes(NamedNodeMap attrs)
Sorts Attributes of a given Node
|
public static List getSimilarChildNodes(Node parent, Node testNode, boolean ignoreWhitespace)
public static List getSimilarChildXNodes(Node parent, Node testNode, boolean ignoreWhitespace)
public static String replaceElementText(String xmlFile, String tagName, String value, boolean all, boolean overwrite) throws DOMException
xmlFile
- the fully qualified name of the XML filetagName
- the element tag whose text needs to be changedvalue
- the new text value for the Elementall
- true if all the matching Elements values needs to be changed, false otherwiseoverwrite
- true if File needs to be overwritten with the changes, false otherwiseDOMException
public static Document getDocument(String docPath) throws SAXException, IOException
docPath
- the path to the XML fileSAXException
IOException
Document
public static String replaceElementText(String xmlFile, Map elements, boolean all, boolean overwrite) throws DOMException
xmlFile
- the fully qualified name of the XML fileelements
- the Map containing Element tag names and corresponding Text valuesall
- true if all the matching Elements values needs to be changed, false otherwiseoverwrite
- true if File needs to be overwritten with the changes, false otherwiseDOMException
public static String replaceElementText(String xmlFile, String elementTag, List values, boolean overwrite) throws DOMException
xmlFile
- the fully qualified name of the XML fileelementTag
- the Element tag whose value is to be changedvalues
- the List containing values for each occerence of the Element tagoverwrite
- true if File needs to be overwritten with the changes, false otherwiseDOMException
public static Node removeChildren(Node node)
node
- the Node whose children is to be removedpublic static String getText(Node node)
public static String print(Node node, boolean canonical, boolean normalize)
node
- the Node to be converted to stringcanonical
- the flag to indicate if conversion is canonical or notnormalize
- true if normalized document is to be returned, false otherwise
protected static String normalize(String s, boolean canonical)
s
- the String to be normalizedprotected static Attr[] sortAttributes(NamedNodeMap attrs)
attrs
- the NamedNodeMap containing Node Attributespublic static String generateXPath(String childXPath)
childXPath
- the child XPathpublic static String generateXPath(Node node, boolean ignoreWhitespace)
public static String generateXPath(Node node, boolean ignoreWhitespace, boolean noValues)
node
- the Node whose XPath is to be foundignoreWhitespace
- the flag to indicate if Whitespace will be ignorednoValues
- the flag to indicate if Node values will be includedpublic static String generateXPath(Node node, boolean ignoreWhitespace, boolean includeValues, boolean noIndex)
node
- the Node whose XPath is to be foundignoreWhitespace
- the flag to indicate if Whitespace will be ignoredincludeValues
- the flag to indicate if Node values will be includednoIndex
- the flag to indicate if Node indexes are includedpublic static String generateXPath(Node node, String parentXPath, boolean ignoreWhitespace, boolean includeValues, boolean noIndex)
node
- the Node whose XPath is to be foundparentXPath
- the XPath of the parent NodeignoreWhitespace
- the flag to indicate if Whitespace will be ignoredincludeValues
- the flag to indicate if Node values will be includednoIndex
- the flag to indicate if Node indexes are includedpublic static String getNoIndexXPath(String xPath)
xPath
- the XPath expression with indexespublic static int getXPathNodeIndex(Node node, boolean ignoreWhitespace)
public static boolean nodesEqual(Node node1, Node node2, boolean ignoreWhitespace)
public static boolean nodesSimilar(Node node1, Node node2, boolean ignoreWhitespace)
public static void printNodeBasics(Node node)
public static String getNodeBasics(Node node)
public static void removeLeafNode(Node node)
public static String getNodeTypeStr(int nodeType)
public static boolean isWhitespaceTextNode(Node node)
public static boolean isCommentNode(Node node)
public static boolean isStrElementNode(String elementName, Node elementNode, boolean ignoreCase)
public static boolean areNullorEqual(Object obj1, Object obj2, boolean ignoreWhitespace, boolean ignoreCase)
public static boolean areNonNullAndEqual(Object obj1, Object obj2)
public static void log(String msg)
public static void main(String[] args)
Copyright © 2017. All rights reserved.