Example usage for org.w3c.dom Node ELEMENT_NODE

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

Introduction

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

Prototype

short ELEMENT_NODE

To view the source code for org.w3c.dom Node ELEMENT_NODE.

Click Source Link

Document

The node is an Element.

Usage

From source file:Main.java

public static ArrayList<String> getNodeListAttValAsStringCols(String Xpath, Node node, String[] attrNames,
        String sep) throws Exception {
    ArrayList<String> retV = new ArrayList<String>();

    if (sep == null) {
        sep = " ";
    }// w  w  w .j  a v  a2  s . c  o m
    int aNamesL = attrNames.length;
    if (aNamesL > 0) {

        NodeList nl = getNodesListXpathNode(Xpath, node);
        int l = nl.getLength();
        Element e = null;
        String val = "";

        for (int i = 0; i < l; i++) {
            e = (Element) nl.item(i);
            if (e.getNodeType() == Node.ELEMENT_NODE) {
                StringBuilder sb = new StringBuilder();
                for (int y = 0; y < aNamesL; y++) {
                    sb.append(e.getAttribute(attrNames[y]));
                    if (y < aNamesL - 1) {
                        sb.append(sep);
                    }
                }
                val = sb.toString();
                if (val != null && val.length() > 0) {
                    //log.info("getNodeListAttValAsStringCol val = "+val +" attrNames = "+attrNames);
                    /*try {
                       log.info(convertToStringLeaveCDATA(e));
                    }catch(Exception E) {
                       E.printStackTrace();
                    }*/
                    retV.add(val);
                }
            }
        }
    }
    return retV;
}

From source file:Main.java

public static boolean isSpacePreserved(Node node) {
    if (node == null) {
        return false;
    }//from  ww  w . j a v a  2s.c  o m
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        final String spaceAttr = ((Element) node).getAttributeNS(XMLConstants.XML_NS_URI, "space");
        if (spaceAttr != null) {
            return ("preserve".equals(spaceAttr));
        }
    }
    return isSpacePreserved(node.getParentNode());
}

From source file:DomUtil.java

/**
 * Extract the textual content from a Node.
 * This is rather like the XPath value of a Node.
 * @param node The node to extract the text from
 * @return The textual value of the node
 *///from   www  .java 2  s  .c  o m
public static String getText(Node node) {
    StringBuffer reply = new StringBuffer();

    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);

        if ((child instanceof CharacterData && !(child instanceof Comment))
                || child instanceof EntityReference) {
            reply.append(child.getNodeValue());
        } else if (child.getNodeType() == Node.ELEMENT_NODE) {
            reply.append(getText(child));
        }
    }

    return reply.toString();
}

From source file:Main.java

public static Element getFirstChild(Element parent, String name) {
    NodeList nl = parent.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node n = nl.item(i);/*from  ww w  .  ja  va2s.  c o m*/
        if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(name)) {
            return (Element) n;
        }
    }
    return null;
}

From source file:Main.java

public static Element findFirstChildElementByName(Node node, String sName) {
    if (node == null || sName == null)
        return null;
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        if (sName.equals(node.getNodeName())) {
            return (Element) node;
        } else {/*w  w  w. j  ava2s . co  m*/
            NodeList nodes = node.getChildNodes();
            for (int i = 0; i < nodes.getLength(); i++) {
                Node n = nodes.item(i);
                if (n.getNodeType() == Node.ELEMENT_NODE) {
                    if (sName.equals(n.getNodeName())) {
                        return (Element) n;
                    } else {
                        Element nextNode = findFirstChildElementByName(n, sName);
                        if (nextNode != null)
                            return nextNode;
                    }
                }
            }
        }
    }
    // Should only reach here if a non-Element node is passed in
    return null;
}

From source file:Main.java

static public String getNodeValue(Node node) {
    if (node == null)
        return null;

    if (node.getNodeType() == Node.ELEMENT_NODE)
        return getElementText((Element) node);
    else if (node.getNodeType() == Node.DOCUMENT_FRAGMENT_NODE)
        return getFragmentText((DocumentFragment) node);
    else/*from w w  w .  j a v  a 2  s . c o m*/
        return node.getNodeValue();
}

From source file:Main.java

/**
 * Find all child node(immediate children only) under current node that is an element node and node name is tagName
 * This is  a combination of getChildNodes() and getElementByTagName().
 * Also this helper method returns an iterable node list for convinient use in the foreach statement.
 * Only element node directly under currentNode are returned.(i.e no grandchildren).   
 * The order of the children are maintained (removing non-element node) 
 * @param currentNode//from  ww w. j ava  2  s . c  o m
 * @param tagName - case sensitive
 * @return list of element nodes equals tagname (case sensitive) 
 */
public static List<Node> getChildElementsByTagName(Node currentNode, String tagName) {
    List<Node> results = new ArrayList<Node>();
    NodeList childNodes = currentNode.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node child = childNodes.item(i);
        if ((child.getNodeType() == Node.ELEMENT_NODE) && child.getNodeName().equals(tagName))
            results.add(child);
    }
    return results;
}

From source file:Main.java

/**
 * Extract default values of variables defined in the DOM subtree below node.
 * Default values are used to populate the variableDefs map.  Variables
 * already defined in this map will NOT be modified.
 *
 * @param node root node of DOM subtree to extract default values from.
 * @param variableDefs map which default values will be added to.
 *///  www . ja  v  a 2 s .c  o m
public static void extractVariableDefaults(final Node node, Map<String, String> variableDefs) {
    switch (node.getNodeType()) {
    case Node.ELEMENT_NODE:
        final Element element = (Element) node;
        final NamedNodeMap attrs = element.getAttributes();
        for (int i = 0; i < attrs.getLength(); i++) {
            Attr attr = (Attr) attrs.item(i);
            extractVariableDefaultsFromString(attr.getValue(), variableDefs);
        }
        break;

    case Node.CDATA_SECTION_NODE:
        String content = node.getTextContent();
        extractVariableDefaultsFromString(content, variableDefs);
        break;

    default:
        break;
    }

    final NodeList children = node.getChildNodes();
    for (int childIndex = 0; childIndex < children.getLength(); childIndex++)
        extractVariableDefaults(children.item(childIndex), variableDefs);
}

From source file:Main.java

/***************************************************************************
 * Returns the value of the first child under the give element with the
 * given name.//from w ww .j  ava 2 s  . c o m
 * 
 * @param e
 * @param name
 * @return
 * @throws Exception
 **************************************************************************/
public static String getChildValueByName(Element e, String name) throws Exception {
    String s = "Not found";

    /*
     * The getElementsByTagName() function returns ANY children under the
     * given element with the given tag name. This function is intended to
     * return the value of only an immediate child with a given name.
     */
    NodeList childNodes = e.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node node = childNodes.item(i);
        if (node.getNodeType() != Node.ELEMENT_NODE)
            continue;

        if (node.getNodeName().equals(name)) {
            if (node.getFirstChild() != null)
                s = node.getFirstChild().getNodeValue();
            else
                s = "";
            break;
        }
    }

    return s;
}

From source file:Main.java

/**
 * Returns the node type name from the node type code
 *
 * @param nodeType//from   ww w  .j av  a2s  . c  o m
 * @return
 */
public static String getTypeName(short nodeType) {
    switch (nodeType) {
    case Node.ELEMENT_NODE:
        return "element";
    case Node.DOCUMENT_NODE:
        return "document";
    case Node.TEXT_NODE:
        return "text";
    case Node.ATTRIBUTE_NODE:
        return "attribute";
    case Node.CDATA_SECTION_NODE:
        return "cdata";
    }
    return "Unknown[" + nodeType + "]";
}