public class Node
extends java.lang.Object
Document
for more information.Most of methods of this class requires XML element/node references as inputs. If the node references are - non-zero invalid (positive) references, and 'bus.xml.nom.exceptions.enabled' property is enabled, then the method throws XMLException - zero and negative values, no effect
Modifier and Type | Field and Description |
---|---|
static int |
WRITE_HEADER
Use this constant when you want to include xml header while serialization
in Node.writeToFile()
|
static int |
WRITE_NORMAL
Use this constant when you want to do normal serialization in
Node.write()
|
static int |
WRITE_PRETTY
Use this constant when you want to do pretty serialization in
Node.write()
|
Modifier and Type | Method and Description |
---|---|
static int |
add(int node,
int dest)
Adds the specified node to the destination node.
|
static int |
add(int from,
int to,
int dest)
Adds a range of nodes to the destination node.
|
static int |
append(int node,
int destination)
Appends the specified node to most right sibling of the destination node.
|
static int |
append(int from,
int to,
int dest)
Appends a range of nodes to most right sibling of the destination node.
|
static int |
appendToChildren(int node,
int destination)
Appends the specified node to the children of the destination node.
|
static int |
appendToChildren(int from,
int to,
int dest)
Appends the range of nodes to the children of the destination node.
|
static void |
captureTaceInMap(int node) |
static int |
clone(int node,
boolean deep)
Returns a clone of the node.
|
static int |
createCDataElement(java.lang.String tag,
java.lang.String text,
int parent)
Creates a new CDATA element by name 'tag' with 'text' as CData section,
and appends it to 'parent'.
|
static int |
createCDataElementNS(java.lang.String name,
java.lang.String CDataContent,
int parent)
Deprecated.
in C3, use createCDataElementWithParentNS()
|
static int |
createCDataElementNS(java.lang.String name,
java.lang.String CDataContent,
java.lang.String prefix,
int parent)
Deprecated.
in C3, use createCDataElementNSWithPrefix()
|
static int |
createCDataElementNS(java.lang.String name,
java.lang.String CDataContent,
java.lang.String prefix,
java.lang.String namespaceURI,
int parent)
Creates a new CDATA element with input namespace details and appends it
to the children of the indicated parent.
|
static int |
createCDataElementNSWithPrefix(java.lang.String name,
java.lang.String CDataContent,
java.lang.String prefix,
int parent)
Creates a new CDATA element with input 'prefix' and appends it to the
children of the indicated parent.
|
static int |
createCDataElementWithParentNS(java.lang.String name,
java.lang.String CDataContent,
int parent)
Creates a new CDATA element with parent namespace details and appends it
to the children of the indicated parent.
|
static int |
createElement(java.lang.String tag,
int parent)
Creates a new element with the given tag and appends it to parent.
|
static int |
createElementNS(java.lang.String name,
java.lang.String content,
int parent)
Deprecated.
in C3, use createElementWithParentNS()
|
static int |
createElementNS(java.lang.String name,
java.lang.String content,
java.lang.String prefix,
int parent)
Deprecated.
in C3, use createElementNSWithPrefix()
|
static int |
createElementNS(java.lang.String name,
java.lang.String content,
java.lang.String prefix,
java.lang.String namespaceURI,
int parent)
Creates a new element with the given namespace details and appends it to
the children of the indicated parent.
|
static int |
createElementNSWithPrefix(java.lang.String name,
java.lang.String content,
java.lang.String prefix,
int parent)
Creates a new element with namespace prefix 'prefix' and appends it to
the children of the indicated parent.
|
static int |
createElementWithParentNS(java.lang.String name,
java.lang.String content,
int parent)
Creates a new element with parent namespace details and appends it to the
children of the indicated parent.
|
static int |
createTextElement(java.lang.String tag,
java.lang.String text,
int parent)
Creates a new element, appends it to parent, and creates a new text node
which is appended to the newly created element.
|
static byte[] |
deflate(int node)
It compresses the content of the node.This method takes the data of the
NOM node, and does not include the element name or attributes of the NOM
node.
|
static void |
delete(int node)
Deletes a single node and its children
|
static void |
delete(int from,
int to)
Deletes the range of nodes specified in 'from' and 'to'
|
static int |
duplicate(int node)
Duplicates the specified node including all the child elements.
|
static int |
duplicate(int from,
int to)
Deprecated.
in C2. Use clone(int node, boolean deep).
|
static int |
duplicateAndAdd(int from,
int to,
int dest)
Duplicates and adds a range of nodes to the destination node.
|
static int |
duplicateAndAppend(int from,
int to,
int dest)
Duplicates and appends a range of nodes to most right sibling of the
destination node.
|
static int |
duplicateAndAppendToChildren(int from,
int to,
int dest)
Duplicates and appends the range of nodes to the children of the
destination node.
|
static int |
duplicateAndInsert(int from,
int to,
int dest)
Duplicates and inserts a range of nodes into the destination node.
|
static int |
duplicateAndInsertInChildren(int from,
int to,
int dest)
Duplicates and inserts the range of nodes into the children of the
destination node.
|
static int |
duplicateElement(int element)
Duplicates an Element.
|
static java.lang.String |
getAttribute(int node,
java.lang.String name)
Returns the value of the attribute for the specified attribute name of
the given node, if the node is of type Element
|
static java.lang.String |
getAttribute(int node,
java.lang.String name,
java.lang.String defaultValue)
Returns the value of the given attribute, or the default value if the
attribute does not exist
|
static int |
getAttributeInt(int node,
java.lang.String name,
int defaultValue)
Returns the value of the given attribute value as integer, or the default
value if the attribute does not exist or is not an integer value.
|
static java.lang.String |
getAttributeLocalName(int node,
int index)
Returns the local name of the attribute with the specified index in the
node
|
static java.lang.String |
getAttributeName(int node,
int index)
Returns the name of the attribute with the specified index in the node
|
static java.lang.String |
getAttributeNamespaceURI(int node,
int index)
Returns the namespace URI associated with the namespace prefix of the
attribute with the specified index in the node
|
static java.lang.String |
getAttributeNS(int node,
java.lang.String name,
java.lang.String uri)
Returns the value of the attribute for the specified attribute name which
is bound with the given namespace URI of the given node, if the node is
of type Element
|
static java.lang.String |
getAttributeNS(int node,
java.lang.String name,
java.lang.String uri,
java.lang.String defaultValue)
Returns the value of the given attribute which is bound with the give
URI, or the default value if the attribute does not exist
|
static java.lang.String |
getAttributePrefix(int node,
int index)
Returns the namespace prefix of the attribute with the specified index in
the node
|
static int |
getCompoundType(int node)
Returns the compound type of the node
|
static java.lang.String |
getData(int node)
Returns the data in the node, if it is of type Text, otherwise collects
all data of all children nodes and returns that.
|
static java.lang.String |
getData(int node,
java.lang.String defaultValue)
Deprecated.
in C1, use getDataWithDefault()
|
static byte[] |
getDataBytesForElement(int node,
java.lang.String tag,
byte[] defaultValue)
Retrieves the data of the first child of node that has the specified
tagname
|
static byte[] |
getDataBytesForElementWithNS(int node,
java.lang.String tag,
java.lang.String uri,
byte[] defaultValue)
Retrieves the data of the first child of node that has the specified
tagname and which is bound with the given namespace URI.
|
static byte[] |
getDataBytesWithDefault(int node,
byte[] defaultValue)
Returns the data of the node in byte array, if it is of type Text, otherwise collects
all data of all children nodes and returns that.
|
static java.lang.String |
getDataElement(int node,
java.lang.String tag,
java.lang.String defaultValue)
Retrieves the data of the first child of node that has the specified
tagname.
|
static java.lang.String |
getDataElementNS(int node,
java.lang.String tag,
java.lang.String uri,
java.lang.String defaultValue)
Retrieves the data of the first child of node that has the specified
tagname and which is bound with the given namespace URI.
|
static java.lang.String |
getDataOfFirstDataChild(int node,
java.lang.String defaultValue)
Deprecated.
use getDataWithDefault(int node, String defaultValue) method.
|
static int |
getDataType(int node)
Returns the datatype of the node for which the attribute dt is set
|
static java.lang.String |
getDataWithDefault(int node,
java.lang.String defaultValue)
Returns the data in the node, if it is of type Text, otherwise collects
all data of all children nodes and returns that.
|
static Document |
getDocument(int node)
Returns a Document object to which the node belongs.
|
static int |
getElement(int node,
java.lang.String tagName)
Returns a first child node with the given tag name.
|
static int |
getElementNS(int node,
java.lang.String tagName,
java.lang.String uri)
Returns a child node, with the given name which is bound with the given
URI or 0.
|
static int |
getFirstChild(int node)
Returns the first child of the node.It can be either a data node or an
element node.
|
static int |
getFirstChildElement(int node)
Returns the first child element of the node
|
static int |
getFirstDataNode(int node)
Returns the first child of node that is of type data
|
static int |
getFirstElement(int node)
Returns the first child of node which is of type element
|
static int |
getLastChild(int node)
Returns the last child of the node.It can be either a data node or an
element node.
|
static int |
getLastChildElement(int node)
Returns the last child element of the node
|
static java.lang.String |
getLocalName(int node)
Returns the local name of the node.
|
static java.lang.String |
getName(int node)
Returns the tagname of the node, if it is of type element
|
static java.lang.String |
getNamespaceURI(int node)
Returns the namespace URI associated with the prefix of the element.
|
static int |
getNextElement(int node)
Returns the next (right) sibling element of the node
|
static int |
getNextSibling(int node)
Returns the next (right) sibling of the node.It can be either a data node
or an element node.
|
static int |
getNumAttributes(int node)
Returns the number of attributes in the node, if it is of type element
|
static int |
getNumChildElements(int node)
Returns the number of child elements of the node
|
static int |
getNumChildren(int node)
Returns the number of children of the node.It counts both data nodes and
element nodes.
|
static int |
getNumNextSiblingElements(int node)
Returns the number of sibling elements next to (on the right of) the node
|
static int |
getNumNextSiblings(int node)
Returns the number of siblings next to (on the right of) the node
including elements and text
|
static int |
getNumPreviousSiblingElements(int node)
Returns the number of sibling elements previous to (on the left of) the
node
|
static int |
getNumPreviousSiblings(int node)
Returns the number of siblings previous to (on the left of) the node
including elements and text
|
static int |
getNumSiblingElements(int node)
Returns the number of sibling elements of the node
|
static int |
getNumSiblings(int node)
Returns the number of siblings of the node.It counts both data nodes and
element nodes.
|
static int |
getParent(int node)
Returns the parent node reference of the given node reference
|
static java.lang.String |
getPrefix(int node)
Returns the prefix of the tag name of the element.
|
static int |
getPreviousElement(int node)
Returns the previous (left) sibling element of the node
|
static int |
getPreviousSibling(int node)
Returns the previous (left) sibling of the node.It can be either a data
node or an element node.
|
static int |
getRoot(int node)
Returns the root node of the node (DOM: document element).
|
static int |
getType(int node)
Returns the type of the node
|
static int |
insert(int node,
int dest)
Inserts the given node into the destination node.
|
static int |
insert(int from,
int to,
int dest)
Inserts a range of nodes into the destination node.
|
static int |
insertInChildren(int from,
int to,
int dest)
Inserts the range of nodes into the children of the destination node.
|
static boolean |
isValidNode(int supposedNode)
Determines whether the supposed node is a valid node or not.
|
static int |
listCompare(int from1,
int to1,
int from2,
int to2,
int depth,
int flags)
Compares two lists of XML nodes for equality
|
static boolean |
nameEquals(int node,
java.lang.String name)
Checks whether the name of the node is equal to the given 'name'.
|
static int |
removeAttribute(int node,
java.lang.String name)
Removes the attribute with the specified name in the node, and returns
the result of this action (0 if succeeded).
|
static int |
removeAttributeNS(int node,
java.lang.String name,
java.lang.String URI)
Removes the attribute with the specified name & URI in the node, and
returns the result of this action (0 if succeeded).
|
static int |
removeAttributesRecursive(int node,
java.lang.String name1,
java.lang.String name2,
java.lang.String name3,
java.lang.String name4)
Removes the attributes with the specified names recursively from the node
and children, and returns the result of this action (0 if succeeded).
|
static int |
rewriteTextElement(int node,
java.lang.String tag,
java.lang.String data)
Deprecated.
in C2. Use setDataElement(int node, String tag, String data).
|
static void |
setAttribute(int node,
java.lang.String name,
java.lang.String value)
Sets the attribute in the node, if it is of type element
|
static void |
setAttributeNS(int node,
java.lang.String name,
java.lang.String uri,
java.lang.String value)
Sets the attribute which is bound with the given URI in the node, if it
is of type element.
|
static void |
setCDataBytesForElement(int node,
java.lang.String tag,
byte[] data)
Finds a first level child element of 'node' by name 'tag' and replaces
the CData section by the given 'data'.
|
static int |
setCDataElement(int node,
java.lang.String tag,
java.lang.String data)
Finds a first level child element of 'node' by name 'tag' and replaces
the CData section by the given 'data'.
|
static void |
setData(int node,
java.lang.String data)
Sets the data in the node, if it is of type Text
|
static void |
setDataBytes(int node,
byte[] data)
Sets the given data in the node
|
static void |
setDataBytesForElement(int node,
java.lang.String tag,
byte[] data)
Rewrites the data children of the element 'node' to have a new element as
last child with 'tag' as element name .
|
static void |
setDataBytesForElementWithNS(int node,
java.lang.String tag,
java.lang.String uri,
byte[] data)
Rewrites the data children of the element 'node' to have a new element as
last child with 'tag' as element name and binds with the given namespace
URI.
|
static int |
setDataElement(int node,
java.lang.String tag,
java.lang.String data)
Rewrites the data children of the element 'node' to have a new element as
last child with 'tag' as element name .
|
static int |
setDataElementNS(int node,
java.lang.String tag,
java.lang.String uri,
java.lang.String data)
Rewrites the data children of the element 'node' to have a new element as
last child with 'tag' as element name and binds with the given namespace
URI.
|
static void |
setName(int node,
java.lang.String name)
Sets the tagname of the node, if it is of type element
|
static int |
setNSDefinition(int node,
java.lang.String prefix,
java.lang.String uri)
Set the namespace definition to the node.
|
static int |
setNSPrefix(int node,
java.lang.String prefix)
Set the namespace prefix to the node.
|
static int |
setNSPrefixRecursive(int node,
java.lang.String oldPrefix,
java.lang.String newPrefix)
Set the old namespace prefix to the new namespace prefix recursively to
the given node and its children.
|
static int |
unlink(int node)
Unlinks a given node and it children from the tree it is in
|
static int |
unlink(int from,
int to)
Unlinks a range of nodes from the tree they are in
|
static byte[] |
write(int node,
boolean pretty)
Writes the contents of the node into the byte-array
|
static byte[] |
write(int from,
int to,
int pretty)
Writes the given range of nodes to byte-array
|
static void |
writeToFile(int from,
int to,
java.lang.String file,
int pretty)
Writes a range of nodes to a file.
|
static java.lang.String |
writeToString(int node,
boolean pretty)
Writes the contents of the node into the string
|
static java.lang.String |
writeToStringWithNSInfo(int node,
boolean pretty)
Writes the contents of the node with namespace information(defined in
ancestor node)into the string
|
static byte[] |
writeWithNSInfo(int from,
int to,
int pretty)
Writes the given range of nodes to byte-array with namespace
information(defined in ancestor node)
|
public static final int WRITE_NORMAL
public static final int WRITE_PRETTY
public static final int WRITE_HEADER
public static boolean isValidNode(int supposedNode)
public static int listCompare(int from1, int to1, int from2, int to2, int depth, int flags)
from1
- the start reference of the first set of nodesto1
- the end reference of the first set of nodesfrom2
- the start reference of the second set of nodesto2
- the end reference of the second set of nodesdepth
- depth until which to compareflags
- This specifies the behavior of the comparison
String xml1 = "<root><ele1 att1=\"att1\">data1</ele1></root>";
String xml2 = "<root><ele1 att1=\"att1\">data1</ele1></root>";
try{
int root1 = doc.parseString(xml1); int root2 = doc.parseString(xml2);
int flag = Node.listCompare(root1, root1, root2, root2, 0, 0);
//Input nodes are matched for equality even though data of <ele1> is different for both input xml.
}catch(Exception e){
e.printStackTrace();
}
String xml1 = "<root><ele1 att1=\"att1\">data1</ele1></root>";
String xml2 = "<root><ele1 att1=\"att1\">data2</ele1></root>";
try{
int root1 = doc.parseString(xml1); int root2 = doc.parseString(xml2);
int flag = Node.listCompare(root1, root1, root2, root2, 0, 1);
//Input nodes are matched for equality even though data of <ele1> is different for both input xml.
}catch(Exception e){
e.printStackTrace();
}
String xml1 = "<root><ele1 att1=\"att1\">data1</ele1></root>";
String xml2 = "<root><ele1 att1=\"att2\">data1</ele1></root>";
try{
int root1 = doc.parseString(xml1); int root2 = doc.parseString(xml2);
int flag = Node.listCompare(root1, root1, root2, root2, 0, 2);
//Input nodes are matched for equality even though values of "attr1"is different for both input xml.
}catch(Exception e){
e.printStackTrace();
}
String xml1 = "<p:root xmlns:p=\"http://www.w3.org/TR/html4/\"><p:ele1 att1=\"att1\">data1</p:ele1></p:root>";
String xml2 = "<q:root xmlns:q=\"http://www.w3.org/TR/html4/\"><q:ele1 att1=\"att1\">data1</q:ele1></q:root>";
try{
int root1 = doc.parseString(xml1); int root2 = doc.parseString(xml2);
int flag = Node.listCompare(root1, root1, root2, root2, 0, 8);
//Input nodes are matched for equality even though prefixes are different.
}catch(Exception e){
e.printStackTrace();
}
String xml1 = "<root><row><col1>Apples</col1><col2>Bananas</col2></row></root>";
String xml2 = "<root><row><col1>Apples</col1><col2>Bananas</col2><col3>Oranges</col3></row></root>";
try{
int root1 = doc.parseString(xml1); int root2 = doc.parseString(xml2);
int flag = Node.listCompare(root1,0,root2,0,0,128);
//Input nodes are matched for equality even though the order of child elements are different.
}catch(Exception e){
e.printStackTrace();
}
String xml1 = "<root><row><col1>Apples</col1><col2>Bananas</col2></row></root>";
String xml2 = "<root><row><col1>Apples</col1><col2>Bananas</col2></row></root>";
try{
int root1 = doc.parseString(xml1); int root2 = doc.parseString(xml2);
int flag = Node.listCompare(root1,0,root2,0,0,128);
//Input nodes are matched for equality.
}catch(Exception e){
e.printStackTrace();
}
String xml1 = "<p:root xmlns:p=\"http://www.w3.org/TR/html4/\"><p:row><p:col1>Apples</p:col1><p:col2>Bananas</p:col2></p:row></p:root>";
String xml2 = "<q:root xmlns:q=\"http://www.w3.org/TR/html4/\"><q:row><q:col1>Bananas</q:col1><q:col2>Apples</q:col2><q:col3>Oranges</q:col3></q:row></q:root>";
try{
int root1 = doc.parseString(xml1); int root2 = doc.parseString(xml2);
int flag = Node.listCompare(root1,0,root2,0,0,136);
//Input nodes are matched for equality.
}catch(Exception e){
e.printStackTrace();
}
XMLException
public static java.lang.String getPrefix(int node)
node
- The XML element for which the prefix is required
int element = document.load("<prefix:element xmlns:prefix=\"http://cordys.com/test\"/>".getBytes());
Node.getPrefix(element);
// returns "prefix"
public static java.lang.String getLocalName(int node)
node
- The XML element for which the localname is required
int element = document.load("<prefix:element xmlns:prefix=\"http://cordys.com/test\"/>".getBytes());
Node.getLocalName(element);
// returns "element"
public static java.lang.String getNamespaceURI(int node)
node
- The XML element for which the namespace URI is required.
int element = 0;
try {
element = document.load("<prefix:element xmlns:prefix=\"http://cordys.com/test\"/>".getBytes());
String namespace = Node.getNamespaceURI(element); // returns "http://cordys.com/test"
element = document.load("<element xmlns=\"http://cordys.com/test\"/>".getBytes());
namespace = Node.getNamespaceURI(element); // returns "http://cordys.com/test"
element = document.load("<element/>".getBytes());
namespace = Node.getNamespaceURI(element); // returns null
element = document.load("<element xmlns=\"test\"><child><grandchild xmlns=\"\"/></child></element>".getBytes());
int childElement = Node.getFirstChild(element);
int grandchildElement = Node.getFirstChild(childElement);
namespace = Node.getNamespaceURI(grandchildElement); // returns null
}
finally {
Node.delete(element);
}
public static int getRoot(int node)
node
- The XML element reference.
String xmlStr = "<root xmlns:h=\"http://www.w3.org/TR/html4\" ><h:table></h:table></root>";
try {
int result = document.load(xmlStr.getBytes());
result = Node.getFirstChildElement(result);
result = Node.getRoot(result);
assertEquals("root", Node.getName(result));
} catch (XMLException e) {
e.printStackTrace();
}
@Deprecated public static int rewriteTextElement(int node, java.lang.String tag, java.lang.String data)
node
- The XML element referencetag
- The tagname of the element to be rewrittendata
- The data of the element that is to be rewritten for the
matched element with the given tag name.public static int setDataElement(int node, java.lang.String tag, java.lang.String data)
node
- XML element from which the operation startstag
- element to be operated, if empty, then 'node' will be operateddata
- data to be added
int element = document.load("<element/>".getBytes());
Node.setDataElement(element, "childElement", "value");
// This will change the element node as
// <element>
// <childElement>value</childElement>
// </element>
public static int setDataElementNS(int node, java.lang.String tag, java.lang.String uri, java.lang.String data)
node
- XML element from which the operation startstag
- element to be operated, if empty, then 'node' will be operateduri
- The namespace URI.data
- data to be addedint element = document.load("<element xmlns:ns=\"http://cordys.com/test\"/>".getBytes("UTF-8")); Node.setDataElementNS(element,"childElement", "http://cordys.com/test", "value"); //The result will be as below <element xmlns:ns="http://cordys.com/test"> <childElement xmlns="http://cordys.com/test">value </childElement> </element>
int element = document.load("<element/>".getBytes("UTF-8")); Node.setDataElementNS(element,"childElement", "http://cordys.com/test", "value"); //The result will be as below <element> <childElement xmlns="http://cordys.com/test">value</childElement> </element>
int element = document.load("<element/>".getBytes("UTF-8")); Node.setDataElementNS(element,"", "http://cordys.com/test", "value"); //The result will be as below <element>value</element>
public static int setCDataElement(int node, java.lang.String tag, java.lang.String data)
node
- XML element from which the operation startstag
- element to be operated, if empty, then 'node' will be operateddata
- data to be added
String xml =
"<root>
<employee> Cordys employee </employee>
<employee1>
<test> Cordys employee </test>
</employee1>
</root>";
int root = document.load(xml.getBytes("UTF-8"));
// Changes 'employee' value from "Cordys employee" to "<![CDATA[testing data]]>"
Node.setCDataElement(root,"employee","testing data");
// Removes 'test' from 'employee1', and adds "<![CDATA[testing data]]>" to 'employee1'
Node.setCDataElement(root,"employee1","testing data");
// Changes 'employee' value from "Cordys employee" to "<![CDATA[testing data]]>"
int emp = Node.getFirstChild(root);
Node.setCDataElement(emp,"","testing data");
public static int createElement(java.lang.String tag, int parent)
tag
- The tag for the new elementparent
- The parent for the new element
Document oDocument = new Document();
String xmlStr = "<root xmlns:h=\"http://www.w3.org/TR/html4\" ><h:table></h:table></root>";
try {
int result = oDocument.load(xmlStr.getBytes());
Node.createElement("chair", result);
result = Node.getLastChildElement(result);
assertEquals("chair", Node.getName(result));
} catch (XMLException e) {
e.printStackTrace();
}
public static int createTextElement(java.lang.String tag, java.lang.String text, int parent)
tag
- The tag for the new elementtext
- The data for the new nodeparent
- The parent for the new element
Document oDocument = new Document();
String xmlStr = "<root xmlns:h=\"http://www.w3.org/TR/html4\" ><h:table></h:table></root>";
try {
int result = oDocument.load(xmlStr.getBytes());
Node.createTextElement("chair", "createdText", result);
result = Node.getLastChildElement(result);
assertEquals("chair", Node.getName(result));
} catch (XMLException e) {
e.printStackTrace();
}
public static int createCDataElement(java.lang.String tag, java.lang.String text, int parent)
tag
- The tag for the new elementtext
- The CData for the new nodeparent
- The parent for the new element
Document oDocument = new Document();
String xmlStr = "<root xmlns:h=\"http://www.w3.org/TR/html4\" ><h:table></h:table></root>";
try {
int result = oDocument.load(xmlStr.getBytes());
Node.createCDataElement("chair", "createdText", result);
result = Node.getLastChildElement(result);
assertEquals("<chair><![CDATA[createdText]]></chair>", Node.writeToString(
result, false));
} catch (XMLException e) {
e.printStackTrace();
}
public static java.lang.String getDataElement(int node, java.lang.String tag, java.lang.String defaultValue)
node
- The element referencetag
- The tag name of the elementdefaultValue
- That is to be returned if the given element tag is not
available or the data to the element is not available.
int element = document.load("<element><childElement>value</childElement></element>".getBytes());
String data = Node.getDataElement(element, "childElement", "noData"); // returns value
data = Node.getDataElement(element, "someElement", "noData"); // returns noData
public static java.lang.String getDataElementNS(int node, java.lang.String tag, java.lang.String uri, java.lang.String defaultValue)
node
- The element referencetag
- The tag name of the elementuri
- The namespace URI of the elementdefaultValue
- That is to be returned if the given element tag is not
available or the data to the element is not available.
int element = document.load("<element xmlns:ns=\"http://cordys.com/test\"><ns:childElement>value</ns:childElement></element>".getBytes());
String data = Node.getDataElementNS(element, "childElement", "http://cordys.com/test", "noData"); // returns value
data = Node.getDataElementNS(element, "childElement", "http://cordys.com/test1", "noData") // returns noData
data = Node.getDataElementNS(element, "childElement1", "http://cordys.com/test", "noData") // returns noData
public static void delete(int from, int to)
from
- The start element reference for deleteto
- The end element reference for delete
Document oDocument = new Document();
String xmlStr = "<root><table></table><chair/></root>";
try {
int result = oDocument.load(xmlStr.getBytes());
int from = Node.getFirstChild(result);
int to = Node.getLastChild(result);
Node.delete(from, to);
assertEquals("<root/>", Node.writeToString(result, false));
} catch (XMLException e) {
e.printStackTrace();
}
public static void delete(int node)
node
- The element reference to be deleted
Document oDocument = new Document();
String xmlStr = "<root xmlns:h=\"http://www.w3.org/TR/html4\" ><h:table></h:table><chair/></root>";
try {
int result = oDocument.load(xmlStr.getBytes());
Node.delete(Node.getLastChildElement(result));
result = Node.getLastChildElement(result);
assertEquals("h:table", Node.getName(result));
} catch (XMLException e) {
e.printStackTrace();
}
public static void captureTaceInMap(int node)
public static int unlink(int from, int to)
from
- The start element reference for deleteto
- The end element reference for delete
Document oDocument = new Document();
String xmlStr = "<root><table></table><chair/><bench/></root>";
try {
int result = oDocument.load(xmlStr.getBytes());
int child1 = Node.getFirstChildElement(result);
int child2 = Node.getNextSibling(child1);
Node.unlink(child1, child2);
result = Node.getFirstChildElement(result);
assertEquals("bench", Node.getName(result));
} catch (XMLException e) {
e.printStackTrace();
}
public static int unlink(int node)
node
- The element reference to be unlinked.
Document oDocument = new Document();
String xmlStr = "<root><table></table><chair/><bench/></root>";
try {
int result = oDocument.load(xmlStr.getBytes());
int child1 = Node.getFirstChildElement(result);
Node.unlink(child1);
result = Node.getFirstChildElement(result);
assertEquals("chair", Node.getName(result));
} catch (XMLException e) {
e.printStackTrace();
}
public static int insert(int from, int to, int dest)
from
- The start element reference to be inserted.to
- The end element reference to be inserted. If from and to are
same, only that node will be inserted. If to is specified as
0, all siblings of 'from' will be inserteddest
- The destination node reference forwhich the range of elements
are inserted as immediate left siblings
int root = document.parseString("<root><element1/><element2/></root>");
int newelement = document.parseString("<newElement>value</newElement>");
Node.insert(newelement, newelement, Node.getFirstChild(root));
// After this operation, root element will be as shown below
// <root>
// <newElement>value</newElement>
// <element1/>
// <element2/>
// </root>
public static int insert(int node, int dest)
node
- The element reference to be inserted.dest
- The destination node reference forwhich the given element is
inserted as immediate left sibling
int root = document.parseString("<root><element1/><element2/></root>");
int newelement = document.parseString("<newElement>value</newElement>");
Node.insert(newelement, Node.getFirstChild(root));
// After this operation, root element will be as shown below
// <root>
// <newElement>value</newElement>
// <element1/>
// <element2/>
// </root>
public static int add(int from, int to, int dest)
from
- The start element reference to be added.to
- The end element reference to be added. If from and to are
same, only that node will be added. If to is specified as 0,
all siblings of 'from' will be addeddest
- The destination node reference forwhich the range of elements
are added as immediate right siblings
int root = document.parseString("<root><element1/><element2/></root>");
int newelement = document.parseString("<newElement>value</newElement>");
Node.add(newelement, newelement, Node.getFirstChild(root));
// After this operation, root element will be as shown below
// <root>
// <element1/>
// <newElement>value</newElement>
// <element2/>
// </root>
public static int add(int node, int dest)
node
- The element reference to be added.dest
- The destination node reference forwhich the given element is
added as immediate right sibling
int root = document.parseString("<root><element1/><element2/></root>");
int newelement = document.parseString("<newElement>value</newElement>");
Node.add(newelement, Node.getFirstChild(root));
// After this operation, root element will be as shown below
// <root>
// <element1/>
// <newElement>value</newElement>
// <element2/>
// </root>
public static int append(int from, int to, int dest)
from
- The start element reference to be appended.to
- The end element reference to be appended. If from and to are
same, only that node will be appended. If to is specified as
0, all siblings of 'from' will be appended.dest
- The destination node reference forwhich the range of elements
are appended as immediate right siblings
int root = document.parseString("<root><element1/><element2/></root>");
int newelement = document.parseString("<newElement>value</newElement>");
Node.append(newelement, newelement, Node.getFirstChild(root));
// After this operation, root element will be as shown below
// <root>
// <element1/>
// <element2/>
// <newElement>value</newElement>
// </root>
public static int append(int node, int destination)
node
- The element reference to be appended.destination
- The destination node reference forwhich the given element is
appended as immediate right sibling
int root = document.parseString("<root><element1/><element2/></root>");
int newelement = document.parseString("<newElement>value</newElement>");
Node.append(newelement, Node.getFirstChild(root));
// After this operation, root element will be as shown below
// <root>
// <element1/>
// <element2/>
// <newElement>value</newElement>
// </root>
public static int insertInChildren(int from, int to, int dest)
from
- The start element reference to be inserted.to
- The end element reference to be inserted. If from and to are
same, only that node will be inserted. If to is specified as
0, all siblings of 'from' will be inserted.dest
- The destination node reference forwhich the range of elements
are inserted as child elements
int root = document.parseString("<root><element1/><element2/></root>");
int newelement = document.parseString("<newElement>value</newElement>");
Node.insertInChildren(newelement, newelement, root);
// After this operation, root element will be as shown below
// <root>
// <newElement>value</newElement>
// <element1/>
// <element2/>
// </root>
public static int appendToChildren(int from, int to, int dest)
from
- The start element reference to be appended.to
- The end element reference to be appended. If from and to are
same, only that node will be appended. If to is specified as
0, all siblings of 'from' will be appended.dest
- The destination node reference
int root = document.parseString("<root><element1/><element2/></root>");
int newelement = document.parseString("<newElement>value</newElement>");
Node.appendToChildren(newelement, newelement, root);
// After this operation, root element will be as shown below
// <root>
// <element1/>
// <element2/>
// <newElement>value</newElement>
// </root>
public static int appendToChildren(int node, int destination)
node
- The element reference to be appended.destination
- The destination node reference
int root = document.parseString("<root><element1/><element2/></root>");
int newelement = document.parseString("<newElement>value</newElement>");
Node.appendToChildren(newelement, root);
// After this operation, root element will be as shown below
// <root>
// <element1/>
// <element2/>
// <newElement>value</newElement>
// </root>
public static int clone(int node, boolean deep)
deep==true
then this will
create clone of all the child elements. If deep==false
then
this will create clone of the given element only, not its children
elements.node
- The element reference to be cloned.deep
- The boolean variable indicates whether to clone child elements
of the given node.
int element = document
.load("<element><childElement><node>value</node></childElement></element>"
.getBytes());
cloneElement = Node.clone(element, true);
// returns
// <element>
// <childElement>
// <node>value</node>
// </childElement>
// </element>
cloneElement = Node.clone(element, false);
// returns
// </element>
@Deprecated public static int duplicate(int from, int to)
from
- The element reference of node from which to duplicateto
- The element reference of the node to which to duplicatepublic static int duplicateElement(int element)
element
- The reference of node to duplicate
int element = document.load("<element att=\"value\"><childElement><node>value</node></childElement></element>".getBytes());
duplElement = Node.duplicateElement(element);
// returns
// </element att="value">
public static int duplicate(int node)
node
- int reference of node which is to be duplicated
int element = document.load("<element att=\"value\"><childElement><node>value</node></childElement></element>".getBytes());
duplElement = Node.duplicate(element);
// returns
// <element att="value">
// <childElement>
// <node>value</node>
// </childElement>
// </element>
public static int duplicateAndInsert(int from, int to, int dest)
from
- int reference of the node from which to duplicateto
- If from and to are same only that node will be duplicated and
inserted if to is specified as 0 all siblings will be
duplicated and inserteddest
- the destination node reference
int root = document.parseString("<root><element1/><element2/></root>");
int newelement = document.parseString("<newElement>value</newElement>");
Node.duplicateAndInsert(newelement, newelement, Node.getFirstChild(root));
// After this operation, root element will be as shown below
// <root>
// <newElement>value</newElement>
// <element1/>
// <element2/>
// </root>
public static int duplicateAndAdd(int from, int to, int dest)
from
- int reference of the node which is to be duplicated and
insertedto
- If from and to are same only that node will be duplicated and
inserted if to is specified as 0 all siblings will be
duplicated and inserteddest
- the destination node reference to be inserted
int root = document.parseString("<root><element1/><element2/></root>");
int newelement = document.parseString("<newElement>value</newElement>");
Node.duplicateAndAdd(newelement, newelement, Node.getFirstChild(root));
// After this operation, root element will be as shown below
// <root>
// <element1/>
// <newElement>value</newElement>
// <element2/>
// </root>
public static int duplicateAndAppend(int from, int to, int dest)
from
- int reference of the node which is to be duplicatedto
- If from and to are same only that node will be duplicated and
appended if to is specified as 0 all siblings will be
duplicated and appendeddest
- the destination node reference
int root = document.parseString("<root><element1/><element2/></root>");
int newelement = document.parseString("<newElement>value</newElement>");
Node.duplicateAndAppend(newelement, newelement, Node.getFirstChild(root));
// After this operation, root element will be as shown below
// <root>
// <element1/>
// <element2/>
// <newElement>value</newElement>
// </root>
public static int duplicateAndInsertInChildren(int from, int to, int dest)
from
- the start node reference which is to be duplicatedto
- the end node reference to be duplicated. If from and to are
same only that node will be duplicated and inserted. If it is
specified as 0 all siblings will be duplicated and inserteddest
- int reference of the node in which to insert
int root = document.parseString("<root><element1/><element2/></root>");
int newelement = document.parseString("<newElement>value</newElement>");
Node.duplicateAndInsertInChildren(newelement, newelement, root);
// After this operation, root element will be as shown below
// <root>
// <newElement>value</newElement>
// <element1/>
// <element2/>
// </root>
public static int duplicateAndAppendToChildren(int from, int to, int dest)
from
- int reference of the node which is to be duplicatedto
- If from and to are same, only that node will be duplicated and
appended if to is specified as 0 all siblings will be
duplicated and appendeddest
- int reference of the node in which to insert
int root = document.parseString("<root><element1/><element2/></root>");
int newelement = document.parseString("<newElement>value</newElement>");
Node.duplicateAndAppendToChildren(newelement, newelement, root);
// After this operation, root element will be as shown below
// <root>
// <element1/>
// <element2/>
// <newElement>value</newElement>
// </root>
public static void setName(int node, java.lang.String name)
node
- The element reference for which the tagname needs to be
changed.name
- The new tag name.
int element = document.load("<oldelement/>".getBytes());
Node.setName(element, "newelement");
//"oldelement" gets changed to "newelement"
public static java.lang.String getName(int node)
node
- The element reference for which the tagname is required.
int element = document.load("<element/>".getBytes());
String tagName = Node.getName(element);
// returns 'element'
public static void setData(int node, java.lang.String data)
node
- The data element reference for which the data needs to setdata
- The string data that is to be set.
int element = document.load("<element>data</element>".getBytes());
Node.setData(Node.getFirstChild(element), "data-modified");
// The element node will be <element>data-modified</element>
@Deprecated public static java.lang.String getData(int node, java.lang.String defaultValue)
node
- The text element referencedefaultValue
- The value that is to be returned if the data is not found to
the given text elementpublic static java.lang.String getDataWithDefault(int node, java.lang.String defaultValue)
node
- The text element referencedefaultValue
- The value that is to be returned if the data is not found to
the given text element
int element = document.load("<element>data</element>".getBytes());
String data = Node.getDataWithDefault(element, "default");
//returns "data"
int element = document.load("<element></element>".getBytes());
String data = Node.getDataWithDefault(element, "default");
//returns "default"
public static java.lang.String getData(int node)
node
- The text element reference
int element = document.load("<element>data</element>".getBytes());
String data = Node.getData(Node.getFirstChild(element));
//returns "data"
@Deprecated public static java.lang.String getDataOfFirstDataChild(int node, java.lang.String defaultValue)
node
- The node element reference.defaultValue
- The value that is to be returned if the data is not found to
the first child of given elementpublic static void setAttribute(int node, java.lang.String name, java.lang.String value)
node
- The reference of the nodename
- The name of the attributevalue
- The value for the attribute
int element = document.load("<element/>".getBytes());
Node.setAttribute(element, "attributename", "attributeValue");
// The element node will look like
// <element attributename="attributeValue"/>
public static void setAttributeNS(int node, java.lang.String name, java.lang.String uri, java.lang.String value)
node
- The reference of the nodename
- The name of the attributeuri
- The namespace URI.value
- The value for the attribute
int element = document.load("<element xmlns:ns=\"http://cordys.com/test\"/>".getBytes());
Node.setAttributeNS(element,"attributename","http://cordys.com/test","attributeValue");
// The element node will look like
// <element ns:attributename="attributeValue"/>
public static int setNSDefinition(int node, java.lang.String prefix, java.lang.String uri)
node
- The reference of the node for which the namespace definition
is to be set.prefix
- The namespace prefixuri
- The namespace URIThrows
- an IllegalArgumentException
, if the uri is
null or the prefix is empty String.
int element = document.load("<element xmlns:ns=\"http://cordys.com/test\"/>".getBytes());
Node.setNSDefinition(element,"ns","http://cordys.com/ns");
// Now, the element node will look like
// <element xmlns:ns="http://cordys.com/ns"/>
// If 'ns' is not defined to the element node, then it will create a new namespace definition.
public static int setNSPrefix(int node, java.lang.String prefix)
XMLExceptionIf the namespace prefix is already set to the node, It will modify the namespace prefix with the given prefix value. If the namespace prefix is not set to the node, It will set the namespace prefix with the given prefix value. If the prefix is null, then it is treated as a default namespace.
node
- The reference of the node forwhich the namespace definition is
to be set.prefix
- The namespace prefixThrows
- an IllegalArgumentException
, if the prefix is
empty String.
int element = document.load("<element xmlns:ns=\"http://cordys.com/test\"/>".getBytes());
Node.setNSPrefix(element,"ns");
// Now, the element node will look like
// <ns:element xmlns:ns="http://cordys.com/ns"/>
public static int setNSPrefixRecursive(int node, java.lang.String oldPrefix, java.lang.String newPrefix)
node
- The reference of the node forwhich the namespace definition is
to be set.oldPrefix
- The old namespace prefix of the nodenewPrefix
- The new namespace prefix of the nodeThrows
- an XMLException
, if the given oldPrefix or
newPrefix is not defined to the node or to its ancestor
elementsThrows
- an IllegalArgumentException
, if the oldPrefix
or newPrefix is empty String.
int element = document.load("<ns:element xmlns:nsNew=\"http://cordys.com/nsnew\" xmlns:ns=\"http://cordys.com/test\"/>".getBytes());
Node.setNSPrefixRecursive(element,"ns", "nsNew");
// Now, the element node will look like
// <nsNew:element xmlns:nsNew="http://cordys.com/nsnew" xmlns:ns="http://cordys.com/test"/>
public static java.lang.String getAttribute(int node, java.lang.String name)
node
- The reference of the nodename
- The name of the attribute
int element = document.load("<element att=\"attVal\"/>".getBytes());
String value = Node.getAttribute(element, "att"); // returns 'attVal'
public static java.lang.String getAttributeNS(int node, java.lang.String name, java.lang.String uri)
node
- The reference of the nodename
- The name of the attributeuri
- The namespace URI of the attribute, The special value "*"
matches all namespaces
int element = document.load("<element xmlns:ns=\"http://cordys.com/test\" ns:att=\"attVal\"/>".getBytes());
String value = Node.getAttributeNS(element,"att", "http://cordys.com/test"); // returns 'attVal'
public static int removeAttribute(int node, java.lang.String name)
node
- The reference of the nodename
- The name of the attribute to be removed
int element = document.load("<element att=\"attVal\"/>".getBytes());
int isDeleted = Node.removeAttribute(element, "att");
//Now, the element node will look like
//<element/>
public static int removeAttributeNS(int node, java.lang.String name, java.lang.String URI)
node
- The reference of the nodename
- The name of the attribute to be removedURI
- the namespace URI of the attribute to remove. The special
value "*" matches all namespaces
int element = document
.load("<ns:element ns:att=\"attVal\" ns:att1=\"testing\" xmlns:ns=\"http://test.com\"/>"
.getBytes());
int isDeleted = Node.removeAttributeNS(element, "att", "http://test.com/");
// removing attribute using its local name
int isAtt1Deleted = Node.removeAttributeNS(element, "att1", "*");
public static int removeAttributesRecursive(int node, java.lang.String name1, java.lang.String name2, java.lang.String name3, java.lang.String name4)
node
- The reference of the nodename1
- The name of the attribute to be removedname2
- The name of the attribute to be removedname3
- The name of the attribute to be removedname4
- The name of the attribute to be removed
Document oDocument = new Document();
String xmlStr = "<root><table attr1=\"abc\" attr2=\"def\" attr3=\"ghi\" attr4=\"jkl\">xyz</table><chair/></root>";
try {
int result = oDocument.load(xmlStr.getBytes());
Node.removeAttributesRecursive(result, "attr1", "attr2", "attr3", "attr4");
assertEquals("<root><table>xyz</table><chair/></root>", Node.writeToString(
result, false));
} catch (XMLException e) {
e.printStackTrace();
}
public static int getNumAttributes(int node)
node
- The reference of the node
Document oDocument = new Document();
String xmlStr = "<oldelement/>";
try {
int result = 0;
result = oDocument.load("<element attr1=\"xys\" attr2=\'abc\'></element>"
.getBytes());
int ret = Node.getNumAttributes(result);
assertEquals(2, ret);
} catch (XMLException e) {
e.printStackTrace();
}
public static java.lang.String getAttributeName(int node, int index)
node
- The reference of the nodeindex
- The index of the attribute where the index starts from 1.
int element = document.load("<element att=\"attVal\"/>".getBytes());
String attName = Node.getAttributeName(element, 1); // returns 'att'
public static java.lang.String getAttributeLocalName(int node, int index)
node
- The reference of the nodeindex
- The index of the attribute where the index starts from 1.
int element = document.load("<element xmlns:ns=\"http://cordys.com/test\" ns:att=\"attVal\"/>".getBytes());
String attName = Node.getAttributeLocalName(element,2); // returns 'att'
public static java.lang.String getAttributePrefix(int node, int index)
node
- The reference of the nodeindex
- The index of the attribute where the index starts from 1.
int element = document.load("<element xmlns:ns=\"http://cordys.com/test\" ns:att=\"attVal\"/>".getBytes());
String attName = Node.getAttributePrefix(element,2); // returns 'ns'
public static java.lang.String getAttributeNamespaceURI(int node, int index)
node
- The reference of the nodeindex
- The index of the attribute where the index starts from 1.
int element = document.load("<element xmlns:ns=\"http://cordys.com/test\" ns:att=\"attVal\"/>".getBytes());
String attName = Node.getAttributeNamespaceURI(element,2); // returns 'http://cordys.com/test'
public static int getType(int node)
node
- The node reference
int element = document.load("<element xmlns:ns=\"http://cordys.com/test\" ns:att=\"attVal\"/>".getBytes());
String attName = Node.getType(element); // returns 8 (of type node)
NodeType
public static int getCompoundType(int node)
node
- The node reference
int element = document.load("<element xmlns:ns=\"http://cordys.com/test\" ns:att=\"attVal\"/>".getBytes());
String attName = Node.getCompoundType(element); // returns 0 (of type BASE)
CompoundType
public static int getDataType(int node)
node
- The element reference forwhich the data type is required.
int element = document.load("<element>xyz<child1/><child2/></element>"
.getBytes());
String attName = Node.getCompoundType(element); // returns 1 (of type STRING)
DataType
public static int getParent(int node)
node
- The node reference.
Document oDocument = new Document();
try {
int result = 0;
result = oDocument.load("<element>xyz<child1/><child2/></element>"
.getBytes());
result = Node.getFirstChild(result);
result = Node.getParent(result);
assertEquals("element", Node.getName(result));
} catch (XMLException e) {
e.printStackTrace();
}
public static int getFirstChild(int node)
node
- The node reference.
Document oDocument = new Document();
try {
int result = 0;
result = oDocument.load("<element>xyz<child1/><child2/></element>"
.getBytes());
result = Node.getFirstChild(result);
assertEquals("xyz", Node.getData(result));
} catch (XMLException e) {
e.printStackTrace();
}
public static int getFirstChildElement(int node)
node
- The node reference.
Document oDocument = new Document();
try {
int result = 0;
result = oDocument.load("<element>xyz<child1/><child2/></element>"
.getBytes());
result = Node.getFirstChildElement(result);
assertEquals("child1", Node.getName(result));
} catch (XMLException e) {
e.printStackTrace();
}
public static int getLastChild(int node)
node
- The node reference.
Document oDocument = new Document();
try {
int result = 0;
result = oDocument.load("<element><child1/><child2/></element>".getBytes());
result = Node.getLastChild(result);
assertEquals("child2", Node.getName(result));
} catch (XMLException e) {
e.printStackTrace();
}
public static int getLastChildElement(int node)
node
- The node reference.
Document oDocument = new Document();
try {
int result = 0;
result = oDocument.load("<element>xyz<child1/><child2/>xyz</element>"
.getBytes());
result = Node.getLastChildElement(result);
assertEquals("child2", Node.getName(result));
} catch (XMLException e) {
e.printStackTrace();
}
public static int getPreviousSibling(int node)
node
- The node reference.
Document oDocument = new Document();
try {
int result = 0;
result = oDocument.load("<element>xyz<child1/><child2/>xyz</element>"
.getBytes());
result = Node.getFirstChildElement(result);
result = Node.getPreviousSibling(result);
assertEquals("xyz", Node.getData(result));
} catch (XMLException e) {
e.printStackTrace();
}
public static int getPreviousElement(int node)
node
- The node reference.
ocument oDocument = new Document();
try {
int result = 0;
result = oDocument.load("<element>xyz<child1/><child2/>xyz</element>"
.getBytes());
result = Node.getLastChild(result);
result = Node.getPreviousElement(result);
assertEquals("child2", Node.getName(result));
} catch (XMLException e) {
e.printStackTrace();
}
public static int getNextSibling(int node)
node
- The node reference.
Document oDocument = new Document();
try {
int result = 0;
result = oDocument.load("<element>xyz<child1/><child2/>xyz</element>"
.getBytes());
result = Node.getFirstChildElement(result);
result = Node.getNextSibling(result);
assertEquals("child2", Node.getName(result));
} catch (XMLException e) {
e.printStackTrace();
}
public static int getNextElement(int node)
node
- The node reference.
Document oDocument = new Document();
try {
int result = 0;
result = oDocument.load("<element>xyz<child1/><child2/></element>"
.getBytes());
result = Node.getFirstChild(result);
result = Node.getNextElement(result);
assertEquals("child1", Node.getName(result));
} catch (XMLException e) {
e.printStackTrace();
}
public static int getNumChildren(int node)
node
- The node reference
Document oDocument = new Document();
try {
int result = 0;
result = oDocument.load("<element>xyz<child1/><child2/></element>"
.getBytes());
int num = Node.getNumChildren(result);
assertEquals(3, num);
} catch (XMLException e) {
e.printStackTrace();
}
public static int getNumChildElements(int node)
node
- The node reference
Document oDocument = new Document();
try {
int result = 0;
result = oDocument.load("<element>xyz<child1/><child2/>xyz</element>"
.getBytes());
int num = Node.getNumChildElements(result);
assertEquals(2, num);
} catch (XMLException e) {
e.printStackTrace();
}
public static int getNumSiblings(int node)
node
- The node reference
Document oDocument = new Document();
try {
int result = 0;
result = oDocument.load("<element>xyz<child1/><child2/>xyz</element>"
.getBytes());
int num = Node.getNumSiblings(Node.getFirstChild(result));
assertEquals(3, num);
} catch (XMLException e) {
e.printStackTrace();
}
public static int getNumSiblingElements(int node)
node
- The node reference
Document oDocument = new Document();
try {
int result = 0;
result = oDocument.load("<element>xyz<child1/><child2/></element>"
.getBytes());
int num = Node.getNumSiblingElements(Node.getFirstChild(result));
assertEquals(2, num);
} catch (XMLException e) {
e.printStackTrace();
}
public static int getNumPreviousSiblings(int node)
node
- The node reference
Document oDocument = new Document();
try {
int result = 0;
result = oDocument.load("<element>xyz<child1/><child2/></element>"
.getBytes());
int num = Node.getNumPreviousSiblings(Node.getLastChild(result));
assertEquals(3, num);
} catch (XMLException e) {
e.printStackTrace();
}
public static int getNumPreviousSiblingElements(int node)
node
- The node reference
Document oDocument = new Document();
try {
int result = 0;
result = oDocument.load("<element>xyz<child1/><child2/></element>"
.getBytes());
int num = Node.getNumPreviousSiblingElements(Node.getLastChild(result));
assertEquals(2, num);
} catch (XMLException e) {
e.printStackTrace();
}
public static int getNumNextSiblings(int node)
node
- The node reference
Document oDocument = new Document();
try {
int result = 0;
result = oDocument.load("<element>xyz<child1/><child2/></element>"
.getBytes());
int num = Node.getNumNextSiblings(Node.getFirstChild(result));
assertEquals(3, num);
} catch (XMLException e) {
e.printStackTrace();
}
public static int getNumNextSiblingElements(int node)
node
- The node reference
Document oDocument = new Document();
try {
int result = 0;
result = oDocument.load("<element>xyz<child1/><child2/></element>"
.getBytes());
int num = Node.getNumNextSiblingElements(Node.getFirstChild(result));
assertEquals(2, num);
} catch (XMLException e) {
e.printStackTrace();
}
public static byte[] write(int from, int to, int pretty)
from
- The start element reference from which the write operation
begins.to
- The end element reference where the write operation ends.pretty
- If this value is 1, it is written with indentation. If this
value is 0, it is written without indentation.
Document oDocument = new Document();
try {
int result = 0;
result = oDocument.load("<element></element>".getBytes());
byte[] obyteArr = Node.write(result, result, 0);
assertEquals("<element/>", new String(obyteArr));
} catch (XMLException e) {
e.printStackTrace();
}
public static byte[] writeWithNSInfo(int from, int to, int pretty)
from
- The start element reference from which the write operation
begins.to
- The end element reference where the write operation ends.pretty
- If this value is 1, it is written with indentation. If this
value is 0, it is written without indentation.
Document oDocument = new Document();
try {
int result = 0;
result = oDocument.load("<element xmlns:ns=\"http://cordys.com/test\">xyz<ns:child1/><child2/></element>"
.getBytes());
result = Node.getFirstChild(result);
byte[] obyteArr = Node.writeWithNSInfo(result, result, 0);
assertEquals("<ns:child1 xmlns:ns=\"http://cordys.com/test\"/>", new String(obyteArr));
} catch (XMLException e) {
e.printStackTrace();
}
public static byte[] write(int node, boolean pretty)
node
- The node that is to be writtenpretty
- If this value is true, it is written with indentation. If this
value is false, it is written without indentation.
Document oDocument = new Document();
try {
int result = 0;
result = oDocument.load("<element></element>".getBytes());
byte[] obyteArr = Node.write(result, false);
assertEquals("<element/>", new String(obyteArr));
} catch (XMLException e) {
e.printStackTrace();
}
public static byte[] deflate(int node)
node
- NOM node handle whose data is to be compressed. If the NOM
node is of type Node.ELEMENT, then the data to be compressed
will be taken from the content of element, and does not
include element name or attributes. If the NOM node is of type
Node.DATA, then the data to be compressed will be taken from
the input data node till last right sibling (including element
& data nodes).com.cordys.util.ZlibInflater}
Example:
public void testDeflate() {
try {
int root = doc.load("xml input".getBytes());
byte[] arr = Node.deflate(root);
IInflater inflater = new ZlibInflater();
byte[] ret = inflater.inflate(arr);
String deCompressed = new String(ret);
assertEquals(Node.getData(root), deCompressed);
} catch (XMLException e) {
e.printStackTrace();
}
}
public static java.lang.String writeToString(int node, boolean pretty)
node
- The node that is to be writtenpretty
- If this value is true, it is written with indentation. If this
value is false, it is written without indentation.
Document oDocument = new Document();
try {
int result = 0;
result = oDocument.load("<element></element>".getBytes());
String str = Node.writeToString(result, false);
assertEquals("<element/>", str);
} catch (XMLException e) {
e.printStackTrace();
}
public static java.lang.String writeToStringWithNSInfo(int node, boolean pretty)
node
- The node that is to be writtenpretty
- If this value is true, it is written with indentation. If this
value is false, it is written without indentation.
Document oDocument = new Document();
try {
int result = 0;
result = oDocument.load("<element xmlns:ns=\"http://cordys.com/test\">xyz<ns:child1/><child2/></element>"
.getBytes());
result = Node.getFirstChild(result);
String str = Node.writeToStringWithNSInfo(result, false);
assertEquals("<ns:child1 xmlns:ns=\"http://cordys.com/test\"/>", str);
} catch (XMLException e) {
e.printStackTrace();
}
public static final void writeToFile(int from, int to, java.lang.String file, int pretty) throws XMLException
from
- The start element reference.to
- The end element reference.file
- The name of the file to write to (If path for the file is not
specified then the file is written to the directory from where
the java application is invoked i.e. if used in soap processor
which is started as System process then the file is written to
OS System directory i.e. C:\Windows\system32 in case of
WindowsXP, etc.)pretty
- Indicates whether to write with indentation or normally and
whether the xml header needs to be included or not. It can be
created by Oring WRITE_PRETTY and WRITE_HEADER to indicate
that both indentation and inclusion of xml header should take
placeXMLException
- if the nodes could not be written to the file
Document oDocument = new Document();
try {
int result = 0;
String fileName = ".\\newFile.xml";
result = oDocument.load("<element></element>".getBytes());
Node.writeToFile(result, result, fileName, Node.WRITE_PRETTY
+ Node.WRITE_HEADER);
} catch (XMLException e) {
e.printStackTrace();
}
public static int getFirstElement(int node)
node
- The node reference to retrieve the first child element
Document oDocument = new Document();
try {
int result = 0;
result = oDocument.load("<element>xyz<child1/><child2/></element>"
.getBytes());
result = Node.getFirstElement(result);
assertEquals("child1", Node.getName(result));
} catch (XMLException e) {
e.printStackTrace();
}
public static int getFirstDataNode(int node)
node
- The node to retrieve the first data child from
Document oDocument = new Document();
try {
int result = 0;
result = oDocument.load("<element>xyz<child1/><child2/></element>"
.getBytes());
result = Node.getFirstDataNode(result);
assertEquals("xyz", Node.getData(result));
} catch (XMLException e) {
e.printStackTrace();
}
public static java.lang.String getAttribute(int node, java.lang.String name, java.lang.String defaultValue)
node
- The node reference from which the attribute value is to be
retrievedname
- The name of the attributedefaultValue
- The default value that is to be returned if the attribute is
not found.
Document oDocument = new Document();
try {
int result = 0;
result = oDocument.load("<element attr=\"abc\"></element>".getBytes());
String str = Node.getAttribute(result, "attr", "default");
assertEquals("abc", str);
str = Node.getAttribute(result, "att", "default");
assertEquals("default", str);
} catch (XMLException e) {
e.printStackTrace();
}
public static java.lang.String getAttributeNS(int node, java.lang.String name, java.lang.String uri, java.lang.String defaultValue)
node
- The node reference from which the attribute value is to be
retrievedname
- The name of the attributeuri
- The namespace URI of the attributedefaultValue
- The default value that is to be returned if the attribute is
not found.
Document oDocument = new Document();
try {
int result = 0;
result = oDocument
.load("<element xmlns:ns=\"http://cordys.com/test\" ns:attr=\"xyz\"></element>"
.getBytes());
String str = Node.getAttributeNS(result, "ns", null, "Default");
assertEquals("Default", str);
str = Node.getAttributeNS(result, "attr", "http://cordys.com/test",
"Default");
assertEquals("xyz", str);
} catch (XMLException e) {
e.printStackTrace();
}
public static int getAttributeInt(int node, java.lang.String name, int defaultValue)
node
- The node reference from which the attribute value is to be
retrievedname
- The name of the attributedefaultValue
- The default value that is to be returned if the attribute is
not found or is not integer.
Document oDocument = new Document();
try {
int result = 0;
result = oDocument.load("<element attr=\"50\"></element>".getBytes());
int ret = Node.getAttributeInt(result, "attr2", -99);
assertEquals(-99, ret);
ret = Node.getAttributeInt(result, "attr", -99);
assertEquals(50, ret);
} catch (XMLException e) {
e.printStackTrace();
}
public static int getElement(int node, java.lang.String tagName)
node
- The node referencetagName
- The tag name of the child element
Document oDocument = new Document();
try {
int result = 0;
result = oDocument.load("<element><child1/></element>".getBytes());
int ret = Node.getElement(result, "child1");
assertEquals("child1", Node.getName(ret));
} catch (XMLException e) {
e.printStackTrace();
}
public static int getElementNS(int node, java.lang.String tagName, java.lang.String uri)
node
- The element referencetagName
- The tag name of the elementuri
- The namespace URI bywhich the element bound
Document oDocument = new Document();
try {
int result = 0;
result = oDocument
.load("<ns:element xmlns:ns=\"http://cordys.com/test\"><ns:child1/></ns:element>"
.getBytes());
int ret = Node.getElementNS(result, "child1", "http://cordys.com/test");
assertEquals("ns:child1", Node.getName(ret));
} catch (XMLException e) {
e.printStackTrace();
}
public static boolean nameEquals(int node, java.lang.String name)
node
- the node reference.name
- the name of the node that is to be compared.
Document oDocument = new Document();
try {
int result = 0;
result = oDocument.load("<element><child1/></element>".getBytes());
assertEquals(true, Node.nameEquals(result, "element"));
} catch (XMLException e) {
e.printStackTrace();
}
public static Document getDocument(int node)
node
- The node to retrieve the document from
Document oDocument = new Document();
Document oDocument2;
try {
int result = 0;
result = oDocument.load("<element><child1/></element>".getBytes());
oDocument2 = Node.getDocument(result);
assertEquals(true, oDocument2 == oDocument);
assertEquals(true, oDocument2.equals(oDocument));
} catch (XMLException e) {
e.printStackTrace();
}
@Deprecated public static int createElementNS(java.lang.String name, java.lang.String content, int parent)
String xml =
"<ns:root xmlns:ns="http://cordys.com/test/">
<ns:employee> Cordys employee </ns:employee>
<employee1>
<test> Cordys employee </test>
</employee1>
</ns:root>";
Document doc = new com.eibus.xml.nom.Document();
int root = doc.load(xml.getBytes());
int emp = Node.getElement(root, "employee1");
// Creates an element which is child to root element as "<ns:employee2>emplyee2 data</ns:employee2>"
Node.createElementNS("employee2","employee2 data", root); // parent element has namespace 'ns'
// Creates an element which is child to employee1 element as "<employee3>emplyee3 data</employee3>"
Node.createElementNS("employee3","employee3 data", emp); // parent element has no namespace
name
- local name of new elementcontent
- content/data of the new elementparent
- handle of parent element. If 0 passed, then Exception will be
thrownpublic static int createElementWithParentNS(java.lang.String name, java.lang.String content, int parent)
name
- local name of new elementcontent
- content/data of the new elementparent
- handle of parent element. If 0 passed, then Exception will be
thrown
String xml =
"<ns:root xmlns:ns="http://cordys.com/test/">
<ns:employee> Cordys employee </ns:employee>
<employee1>
<test> Cordys employee </test>
</employee1>
</ns:root>";
Document doc = new com.eibus.xml.nom.Document();
int root = doc.load(xml.getBytes());
int emp = Node.getElement(root, "employee1");
// Creates an element which is child to root element as "<ns:employee2>emplyee2 data</ns:employee2>"
Node.createElementWithParentNS("employee2","employee2 data", root); // parent element has namespace 'ns'
// Creates an element which is child to employee1 element as "<employee3>emplyee3 data</employee3>"
Node.createElementWithParentNS("employee3","employee3 data", emp); // parent element has no namespace
@Deprecated public static int createElementNS(java.lang.String name, java.lang.String content, java.lang.String prefix, int parent)
name
- local name of new elementprefix
- prefix for new elementcontent
- content/data of the new elementparent
- handle of parent element. If 0 passed, then Exception will be
thrownpublic static int createElementNSWithPrefix(java.lang.String name, java.lang.String content, java.lang.String prefix, int parent)
name
- local name of new elementprefix
- prefix for new elementcontent
- content/data of the new elementparent
- handle of parent element. If 0 passed, then Exception will be
thrown
String xml =
"<ns:root xmlns:ns="http://cordys.com/test/">
<ns:employee> Cordys employee </ns:employee>
<employee1>
<test> Cordys employee </test>
</employee1>
</ns:root>";
Document doc = new com.eibus.xml.nom.Document();
int root = doc.load(xml.getBytes());
int emp = Node.getElement(root, "employee1");
// Creates an element which is child to root element as "<ns:employee2>emplyee2 data</ns:employee2>"
Node.createElementNSWithPrefix("employee2","employee2 data", "ns", root);
// Creates an element which is child to employee1 element as "<ns:employee3>emplyee3 data</ns:employee3>"
Node.createElementNSWithPrefix("employee3","employee3 data", "ns", emp);
public static int createElementNS(java.lang.String name, java.lang.String content, java.lang.String prefix, java.lang.String namespaceURI, int parent)
name
- The tagname for the new elementcontent
- content/data of the new elementprefix
- 'prefix' for the new element, if the prefix is null then the
created element will have a default namespace with the given
namespaceURI.namespaceURI
- namespace URI of the 'prefix'parent
- handle of parent element. If 0 passed, then Exception will be
thrown
String xml =
"<ns:root xmlns:ns="http://cordys.com/test/">
<ns:employee> Cordys employee </ns:employee>
<employee1>
<test> Cordys employee </test>
</employee1>
</ns:root>";
Document doc = new com.eibus.xml.nom.Document();
int root = doc.load(xml.getBytes());
// Creates an element which is child to root element as "<prefix:employee2 xmlns:prefix="http://www.cordys.com/test/">emplyee2 data</prefix:employee2>"
Node.createElementNS("employee2","employee2 data", "prefix", "http://www.cordys.com/test/", root);
@Deprecated public static int createCDataElementNS(java.lang.String name, java.lang.String CDataContent, int parent)
name
- local name of new elementCDataContent
- CDataContent/CData of the new elementparent
- handle of parent element. If 0 passed, then Exception will be
thrownpublic static int createCDataElementWithParentNS(java.lang.String name, java.lang.String CDataContent, int parent)
name
- local name of new elementCDataContent
- CDataContent/CData of the new elementparent
- handle of parent element. If 0 passed, then Exception will be
thrown
String xml =
"<ns:root xmlns:ns="http://cordys.com/test/">
<ns:employee> Cordys employee </ns:employee>
<employee1>
<test> Cordys employee </test>
</employee1>
</ns:root>";
Document doc = new com.eibus.xml.nom.Document();
int root = doc.load(xml.getBytes());
int emp = Node.getElement(root, "employee1");
// Creates an element which is child to root element as "<ns:employee2><![CDATA[employee2 data]]></ns:employee2>"
Node.createCDataElementWithParentNS("employee2","employee2 data", root); // parent element has namespace 'ns'
// Creates an element which is child to employee1 element as "<employee3><![CDATA[employee3 data]]></employee3>"
Node.createCDataElementWithParentNS("employee3","employee3 data", emp); // parent element has no namespace
@Deprecated public static int createCDataElementNS(java.lang.String name, java.lang.String CDataContent, java.lang.String prefix, int parent)
name
- local name of new elementprefix
- prefix for new elementCDataContent
- CDataContent of the new elementparent
- handle of parent element. If 0 passed, then Exception will be
thrownpublic static int createCDataElementNSWithPrefix(java.lang.String name, java.lang.String CDataContent, java.lang.String prefix, int parent)
name
- local name of new elementprefix
- prefix for new elementCDataContent
- CDataContent of the new elementparent
- handle of parent element. If 0 passed, then Exception will be
thrown
String xml =
"<ns:root xmlns:ns="http://cordys.com/test/">
<ns:employee> Cordys employee </ns:employee>
<employee1>
<test> Cordys employee </test>
</employee1>
</ns:root>";
Document doc = new com.eibus.xml.nom.Document();
int root = doc.load(xml.getBytes());
int emp = Node.getElement(root, "employee1");
// Creates an element which is child to root element as "<ns:employee2><![CDATA[employee2 data]]></ns:employee2>"
Node.createCDataElementNSWithPrefix("employee2","employee2 data", "ns", root); // parent element has namespace 'ns'
// Creates an element which is child to employee1 element as "<ns:employee3><![CDATA[employee3 data]]></ns:employee3>"
Node.createCDataElementNSWithPrefix("employee3","employee3 data", "ns", emp); // parent element has no namespace
public static int createCDataElementNS(java.lang.String name, java.lang.String CDataContent, java.lang.String prefix, java.lang.String namespaceURI, int parent)
name
- The tagname for the new elementCDataContent
- CDataContent of the new elementprefix
- 'prefix' for the new element, if the prefix is null then the
created element will have a default namespace with the given
namespaceURI.namespaceURI
- namespace URI of the 'prefix'parent
- handle of parent element. If 0 passed, then Exception will be
thrown.
String xml =
"<ns:root xmlns:ns="http://cordys.com/test/">
<ns:employee> Cordys employee </ns:employee>
<employee1>
<test> Cordys employee </test>
</employee1>
</ns:root>";
Document doc = new com.eibus.xml.nom.Document();
int root = doc.load(xml.getBytes());
// Creates an element which is child to root element as "<prefix:employee2 xmlns:prefix="http://www.cordys.com/test/"><![CDATA[employee2 data]]></prefix:employee2>"
Node.createCDataElementNS("employee2","employee2 data", "prefix", "http://www.cordys.com/test/", root);
public static byte[] getDataBytesWithDefault(int node, byte[] defaultValue)
node
- Reference to Node
from where data has to be retriveddefaultValue
- Value to be returned if the given element tag is not available
or the data to the element is not availableint root = doc.parseString("<root>data</root>");
byte[] node = Node.getDataBytesWithDefault(root, null);
assertEquals("data", new String(node));
public static byte[] getDataBytesForElement(int node, java.lang.String tag, byte[] defaultValue)
node
- Reference to Node
tag
- Name of the tag, whose data has to be retriveddefaultValue
- Value to be returned if the given element tag is not available
or the data to the element is not availableint root = doc.parseString("<root><child>data</child></root>");
byte[] node = Node.getDataBytesForElement(root,"child",null);
assertEquals("data"), new String(node);
public static byte[] getDataBytesForElementWithNS(int node, java.lang.String tag, java.lang.String uri, byte[] defaultValue)
node
- Reference to Node
tag
- Name of the tag, whose data has to be retriveduri
- The namespace URI of the elementdefaultValue
- That is to be returned if the given element tag is not
available or the data to the element is not available.int root = doc.parseString("<root><child>data1</child><child xmlns=\"https://www.cordys.com\">data</child></root>");
byte[] node = Node.getDataBytesForElementWithNS(root,"child","https://www.cordys.com",null);
assertEquals("data", new String(node));
public static void setDataBytes(int node, byte[] data)
node
- reference to Node
data
- bytes in UTF-8 that has to be set as data in the referenced node
String xml = "<root>SomeData</root>";
int root = doc.parseString(xml);
Node.setDataBytes(Node.getFirstChild(root), "test1".getBytes("UTF-8"));
String data = Node.getData(root);
assertEquals("test1", data);
public static void setDataBytesForElement(int node, java.lang.String tag, byte[] data)
node
- reference to Node
tag
- Name of the node whose value has to be setdata
- bytes in UTF-8 that has to be added
String xml = "<root><test>testing data</test><test1>testing data</test1><test>testing data</test><test1>testing data</test1><test>testing data</test><test1>testing data</test1></root>";
int root = 0;
root = doc.parseString(xml);
Node.setDataBytesForElement(root, "test1", "test1".getBytes("UTF-8"));
int node = Node.getNextSibling(Node.getFirstChild(root));
String data = Node.getData(node);
assertEquals("test1", data);
Node.delete(root);
public static void setDataBytesForElementWithNS(int node, java.lang.String tag, java.lang.String uri, byte[] data)
node
- reference to Node
tag
- Name of the node whose value has to be seturi
- Namespace URIdata
- bytes in UTF-8 that has to be added
String xml = "<root xmlns='http://test.com'><test>testing data</test><test1>testing data</test1><test>testing data</test><test1>testing data</test1><test>testing data</test><test1>testing data</test1></root>";
int root = 0;
root = doc.parseString(xml);
Node.setDataBytesForElementWithNS(root,"test1","http://test.com","new data".getBytes());
int node = Node.getNextSibling(Node.getFirstChild(root));
String data = Node.getData(node);
assertEquals("new data", data);
public static void setCDataBytesForElement(int node, java.lang.String tag, byte[] data)
node
- XML element from which the operation startstag
- element to be operated, if empty, then 'node' will be operateddata
- bytes in UTF-8 that has to be added
String cdata = "<tag><tag1>i>10</tag1><tag2>i>10 && j< 50</tag2><tag3>\"this is a cdata text\"</tag3><tag4>'this is in single quotes'</tag4></root>";
Node.createCDataElement("cdatanode", cdata, root);
Node.setCDataBytesForElement(root, "cdatanode", cdata.getBytes());
String data = Node.getDataElement(root, "cdatanode", "");
assertEquals(cdata, data);