Java Utililty Methods XML Node Value

List of utility methods to do XML Node Value

Description

The list of methods to do XML Node Value are organized into topic(s).

Method

StringgetValueByTagName(Node n, String tag)
get Value By Tag Name
if (n == null)
    return null;
if (tag.equals(n.getLocalName()))
    return n.getFirstChild().getNodeValue();
if (n.hasChildNodes())
    return getValueByTagName(n.getFirstChild(), tag);
else if (n.getNextSibling() != null)
    return getValueByTagName(n.getNextSibling(), tag);
...
StringgetValueOfValueNode(Node n)
Method to get the value of "Value" node
NodeList textNodes = n.getChildNodes();
Node textNode;
StringBuffer value = new StringBuffer("");
for (int j = 0; j < textNodes.getLength(); j++) {
    textNode = textNodes.item(j);
    value.append(textNode.getNodeValue());
return (value.toString().trim());
...
NodegetValues(Node metric)
get Values
return metric.getFirstChild().getNextSibling();
DoublenodeToDouble(Node node)
Converts a node to a double
Double returnValue = null;
try {
    String nodeValue = node.getFirstChild().getNodeValue();
    nodeValue = nodeValue.trim();
    returnValue = nf.parse(nodeValue).doubleValue();
} catch (ParseException e) {
    e.printStackTrace();
return returnValue;
voidputAll(final NamedNodeMap dst, final NamedNodeMap src)
Puts all Nodes of the source NamedNodeMap into the destination NamedNodeMap
final int size = size(src);
for (int i = 0; i < size; i++) {
    dst.setNamedItemNS(src.item(i));
TextsetText(Element parent, String text)
removes all element/text children, then adds a new single text child
removeChildNodesExceptAttributes(parent);
Document doc = parent.getOwnerDocument();
Text child = doc.createTextNode(text);
parent.appendChild(child);
return child;
voidsetText(Element parentNode, String data)
Sets data to be the TEXT content of element
if (data == null)
    return;
Text txt = getTextNode(parentNode);
if (txt != null)
    txt.setData(data);
else {
    txt = parentNode.getOwnerDocument().createTextNode(data);
    parentNode.appendChild(txt);
...
voidsetText(Node n, String text)
Sets the text for a node
if (n == null)
    throw new IllegalArgumentException("Node argument cannot be null");
if (text == null)
    throw new IllegalArgumentException("Node text argument cannot be null");
NodeList nl = n.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
    if (nl.item(i).getNodeType() == Node.TEXT_NODE) {
        nl.item(i).setNodeValue(text);
...
TextsetText(Node node, String text)
Creates a Text child, or replaces all existing children of type Text.
NodeList children = node.getChildNodes();
if (children != null) {
    for (int i = 0, n = children.getLength(); i < n; ++i) {
        Node child = children.item(i);
        if (child instanceof Text) {
            return ((Text) child).replaceWholeText(text);
Text ans = node.getOwnerDocument().createTextNode(text);
node.appendChild(ans);
return ans;
voidsetText(Node node, String text)
set Text
Text txt = node.getOwnerDocument().createTextNode(text);
node.appendChild(txt);
node.normalize();