Example usage for org.w3c.dom Node getNodeValue

List of usage examples for org.w3c.dom Node getNodeValue

Introduction

In this page you can find the example usage for org.w3c.dom Node getNodeValue.

Prototype

public String getNodeValue() throws DOMException;

Source Link

Document

The value of this node, depending on its type; see the table above.

Usage

From source file:Main.java

public static Element overrideXml(Element target, Element parent) {

    if (parent != null) {

        NamedNodeMap namedNodeMap = parent.getAttributes();
        for (int i = 0; i < namedNodeMap.getLength(); i++) {

            Node attributeNode = namedNodeMap.item(i);
            String parentAttributeName = attributeNode.getNodeName();
            String parentAttributeValue = attributeNode.getNodeValue();

            // attribute override
            if (!target.hasAttribute(parentAttributeName)) {
                target.setAttribute(parentAttributeName, parentAttributeValue);
            }/*  w  ww .  jav a2  s . c o m*/

            // children override
            if (parent.getChildNodes().getLength() > 0) {
                if (target.getChildNodes().getLength() == 0) {
                    for (int j = 0; j < target.getChildNodes().getLength(); j++) {

                        target.appendChild(target.getChildNodes().item(j));
                    }
                }
            }

        }
    }

    return target;
}

From source file:com.mingo.parser.xml.dom.DomUtil.java

/**
 * Transform node attributes to map.//from  ww w . j a  va 2  s  . co  m
 *
 * @param node {@link Node}
 * @return map : key - attribute name; value - attribute value
 */
public static Map<String, String> getAttributes(Node node) {
    Map<String, String> attributes = ImmutableMap.of();
    if (node.hasAttributes()) {
        ImmutableMap.Builder builder = ImmutableMap.builder();
        // get attributes names and values
        NamedNodeMap nodeMap = node.getAttributes();
        for (int i = 0; i < nodeMap.getLength(); i++) {
            Node currentNode = nodeMap.item(i);
            builder.put(currentNode.getNodeName(), currentNode.getNodeValue());
        }
        attributes = builder.build();
    }
    return attributes;
}

From source file:lineage2.gameserver.utils.XMLUtil.java

/**
 * Method getAttributeBooleanValue./*  w w w . ja  v  a 2  s  .  c om*/
 * @param n Node
 * @param item String
 * @param dflt boolean
 * @return boolean
 */
public static boolean getAttributeBooleanValue(Node n, String item, boolean dflt) {
    final Node d = n.getAttributes().getNamedItem(item);
    if (d == null) {
        return dflt;
    }
    final String val = d.getNodeValue();
    if (val == null) {
        return dflt;
    }
    return Boolean.parseBoolean(val);
}

From source file:lineage2.gameserver.utils.XMLUtil.java

/**
 * Method getAttributeIntValue.//from   w w  w .  j  a  v a2s.com
 * @param n Node
 * @param item String
 * @param dflt int
 * @return int
 */
public static int getAttributeIntValue(Node n, String item, int dflt) {
    final Node d = n.getAttributes().getNamedItem(item);
    if (d == null) {
        return dflt;
    }
    final String val = d.getNodeValue();
    if (val == null) {
        return dflt;
    }
    return Integer.parseInt(val);
}

From source file:lineage2.gameserver.utils.XMLUtil.java

/**
 * Method getAttributeLongValue./* w w w.ja  v  a  2 s.  c om*/
 * @param n Node
 * @param item String
 * @param dflt long
 * @return long
 */
public static long getAttributeLongValue(Node n, String item, long dflt) {
    final Node d = n.getAttributes().getNamedItem(item);
    if (d == null) {
        return dflt;
    }
    final String val = d.getNodeValue();
    if (val == null) {
        return dflt;
    }
    return Long.parseLong(val);
}

From source file:Main.java

/**
 * Dumps a debug listing of the child nodes of the given node to
 * System.out.//w  w  w .ja  v a 2  s  .  c  o  m
 * 
 * @param node the node to dump the children of
 */
public static void dumpChildren(Node node) {
    System.out.println("Children of " + node.getNodeName() + ", NS: " + node.getNamespaceURI() + ", Type: "
            + node.getClass());
    Node child = node.getFirstChild();
    while (child != null) {
        short nodeType = child.getNodeType();
        String nodeName = child.getNodeName();
        String nodeValue = child.getNodeValue();
        String nsURI = child.getNamespaceURI();
        System.out.println("  Type: " + nodeType + ", Name: " + nodeName + ", Value: " + nodeValue + ", NS: "
                + nsURI + ", Type: " + node.getClass());
        child = child.getNextSibling();
    }
}

From source file:Main.java

public static void walkNodes(Node nodeIn, StringBuffer sb, String sPad) {
    if (nodeIn == null)
        return;/* w w w.  j av  a 2s  .  c om*/
    NamedNodeMap map = nodeIn.getAttributes();
    if (map != null)
        for (int i = 0; i < map.getLength(); i++) {
            Node n = map.item(i);
            if (n.getNodeType() == Node.ATTRIBUTE_NODE) {
                sb.append(sPad + "   Attribute:" + n.getNodeName() + " = " + n.getNodeValue() + '\n');
            }
        }
    NodeList nodes = nodeIn.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node n = nodes.item(i);
        if (n.getNodeType() == Node.ELEMENT_NODE) {
            sb.append(sPad + "Element: " + n.getNodeName() + '\n');
        }
        if (n.getNodeType() == Node.TEXT_NODE) {
            sb.append(sPad + "   Value: = " + n.getNodeValue() + '\n');
        }
        if (n.getNodeType() == Node.ELEMENT_NODE) {
            walkNodes(n, sb, sPad + "   ");
        }
    }
}

From source file:Main.java

public static String logElement(final Element elem, final int level) {

    String estr = "";
    String indentText = "  ";
    String addIndT = "";

    // add indent
    int ind = 0;/*from w  ww . ja  va 2s .  co  m*/
    while (ind < level) {
        addIndT = addIndT + indentText;
        ind++;

    }
    String name = elem.getNodeName();
    estr = "\n" + addIndT + "<" + name + " ";
    // Attribs
    NamedNodeMap namedNodeMap = elem.getAttributes();
    StringBuilder sb = new StringBuilder(estr);
    for (int i = 0; i < namedNodeMap.getLength(); i++) {
        Attr att = (Attr) namedNodeMap.item(i);
        sb.append(" " + estr + att.getName() + "=\"" + att.getNodeValue() + "\" ");
    }
    sb.append(">");
    estr = sb.toString();
    NodeList pl = elem.getChildNodes();
    int index = pl.getLength();
    // text nodes
    if (index > 0) {
        int i = 0;
        while (i < index) {
            Node domNode = pl.item(i);
            if ((domNode.getNodeType()) == org.w3c.dom.Node.TEXT_NODE) {
                String Etext = domNode.getNodeValue();
                estr = estr + "\n " + addIndT + addIndT + Etext;
            }
            i++;
        }
    }
    // Child Elements
    if (index > 0) {
        int i = 0;
        while (i < index) {
            Node domNode = pl.item(i);
            if ((domNode.getNodeType()) == org.w3c.dom.Node.ELEMENT_NODE) {
                Element el = (Element) domNode;
                estr = estr + logElement(el, level + 1);
            }
            i++;
        }
    }
    estr = estr + "\n" + addIndT + "</" + name + ">";

    return estr;
}

From source file:Main.java

private static void printNote(NodeList nodeList) {

    for (int count = 0; count < nodeList.getLength(); count++) {

        Node tempNode = nodeList.item(count);

        // make sure it's element node.
        if (tempNode.getNodeType() == Node.ELEMENT_NODE) {

            // get node name and value
            System.out.println("\nNode Name =" + tempNode.getNodeName() + " [OPEN]");
            System.out.println("Node Value =" + tempNode.getTextContent());

            if (tempNode.hasAttributes()) {

                // get attributes names and values
                NamedNodeMap nodeMap = tempNode.getAttributes();

                for (int i = 0; i < nodeMap.getLength(); i++) {

                    Node node = nodeMap.item(i);
                    System.out.println("attr name : " + node.getNodeName());
                    System.out.println("attr value : " + node.getNodeValue());

                }//from w w  w  .j  a  v a 2s . c  o  m

            }

            if (tempNode.hasChildNodes()) {

                // loop again if has child nodes
                printNote(tempNode.getChildNodes());

            }

            System.out.println("Node Name =" + tempNode.getNodeName() + " [CLOSE]");

        }

    }

}

From source file:Main.java

public static void printNode(Node node, String indent) {
    String nodeName = node.getNodeName();
    System.out.print(indent);/*from   w  w  w  . j a v  a2  s.co  m*/

    if (nodeName.equals("#text")) {
        String nodeText = node.getNodeValue();

        if ((nodeText != null) && (nodeText.trim().length() > 0)) {
            System.out.print(nodeText.trim());
        }
    } else {
        System.out.print(nodeName);
    }

    System.out.print(" ");

    if (!nodeName.equals("#text")) {
        NamedNodeMap attrs = node.getAttributes();

        if (attrs != null) {
            for (int i = 0; i < attrs.getLength(); i++) {
                Node attr = attrs.item(i);
                System.out.print(attr.getNodeName() + "=\"" + attr.getNodeValue() + "\" ");
            }
        }
    }

    NodeList children = node.getChildNodes();

    for (int i = 0; i < children.getLength(); i++) {
        System.out.println();
        printNode(children.item(i), indent + "\t");
    }
}