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

/**
 * Returns the first child element that matches the given local name and
 * namespace. If no child element is present or no child element matches,
 * <code>null</code> is returned.
 *
 * @param parent//w  ww  .j  a  va2s  .  c  om
 * @param childLocalName
 * @param childNamespaceURI
 * @return first child element matching the specified names or <code>null</code>.
 */
public static Element getChildElement(Node parent, String childLocalName, String childNamespaceURI) {
    if (parent != null) {
        NodeList children = parent.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);
            if (child.getNodeType() == Node.ELEMENT_NODE && childLocalName.equals(child.getLocalName())
                    && childNamespaceURI.equals(child.getNamespaceURI())) {
                return (Element) child;
            }
        }
    }
    return null;
}

From source file:Main.java

/**
 * Finds the first element which name matchtes a given tag name
 * that is locacted anywhere below the given parent.
 * //www  .j  av a 2s  .  c o m
 * @param parent the parent element below which to search the child
 * @param tagName the (tag) name of the desired child element
 * @return the child element if an element of that name existed, or null otherwise
 */
static public Element findFirstChildDeep(Element parent, String tagName) { // Child Element suchen
    if (parent == null)
        return null;
    NodeList nl = parent.getElementsByTagName(tagName);
    for (int i = 0; i < nl.getLength(); i++) {
        if (nl.item(i).getNodeType() == Node.ELEMENT_NODE)
            return (Element) nl.item(i);
    }
    return null;
}

From source file:Main.java

protected static void print(PrintStream out, Node node) {
    if (node == null)
        return;/*w ww . ja va 2s  .com*/
    short type = node.getNodeType();
    switch (type) {
    case Node.DOCUMENT_NODE: {
        out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        //out.println("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n");
        NodeList nodelist = node.getChildNodes();
        int size = nodelist.getLength();
        for (int i = 0; i < size; i++)
            print(out, nodelist.item(i));
        break;
    }

    case Node.DOCUMENT_TYPE_NODE: {
        DocumentType docType = (DocumentType) node;
        out.print("<!DOCTYPE " + getDocumentTypeData(docType) + ">\n");
        break;
    }

    case Node.ELEMENT_NODE: {
        out.print('<');
        out.print(node.getNodeName());
        NamedNodeMap map = node.getAttributes();
        if (map != null) {
            int size = map.getLength();
            for (int i = 0; i < size; i++) {
                Attr attr = (Attr) map.item(i);
                out.print(' ');
                out.print(attr.getNodeName());
                out.print("=\"");
                out.print(normalize(attr.getNodeValue()));
                out.print('"');
            }
        }

        if (!node.hasChildNodes())
            out.print("/>");
        else {
            out.print('>');
            NodeList nodelist = node.getChildNodes();
            int numChildren = nodelist.getLength();
            for (int i = 0; i < numChildren; i++)
                print(out, nodelist.item(i));

            out.print("</");
            out.print(node.getNodeName());
            out.print('>');
        }
        break;
    }

    case Node.ENTITY_REFERENCE_NODE: {
        NodeList nodelist = node.getChildNodes();
        if (nodelist != null) {
            int size = nodelist.getLength();
            for (int i = 0; i < size; i++)
                print(out, nodelist.item(i));

        }
        break;
    }

    case Node.CDATA_SECTION_NODE: {
        out.print(normalize(node.getNodeValue()));
        break;
    }

    case Node.TEXT_NODE: {
        out.print(normalize(node.getNodeValue()));
        break;
    }

    case Node.PROCESSING_INSTRUCTION_NODE: {
        out.print("<?");
        out.print(node.getNodeName());
        String s = node.getNodeValue();
        if (s != null && s.length() > 0) {
            out.print(' ');
            out.print(s);
        }
        out.print("?>");
        break;
    }

    case Node.COMMENT_NODE: {
        out.print("<!--");
        out.print(node.getNodeValue());
        out.print("-->");
        break;
    }

    default: {
        out.print(normalize(node.getNodeValue()));
        break;
    }
    }
    out.flush();
}

From source file:Main.java

private static String getXPathFromVector(Vector path) {
    StringBuffer strBuf = new StringBuffer();
    int length = path.size();

    for (int i = 0; i < length; i++) {
        Node tempNode = (Node) path.elementAt(i);
        short nodeType = getNodeType(tempNode);
        String targetValue = getValue(tempNode, nodeType);
        int position = 1;

        tempNode = getPreviousTypedNode(tempNode, nodeType);

        while (tempNode != null) {
            if (nodeType == Node.ELEMENT_NODE) {
                if (getValue(tempNode, nodeType).equals(targetValue)) {
                    position++;//from w w w . j a  v a 2s  .co  m
                }
            } else {
                position++;
            }

            tempNode = getPreviousTypedNode(tempNode, nodeType);
        }

        boolean hasMatchingSiblings = (position > 1);

        if (!hasMatchingSiblings) {
            tempNode = (Node) path.elementAt(i);
            tempNode = getNextTypedNode(tempNode, nodeType);

            while (!hasMatchingSiblings && tempNode != null) {
                if (nodeType == Node.ELEMENT_NODE) {
                    if (getValue(tempNode, nodeType).equals(targetValue)) {
                        hasMatchingSiblings = true;
                    } else {
                        tempNode = getNextTypedNode(tempNode, nodeType);
                    }
                } else {
                    hasMatchingSiblings = true;
                }
            }
        }

        String step;

        switch (nodeType) {
        case Node.TEXT_NODE:
            step = "text()";
            break;
        case Node.PROCESSING_INSTRUCTION_NODE:
            step = "processing-instruction()";
            break;
        default:
            step = targetValue;
            break;
        }

        if (step != null && step.length() > 0) {
            strBuf.append('/' + step);
        }

        if (hasMatchingSiblings) {
            strBuf.append("[" + position + "]");
        }
    }

    return strBuf.toString();
}

From source file:Main.java

public static List<Element> elements(Element element, String tagName) {
    if (element == null || !element.hasChildNodes()) {
        return Collections.emptyList();
    }/*w w w  .  ja  va  2  s  .  co m*/

    List<Element> elements = new ArrayList<Element>();
    for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            Element childElement = (Element) child;
            String childTagName = childElement.getLocalName();

            if (tagName.equals(childTagName))
                elements.add(childElement);
        }
    }
    return elements;
}

From source file:Main.java

static public Element[] findChildElements(Node first, Node last, String name) {
    Vector v = new Vector();
    while (first != last) {
        if (first.getNodeType() == Node.ELEMENT_NODE) {
            if (first.getNodeName().equals(name))
                v.addElement(first);/*from   w w w .j a v  a2s .  c  om*/
        }
        first = first.getNextSibling();
    }
    Element array[] = new Element[v.size()];
    for (int i = 0; i < array.length; ++i) {
        array[i] = (Element) v.elementAt(i);
    }
    return array;
}

From source file:Main.java

public static List<Element> elementsQName(Element element, Set<QName> allowedTagNames) {
    if (element == null || !element.hasChildNodes()) {
        return Collections.emptyList();
    }/*from  w ww.j av a  2s.  c  om*/

    List<Element> elements = new ArrayList<Element>();
    for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            Element childElement = (Element) child;
            QName childQName = new QName(childElement.getNamespaceURI(), childElement.getLocalName());
            if (allowedTagNames.contains(childQName)) {
                elements.add(childElement);
            }
        }
    }
    return elements;
}

From source file:Main.java

/**
 * For the given {@code sourceNode}, read each "top level" element into the resulting {@code Map}. Each element name
 * is a key to the map, each element value is the value paired to the key. Example - anchor is the node, label and
 * href are keys://from ww  w . j a  va 2 s .  c om
 *
 * <pre>
 * {@code
 * <anchor>
 *  <label>Slashdot</label>
 *    <href>http://slashdot.org/</href>
 *  </anchor>
 * }
 * </pre>
 */
public static Map<String, String> readNodeElementsToMap(final Node sourceNode) {
    Map<String, String> result = new HashMap<String, String>();

    if (sourceNode == null) {
        return result;
    }

    NodeList childNodes = sourceNode.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node element = childNodes.item(i);

        if (element.getNodeType() == Node.ELEMENT_NODE) {
            String elementName = element.getNodeName();
            String elementValue = "";
            Node firstChild = element.getFirstChild();

            if (firstChild != null) {
                elementValue = firstChild.getNodeValue();
            }

            if (elementValue != null) {
                result.put(elementName, elementValue);
            }
        }
    }

    return result;
}

From source file:Main.java

/**
 * Get the first child Element of the supplied node that matches a given tag name.
 *
 * @param node The DOM Node./*ww  w. j  a  v  a 2  s  . c o  m*/
 * @param name The name of the child node to search for.
 * @return The first child element with the matching tag name.
 */
public static Element getFirstChildElementByName(Node node, String name) {
    NodeList children = node.getChildNodes();
    int childCount = children.getLength();

    for (int i = 0; i < childCount; i++) {
        Node child = children.item(i);
        if (child != null && child.getNodeType() == Node.ELEMENT_NODE && child.getNodeName() != null
                && child.getNodeName().equals(name)) {
            return (Element) child;
        }
    }
    return null;
}

From source file:Main.java

/**
 * Generates an XPath expression that will return only the given node as its
 * result. This method only works for element, text, document and PI nodes.
 *
 * @param node the node to generate an XPath expression for. This node must
 * be an element node, a text node, a document node, or a processing
 * instruction node./*  w w w.jav a 2 s. c om*/
 * @return an XPath expression that will return only the given node as its
 * result.
 * @exception IllegalArgumentException if the given node is not an element,
 * text, document or PI node.
 */
public static String getXPathExprFromNode(Node node) throws IllegalArgumentException {
    short nodeType = getNodeType(node);

    switch (nodeType) {
    case Node.ELEMENT_NODE:
    case Node.TEXT_NODE:
    case Node.PROCESSING_INSTRUCTION_NODE:
        return getXPathFromVector(getVectorPathFromNode(node));

    case Node.DOCUMENT_NODE:
        return "/";

    default:
        throw new IllegalArgumentException("Only works for element, text, " + "document, and PI nodes.");
    }
}