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 Element getFirstChildElementNS(Element elm, String tns, String localName) {
    if (tns == null && localName == null)
        return getFirstChildElement(elm);

    if (tns == null || tns.length() == 0)
        return getFirstChildElement(elm, localName);

    NodeList nl = elm.getChildNodes();
    for (int c = 0; c < nl.getLength(); c++) {
        Node node = nl.item(c);/*from  w ww .  j  av  a 2s. c om*/
        if (node.getNodeType() != Node.ELEMENT_NODE)
            continue;

        if (localName == null && tns.equals(node.getNamespaceURI()))
            return (Element) node;

        if (localName != null && tns.equals(node.getNamespaceURI()) && localName.equals(node.getLocalName()))
            return (Element) node;
    }

    return null;
}

From source file:Main.java

/**
 * Get all child elements of the specified (root) element. If the root is null, an illegal argument exception is
 * thrown. If the root has no children, an empty array is returned.
 * // ww  w . j a va 2  s.c  o  m
 * @param root The element to search.
 * @return An array of all child elements of the root.
 */
public static ArrayList<Element> getElements(Element root) {
    if (root == null)
        throw new IllegalArgumentException("Null or invalid root element!");
    NodeList lst = root.getChildNodes();
    final int n = lst.getLength();
    ArrayList<Element> a = new ArrayList<Element>(n);
    for (int i = 0; i < n; i++) {
        Node node = lst.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE)
            a.add((Element) node);
    }
    return a;
}

From source file:Main.java

/**
 * Get Element that has attribute id="xyz".
 *//*from   ww w.j a v a  2 s. co  m*/
public static Element getElementById(NodeList nodeList, String id) {
    // Note we should really use the Query here !!

    Element element = null;
    int len = nodeList.getLength();
    for (int i = 0; i < len; i++) {
        Node node = (Node) nodeList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            element = (Element) node;
            if (((Element) node).getAttribute("id").equals(id)) {
                // found it !
                break;
            }
        }
    }

    // returns found element or null
    return element;
}

From source file:Main.java

/**
 * Helper method to get a list of only {@link Text} and {@link Element} typed {@link Node}s.
 * This is partially to workaround the difficulty of working with the {@link NodeList} object.
 * //  www  .  jav a  2  s.c  o m
 * @param node
 *            the node whose children to get
 * 
 * @return the filtered list of child nodes
 */
private static List<Node> getChildNodes(Node node) {
    List<Node> children = new ArrayList<Node>();
    NodeList nl = node.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node child = nl.item(i);
        short type = child.getNodeType();
        if (type == Node.ELEMENT_NODE) {
            children.add(child);
        } else if (type == Node.TEXT_NODE) {
            String text = ((Text) child).getTextContent().trim();
            if (text.length() > 0) {
                children.add(child);
            }
        }
    }
    return children;
}

From source file:Main.java

/**
   Return a list of all the children of a given node
 *///from w w  w . jav a2 s .  c  o m
static public Vector<Node> findAllChildren(Node node) {
    if (node == null)
        return null;

    Vector<Node> found_children = new Vector<Node>();
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child.getNodeType() == Node.ELEMENT_NODE)
            found_children.add(child);
    }
    return found_children;
}

From source file:Main.java

static public ArrayList<Element> selectElements(Element element, String xpathExpression)
        throws XPathExpressionException {
    ArrayList<Element> resultVector = new ArrayList<Element>();
    if (element == null) {
        return resultVector;
    }/*w  w w .j a  v a2  s  .  c om*/
    if (xpathExpression.indexOf("/") == -1) {
        NodeList nodeList = element.getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(xpathExpression)) {
                resultVector.add((Element) node);
            }
        }
    } else {
        XPath xpath = XPathFactory.newInstance().newXPath();
        NodeList nodes = (NodeList) xpath.evaluate(xpathExpression, element, XPathConstants.NODESET);

        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);
            resultVector.add((Element) node);
        }
    }
    return resultVector;
}

From source file:Main.java

/**
 * Remove empty CDATA sections from the given node and all of its descendants. Some parsers
 * silently discard empty CDATA sections, and this method may be used to compare the output from
 * parsers that behave differently with respect to empty CDATA sections.
 * //from ww w.j a  v a 2 s  .  c om
 * @param node
 *            the node to process
 */
public static void removeEmptyCDATASections(Node node) {
    Node child = node.getFirstChild();
    while (child != null) {
        Node next = child.getNextSibling();
        switch (child.getNodeType()) {
        case Node.CDATA_SECTION_NODE:
            if (child.getNodeValue().length() == 0) {
                child.getParentNode().removeChild(child);
            }
            break;
        case Node.ELEMENT_NODE:
            removeEmptyCDATASections(child);
        }
        child = next;
    }
}

From source file:Main.java

/**
 * Find the next sibling element.//from  w w w .  j  a va  2 s .  co  m
 * 
 * @param startNode XML start node.
 * @return the next sibling element.
 */
protected static Element findNextSibling(Node startNode) {
    Node node = null;
    Element returnElement = null;

    // Find the next sibling element
    for (node = startNode; node != null && returnElement == null; node = node.getNextSibling()) {
        // If this node is an element node, then return it
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            returnElement = (Element) node;
        }
    }

    // Return next sibling element
    return (Element) returnElement;
}

From source file:Main.java

/**
 * @param node/*from ww w  .  ja v  a2 s.com*/
 * @throws IOException
 */
public static void serializeNode(Node node) throws IOException {
    if (writer == null)
        writer = new BufferedWriter(new OutputStreamWriter(System.out));

    switch (node.getNodeType()) {
    case Node.DOCUMENT_NODE:
        Document doc = (Document) node;
        writer.write("<?xml version=\"");
        writer.write(doc.getXmlVersion());
        writer.write("\" encoding=\"UTF-8\" standalone=\"");
        if (doc.getXmlStandalone())
            writer.write("yes");
        else
            writer.write("no");
        writer.write("\"?>\n");

        NodeList nodes = node.getChildNodes();
        if (nodes != null)
            for (int i = 0; i < nodes.getLength(); i++)
                serializeNode(nodes.item(i));
        break;
    case Node.ELEMENT_NODE:
        String name = node.getNodeName();
        writer.write("<" + name);
        NamedNodeMap attributes = node.getAttributes();
        for (int i = 0; i < attributes.getLength(); i++) {
            Node current = attributes.item(i);
            writer.write(" " + current.getNodeName() + "=\"");
            print(current.getNodeValue());
            writer.write("\"");
        }
        writer.write(">");

        NodeList children = node.getChildNodes();
        if (children != null) {
            //if ((children.item(0) != null) && (children.item(0).getNodeType() == Node.ELEMENT_NODE))
            //  writer.write("\n");

            for (int i = 0; i < children.getLength(); i++)
                serializeNode(children.item(i));
            if ((children.item(0) != null)
                    && (children.item(children.getLength() - 1).getNodeType() == Node.ELEMENT_NODE))
                writer.write("");
        }

        writer.write("</" + name + ">");
        break;
    case Node.TEXT_NODE:
        print(node.getNodeValue());
        break;
    case Node.CDATA_SECTION_NODE:
        writer.write("CDATA");
        print(node.getNodeValue());
        writer.write("");
        break;
    case Node.COMMENT_NODE:
        writer.write("<!-- " + node.getNodeValue() + " -->\n");
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        writer.write("<?" + node.getNodeName() + " " + node.getNodeValue() + "?>\n");
        break;
    case Node.ENTITY_REFERENCE_NODE:
        writer.write("&" + node.getNodeName() + ";");
        break;
    case Node.DOCUMENT_TYPE_NODE:
        DocumentType docType = (DocumentType) node;
        String publicId = docType.getPublicId();
        String systemId = docType.getSystemId();
        String internalSubset = docType.getInternalSubset();
        writer.write("<!DOCTYPE " + docType.getName());
        if (publicId != null)
            writer.write(" PUBLIC \"" + publicId + "\" ");
        else
            writer.write(" SYSTEM ");
        writer.write("\"" + systemId + "\"");
        if (internalSubset != null)
            writer.write(" [" + internalSubset + "]");
        writer.write(">\n");
        break;
    }
    writer.flush();
}

From source file:Main.java

/**
 * Get the first direct child element of the passed element.
 *
 * @param aStartNode/*  ww w . j  a  v a  2s .com*/
 *        The element to start searching.
 * @return <code>null</code> if the passed element does not have any direct
 *         child element.
 */
@Nullable
public static Element getFirstChildElement(@Nonnull final Node aStartNode) {
    final NodeList aNodeList = aStartNode.getChildNodes();
    final int nLen = aNodeList.getLength();
    for (int i = 0; i < nLen; ++i) {
        final Node aNode = aNodeList.item(i);
        if (aNode.getNodeType() == Node.ELEMENT_NODE)
            return (Element) aNode;
    }
    return null;
}