public final class XmlUtils
extends Object
| Modifier and Type | Class and Description |
|---|---|
static class |
XmlUtils.NodeType
The DOM NodeType.
|
| Modifier and Type | Field and Description |
|---|---|
protected org.w3c.dom.Document |
masterXmlDoc
The master xml doc.
|
| Constructor and Description |
|---|
XmlUtils(File fileObject)
Constructs an XmlUtil object from an XML file object.
|
XmlUtils(File fileObject,
org.xml.sax.EntityResolver er)
Constructs an XmlUtil object from an XML file object.
|
XmlUtils(File fileObject,
org.xml.sax.EntityResolver er,
boolean withLineNumbers)
Constructs an XmlUtil object from an XML file object.
|
XmlUtils(InputStream inStream)
Constructs an XmlUtil object to provide easy wrapping for XML operations.
|
XmlUtils(InputStream inStream,
boolean withLineNumbers)
Constructs an XmlUtil object to provide easy wrapping for XML operations.
|
XmlUtils(org.w3c.dom.Node node)
Constructs an XmlUtil object from an Node object.
|
XmlUtils(String strtoParse)
Instantiates a new xml utils.
|
XmlUtils(String strtoParse,
boolean withLineNumbers)
Instantiates a new xml utils.
|
XmlUtils(String rootName,
String rootValue)
Creates an empty tree with a root node of the specified name and value (a
null can be substituted for value if none is desired - this is often
preferrable).
|
| Modifier and Type | Method and Description |
|---|---|
org.w3c.dom.Attr |
addAttribute(org.w3c.dom.Node node,
String attributeName,
String attributeValue)
Adds an attribute to a specified Node.
|
org.w3c.dom.Node |
addCDataFieldUnder(org.w3c.dom.Node parentNode,
String fieldName,
String fieldValue)
Adds a new Node beneath a specified parent Node.
|
org.w3c.dom.Node |
addFieldUnder(org.w3c.dom.Node parentNode,
String fieldName)
Adds a new Node beneath a specified parent Node, but does NOT assign it a
value (the value is set to null).
|
org.w3c.dom.Node |
addFieldUnder(org.w3c.dom.Node parentNode,
String fieldName,
String fieldValue)
Adds a new Node beneath a specified parent Node.
|
org.w3c.dom.Node |
addRootField(String fieldName)
Adds a new Node directly beneath the XML tree's root.
|
org.w3c.dom.Node |
addRootField(String fieldName,
String fieldValue)
Adds a new Node directly beneath the XML tree's root.
|
void |
changeFieldValue(org.w3c.dom.Node aNode,
String newValue)
Allows updates to the first child node of a field (called "#text" that is
the actual location of a field's value).
|
void |
changeFieldValue(String nodeName,
String newValue)
Allows updates to the first child node of a field (called "#text" that is
the actual location of a field's value).
|
org.w3c.dom.Comment |
createComment(String comment)
Creates the xml comment.
|
void |
deleteNode(org.w3c.dom.Node aNode)
Deletes passed in Node and all of that Node's children.
|
int |
deleteNodesIn(List<org.w3c.dom.Node> manyNodes)
Deletes all the Nodes contained in a Node Vector.
|
int |
deleteNodesNamed(String nodeName)
Deletes all the Nodes in an XML tree named nodeName.
|
void |
dumpTo(OutputStream outStream,
String encoding)
Dumps the contents of the XML tree in raw form to the passed in
OutputStream.
|
void |
dumpTo(OutputStream outStream,
String encoding,
org.w3c.dom.Node rootNode)
Dumps the contents of the XML tree in raw form to the passed in
OutputStream.
|
org.w3c.dom.Node |
getAttribute(org.w3c.dom.Node node,
String attrName)
Gets the attribute.
|
Map<String,String> |
getAttributes(org.w3c.dom.Node node)
Gets the attributes.
|
Map<String,String> |
getAttributes(org.w3c.dom.Node parent,
String name)
Gets the attributes.
|
Boolean |
getBooleanAttribute(org.w3c.dom.Node node,
String attrName)
Gets the boolean attribute.
|
Boolean |
getBooleanAttribute(org.w3c.dom.Node node,
String attrName,
boolean def)
Gets the boolean attribute.
|
List<org.w3c.dom.Node> |
getChildNodes(org.w3c.dom.Node rootNode)
Gets a Vector of Nodes of all the children of a particular Node.
|
List<String> |
getChildValues(org.w3c.dom.Node rootNode)
Gets a Vector of Strings of all the values contained by the children of a
particular Node.
|
org.w3c.dom.Document |
getDoc()
Gets the doc.
|
org.w3c.dom.Node |
getFirstNodeContaining(String searchString)
Searches the XML tree for a field that contains searchString and returns
the address of that Node.
|
org.w3c.dom.Node |
getFirstNodeNamed(org.w3c.dom.Node rootNode,
String nodeName)
Searches ONLY THE FIRST LEVEL CHILDREN of specified rootNode and returns
the first instance of a Node named nodeName.
|
org.w3c.dom.Node |
getFirstNodeNamed(String searchString)
Searches the entire XML tree for a Node of specified String searchString
and returns the address of that Node.
|
List<org.w3c.dom.Node> |
getNodesContaining(String searchString)
Searches the XML tree for Nodes contaning String searchString and returns
the addresses of those Nodes in a Vector.
|
List<org.w3c.dom.Node> |
getNodesNamed(String searchString)
Searches the XML tree for Nodes named String searchString and returns
their addresses in a vector.
|
static XmlUtils.NodeType |
getNodeType(org.w3c.dom.Node node,
boolean checkAttrs,
boolean supportsMulti)
Gets the node type.
|
static XmlUtils.NodeType |
getNodeType(org.w3c.dom.Node node,
boolean checkAttrs,
boolean supportsMulti,
boolean ignoreAttrsInValueAttrNode,
boolean ignoreValuesInValueAttrNode)
Gets the node type.
|
String |
getNodeValue(org.w3c.dom.Node rootNode,
String name)
Gets the node value.
|
Number |
getNumberAttribute(org.w3c.dom.Node node,
String attrName)
Gets the number attribute.
|
Number |
getNumberAttribute(org.w3c.dom.Node node,
String attrName,
Number defaultValue)
Gets the number attribute.
|
org.w3c.dom.Node |
getRootNode()
Returns a live instance of the tree's root node that children can be
accessed, modified, added, and deleted from.
|
String |
getStringAttribute(org.w3c.dom.Node node,
String attrName)
Gets the string attribute.
|
String |
getValueOf(org.w3c.dom.Node aNode)
Looks at the first child node of aNode for the value of the field.
|
String |
getValueOf(String nodeName)
Searches the XML tree for a node called NodeName and then returns a
String of it's value (the first child of the Node).
|
static String |
getValueOfNode(org.w3c.dom.Node aNode)
Looks at the first child node of aNode for the value of the field.
|
boolean |
hasChildNodes(org.w3c.dom.Node node)
Returns whether this node has any children.
|
static boolean |
hasChildren(org.w3c.dom.Node node,
boolean checkAttrs)
Checks if node has any real children of any sort.
|
static boolean |
hasChildren(org.w3c.dom.Node node,
boolean checkAttrs,
boolean ignoreAttrsInValueAttrNode,
boolean ignoreValuesInValueAttrNode)
Checks if node has any real children of any sort.
|
static String |
removeXmlHeader(String xml)
Removes the xml header.
|
void |
setDoc(org.w3c.dom.Document d)
Sets the doc.
|
void |
writeToFile(File fileObject,
String encoding)
Dumps the contents of the XML tree into an XML formatted file as
specified by the File fileObject.
|
void |
writeToFile(String fileName,
String encoding)
Dumps the contents of the XML tree into an XML formatted file specified
in the path String fileNameString.
|
String |
xmlToString(String encoding)
Dumps the contents of the XML tree in raw form to the String.
|
String |
xmlToString(String encoding,
org.w3c.dom.Node rootNode)
Dumps the contents of the XML tree in raw form to the String.
|
public XmlUtils(File fileObject)
throws Exception
fileObject - An object of a valid XMl file. The file object's path will be
made absolute to ensure sanity.Exception - the exceptionpublic XmlUtils(File fileObject,
org.xml.sax.EntityResolver er)
throws Exception
fileObject - An object of a valid XMl file. The file object's path will be
made absolute to ensure sanity.er - the entity resolverException - the exceptionpublic XmlUtils(File fileObject,
org.xml.sax.EntityResolver er,
boolean withLineNumbers)
throws Exception
fileObject - An object of a valid XMl file. The file object's path will be
made absolute to ensure sanity.er - the entity resolverwithLineNumbers - if true add location informationException - the exceptionpublic XmlUtils(InputStream inStream)
throws Exception
inStream - A data stream that is parsed by the docBuilder for better
performance over file objects. Requested by Michael "Wang"
Mandel.Exception - the exceptionpublic XmlUtils(InputStream inStream,
boolean withLineNumbers)
throws Exception
inStream - A data stream that is parsed by the docBuilder for better
performance over file objects.withLineNumbers - if true add location informationException - the exceptionpublic XmlUtils(org.w3c.dom.Node node)
throws Exception
node - the nodeException - the exceptionpublic XmlUtils(String strtoParse)
throws Exception
strtoParse - the strto parseException - the exceptionpublic XmlUtils(String strtoParse,
boolean withLineNumbers)
throws Exception
strtoParse - the string to parsewithLineNumbers - if true add location informationException - the exceptionpublic XmlUtils(String rootName,
String rootValue)
throws Exception
rootName - The name you want to give to the tree's root node.rootValue - A value, if any, that you want the #text field of the root
node to contain. May be null.Exception - the exceptionpublic static XmlUtils.NodeType getNodeType(org.w3c.dom.Node node, boolean checkAttrs, boolean supportsMulti, boolean ignoreAttrsInValueAttrNode, boolean ignoreValuesInValueAttrNode)
node - the nodecheckAttrs - if true check attributes as wellsupportsMulti - if true parser supports multi data setsignoreAttrsInValueAttrNode - the ignore attrs in value attr nodeignoreValuesInValueAttrNode - the ignore values in value attr nodepublic static XmlUtils.NodeType getNodeType(org.w3c.dom.Node node, boolean checkAttrs, boolean supportsMulti)
node - the nodecheckAttrs - if true check attributes as wellsupportsMulti - if true parser supports multi data setspublic static boolean hasChildren(org.w3c.dom.Node node,
boolean checkAttrs)
node - the nodecheckAttrs - if true check attributespublic static boolean hasChildren(org.w3c.dom.Node node,
boolean checkAttrs,
boolean ignoreAttrsInValueAttrNode,
boolean ignoreValuesInValueAttrNode)
node - the nodecheckAttrs - if true check attributesignoreAttrsInValueAttrNode - the ignore attrs in value attr nodeignoreValuesInValueAttrNode - the ignore values in value attr nodepublic org.w3c.dom.Attr addAttribute(org.w3c.dom.Node node,
String attributeName,
String attributeValue)
node - A pre-existing Node on the XML tree under which the new Node
should be placed as a child. The node must implement the
Element interface, or a ClassCastException will occur.attributeName - The name of the attribute to be created.attributeValue - The value of the attribute to be created.public org.w3c.dom.Node addCDataFieldUnder(org.w3c.dom.Node parentNode,
String fieldName,
String fieldValue)
parentNode - A pre-existing Node on the XML tree under which the new Node
should be placed as a child. Not tested for specification of a
Node not already on the tree.fieldName - The name of the field to be created.fieldValue - What should be placed in the #text child of the new field.public org.w3c.dom.Node addFieldUnder(org.w3c.dom.Node parentNode,
String fieldName)
parentNode - A pre-existing Node on the XML tree under which the new Node
should be placed as a child. Not tested for specification of a
Node not already on the tree.fieldName - The name of the field to be created.public org.w3c.dom.Node addFieldUnder(org.w3c.dom.Node parentNode,
String fieldName,
String fieldValue)
parentNode - A pre-existing Node on the XML tree under which the new Node
should be placed as a child. Not tested for specification of a
Node not already on the tree.fieldName - The name of the field to be created.fieldValue - What should be placed in the #text child of the new field.public org.w3c.dom.Node addRootField(String fieldName)
fieldName - The name of the field to be created.public org.w3c.dom.Node addRootField(String fieldName,
String fieldValue)
fieldName - The name of the field to be created.fieldValue - What should be placed in the #text child of the new field.public void changeFieldValue(org.w3c.dom.Node aNode,
String newValue)
aNode - The actual Node object that you want to update.newValue - A String to replace the current #text field with.public void changeFieldValue(String nodeName,
String newValue)
nodeName - A String for the name of a node. Searches the XML tree for the
first Node of this name.newValue - A String to replace the current #text field with.public org.w3c.dom.Comment createComment(String comment)
comment - the commentpublic void deleteNode(org.w3c.dom.Node aNode)
aNode - The Node to remove.public int deleteNodesIn(List<org.w3c.dom.Node> manyNodes)
manyNodes - A Vector of Nodes to remove.public int deleteNodesNamed(String nodeName)
nodeName - Search name of Nodes to be deleted.public void dumpTo(OutputStream outStream,
String encoding,
org.w3c.dom.Node rootNode)
throws Exception
outStream - Stream object to write XmlTree to.encoding - the encodingrootNode - the root nodeException - the exceptionpublic void dumpTo(OutputStream outStream,
String encoding)
throws Exception
outStream - Stream object to write XmlTree to.encoding - the encodingException - the exceptionpublic org.w3c.dom.Node getAttribute(org.w3c.dom.Node node,
String attrName)
node - the nodeattrName - the attr namepublic Map<String,String> getAttributes(org.w3c.dom.Node node)
node - the nodepublic Map<String,String> getAttributes(org.w3c.dom.Node parent,
String name)
parent - the parentname - the namepublic Boolean getBooleanAttribute(org.w3c.dom.Node node,
String attrName)
node - the nodeattrName - the attr namepublic Boolean getBooleanAttribute(org.w3c.dom.Node node,
String attrName,
boolean def)
node - the nodeattrName - the attr namedef - the default valuepublic List<org.w3c.dom.Node> getChildNodes(org.w3c.dom.Node rootNode)
rootNode - The parent Node from which we'll get the child Nodes.public List<String> getChildValues(org.w3c.dom.Node rootNode)
rootNode - The parent Node from which we'll get the values of the
children.public org.w3c.dom.Document getDoc()
public org.w3c.dom.Node getFirstNodeContaining(String searchString)
searchString - the value to search for.public org.w3c.dom.Node getFirstNodeNamed(org.w3c.dom.Node rootNode,
String nodeName)
rootNode - The Node whose children we'll search for a Node called
nodeName.nodeName - The name of the Node we're looking for.public org.w3c.dom.Node getFirstNodeNamed(String searchString)
searchString - A field to search for.public List<org.w3c.dom.Node> getNodesContaining(String searchString)
searchString - A field value to search for.public List<org.w3c.dom.Node> getNodesNamed(String searchString)
searchString - A field name to search for.public String getNodeValue(org.w3c.dom.Node rootNode,
String name)
rootNode - the root nodename - the namepublic Number getNumberAttribute(org.w3c.dom.Node node,
String attrName)
node - the nodeattrName - the attr namepublic Number getNumberAttribute(org.w3c.dom.Node node,
String attrName,
Number defaultValue)
node - the nodeattrName - the attr namepublic org.w3c.dom.Node getRootNode()
public String getStringAttribute(org.w3c.dom.Node node,
String attrName)
node - the nodeattrName - the attr namepublic String getValueOf(org.w3c.dom.Node aNode)
aNode - A Node object to fetch the value from.public static String getValueOfNode(org.w3c.dom.Node aNode)
aNode - A Node object to fetch the value from.public String getValueOf(String nodeName)
nodeName - String of the node to search for.public boolean hasChildNodes(org.w3c.dom.Node node)
node - the nodetrue if this node has any children,
false otherwise.public void setDoc(org.w3c.dom.Document d)
d - the new docpublic void writeToFile(File fileObject,
String encoding)
throws Exception
fileObject - The pre-created File object to dump the tree into.encoding - the encodingException - the exceptionpublic void writeToFile(String fileName,
String encoding)
throws Exception
fileName - The filename to dump the tree into.encoding - the encodingException - the exceptionpublic String xmlToString(String encoding)
throws Exception
encoding - the encodingException - the exceptionpublic String xmlToString(String encoding,
org.w3c.dom.Node rootNode)
throws Exception
encoding - the encodingrootNode - the root nodeException - the exceptionpublic static String removeXmlHeader(String xml)
xml - the xmlCopyright © 2010-2020 Toolsverse. All Rights Reserved.