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 all children of the given element which are element named as
 * specified./* w w w.  j a v  a  2s  . co  m*/
 */
public static List<Element> getNamedChildren(Element element, String elementNames) {
    List<Element> result = new ArrayList<Element>();
    NodeList children = element.getChildNodes();
    for (int i = 0; i < children.getLength(); ++i) {
        Node node = children.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(elementNames)) {
            result.add((Element) node);
        }
    }
    return result;
}

From source file:Main.java

public static Element findSingleElement(Element parent, String fullXPath) {
    Element elt = null;//from   w  w w .j  a v a  2  s .  c  om
    if (parent != null) {
        if (parent.hasChildNodes()) {
            if (fullXPath.startsWith("/"))
                fullXPath = fullXPath.substring(1);
            int index = fullXPath.indexOf("/");
            String childName = ((index != -1) ? fullXPath.substring(0, index) : fullXPath);

            NodeList list = parent.getChildNodes();
            for (int i = 0; i < list.getLength(); i++) {
                Node child = list.item(i);
                if (child.getNodeType() == Node.ELEMENT_NODE) {
                    if (child.getNodeName().equalsIgnoreCase(childName)) {
                        if (index == -1) {
                            elt = (Element) child;
                            break;
                        } else {
                            fullXPath = fullXPath.substring(index + 1);
                            elt = findSingleElement((Element) child, fullXPath);
                        }
                    }
                }
            }
        }
    }
    return elt;
}

From source file:Main.java

/**
 * Access all immediate child elements inside the given Element
 *
 * @param element the starting element, cannot be null.
 * @param elemName the name of the child element to look for, cannot be
 * null./*from  w  ww . jav a  2 s .  c  o  m*/
 * @return array of all immediate child element inside element, or
 * an array of size zero if no child elements are found.
 */
public static Element[] getChildElements(Element element) {
    NodeList list = element.getChildNodes();
    int len = list.getLength();
    ArrayList<Node> array = new ArrayList<Node>(len);

    for (int i = 0; i < len; i++) {
        Node n = list.item(i);

        if (n.getNodeType() == Node.ELEMENT_NODE) {
            array.add(n);
        }
    }
    Element[] elems = new Element[array.size()];

    return (Element[]) array.toArray(elems);
}

From source file:com.msopentech.odatajclient.engine.data.json.DOMTreeUtilsV4.java

public static void writeSubtree(final ODataClient client, final JsonGenerator jgen, final Node content,
        final boolean propType) throws IOException {
    for (Node child : XMLUtils.getChildNodes(content, Node.ELEMENT_NODE)) {
        final String childName = XMLUtils.getSimpleName(child);

        final Node typeAttr = child.getAttributes().getNamedItem(ODataConstants.ATTR_M_TYPE);
        if (typeAttr != null && EdmSimpleType.isGeospatial(typeAttr.getTextContent())) {
            jgen.writeStringField(//ww  w .  j  a va  2  s  .  c o m
                    propType ? ODataConstants.JSON_TYPE : childName + "@" + ODataConstants.JSON_TYPE,
                    typeAttr.getTextContent());

            jgen.writeObjectFieldStart(childName);
            GeospatialJSONHandler.serialize(jgen, (Element) child, typeAttr.getTextContent());
            jgen.writeEndObject();
        } else if (XMLUtils.hasOnlyTextChildNodes(child)) {
            if (child.hasChildNodes()) {
                final String out;
                if (typeAttr == null) {
                    out = child.getChildNodes().item(0).getNodeValue();
                } else {
                    if (typeAttr.getTextContent().startsWith("Edm.")) {
                        final EdmSimpleType type = EdmSimpleType.fromValue(typeAttr.getTextContent());
                        final ODataPrimitiveValue value = client.getPrimitiveValueBuilder().setType(type)
                                .setText(child.getChildNodes().item(0).getNodeValue()).build();
                        out = value.toString();

                        jgen.writeStringField(childName + "@" + ODataConstants.JSON_TYPE, type.toString());
                    } else {
                        // enum
                        out = child.getTextContent();
                        jgen.writeStringField(childName + "@" + ODataConstants.JSON_TYPE,
                                typeAttr.getTextContent());
                    }
                }
                jgen.writeStringField(childName, out);
            } else {
                if (child.getAttributes().getNamedItem(ODataConstants.ATTR_NULL) == null) {
                    if (typeAttr != null && EdmSimpleType.String.toString().equals(typeAttr.getTextContent())) {
                        jgen.writeStringField(childName + "@" + ODataConstants.JSON_TYPE,
                                typeAttr.getTextContent());
                        jgen.writeStringField(childName, StringUtils.EMPTY);
                    } else {
                        jgen.writeArrayFieldStart(childName);
                        jgen.writeEndArray();
                    }
                } else {
                    jgen.writeNullField(childName);
                }
            }
        } else {
            if (XMLUtils.hasElementsChildNode(child)) {
                jgen.writeArrayFieldStart(childName);

                for (Node nephew : XMLUtils.getChildNodes(child, Node.ELEMENT_NODE)) {
                    if (XMLUtils.hasOnlyTextChildNodes(nephew)) {
                        jgen.writeString(nephew.getChildNodes().item(0).getNodeValue());
                    } else {
                        jgen.writeStartObject();
                        DOMTreeUtils.writeSubtree(client, jgen, nephew);
                        jgen.writeEndObject();
                    }
                }

                jgen.writeEndArray();
            } else {
                jgen.writeObjectFieldStart(childName);
                if (typeAttr != null) {
                    jgen.writeStringField("@" + ODataConstants.JSON_TYPE, typeAttr.getTextContent());
                }

                DOMTreeUtils.writeSubtree(client, jgen, child);

                jgen.writeEndObject();
            }
        }
    }
}

From source file:Main.java

/**
 * Gather all the namespaces defined on a node
 *
 * @return//from   w  w w .j av a  2s . com
 */
public static Iterable<Entry<String, String>> getNamespaces(Element element) {
    TreeMap<String, String> map = new TreeMap<String, String>();
    do {
        NamedNodeMap attributes = element.getAttributes();
        for (int i = 0; i < attributes.getLength(); i++) {
            Attr attr = (Attr) attributes.item(i);
            final String name = attr.getLocalName();

            if (attr.getPrefix() != null) {
                if ("xmlns".equals(attr.getPrefix()))
                    if (!map.containsKey(name))
                        map.put(name, attr.getValue());
            } else if ("xmlns".equals(name)) {
                if (!map.containsKey(""))
                    map.put("", attr.getValue());
            }
        }
        if (element.getParentNode() == null || element.getParentNode().getNodeType() != Node.ELEMENT_NODE)
            break;
        element = (Element) element.getParentNode();
    } while (true);
    return map.entrySet();
}

From source file:Main.java

static public Element findChildElementNS(Node first, Node last, String namespaceuri, String qname) {
    while (first != last) {
        if (first.getNodeType() == Node.ELEMENT_NODE) {
            if (isA(first, namespaceuri, qname)) {
                return (Element) first;
            }//from   w ww .  ja va  2  s  .  com

        }
        first = first.getNextSibling();
    }
    return null;
}

From source file:Main.java

private static Element findChildById(Element parent, String tag, String attributeIdValue) {
    // First check to see if any parameters are null
    if (parent == null || tag == null)
        return null;
    // Check to see if this is the element we are interested in. This is
    // redundant apart from first call, but keep in to keep functionality
    //      if (nodeNameEqualTo(parent, tag))
    //         return parent;
    // Get all the children
    NodeList list = parent.getChildNodes();
    int listCount = list.getLength();
    for (int k = 0; k < listCount; k++) {
        Node child = list.item(k);
        // If the node is not an element, ignore it
        if (child.getNodeType() != Node.ELEMENT_NODE)
            continue;
        // Check to see if this node is the node we want
        if (nodeNameEqualTo(child, tag)) {
            if (((Element) child).getAttribute("id").equals(attributeIdValue)) {
                return (Element) child;
            }//from ww  w. j ava 2  s .  c om
        }
    }
    // Now that we have checked all children, we can recurse
    for (int k = 0; k < listCount; k++) {
        Node child = list.item(k);
        // If the node is not an element, ignore it
        if (child.getNodeType() != Node.ELEMENT_NODE)
            continue;
        Element result = findChildById((Element) child, tag, attributeIdValue);
        if (result != null)
            return result;
    }
    return null;
}

From source file:Main.java

/**
 * This method is a tree-search to help prevent against wrapping attacks. It checks that no
 * two Elements have ID Attributes that match the "value" argument, if this is the case then
 * "false" is returned. Note that a return value of "true" does not necessarily mean that
 * a matching Element has been found, just that no wrapping attack has been detected.
 *//*from   ww  w  .  j  av a2 s  .  c o m*/
public static boolean protectAgainstWrappingAttack(Node startNode, String value) {
    Node startParent = startNode.getParentNode();
    Node processedNode = null;
    Element foundElement = null;

    String id = value.trim();
    if (id.charAt(0) == '#') {
        id = id.substring(1);
    }

    while (startNode != null) {
        if (startNode.getNodeType() == Node.ELEMENT_NODE) {
            Element se = (Element) startNode;

            NamedNodeMap attributes = se.getAttributes();
            if (attributes != null) {
                for (int i = 0; i < attributes.getLength(); i++) {
                    Attr attr = (Attr) attributes.item(i);
                    if (attr.isId() && id.equals(attr.getValue())) {
                        if (foundElement == null) {
                            // Continue searching to find duplicates
                            foundElement = attr.getOwnerElement();
                        } else {
                            //log.debug("Multiple elements with the same 'Id' attribute value!");
                            return false;
                        }
                    }
                }
            }
        }

        processedNode = startNode;
        startNode = startNode.getFirstChild();

        // no child, this node is done.
        if (startNode == null) {
            // close node processing, get sibling
            startNode = processedNode.getNextSibling();
        }

        // no more siblings, get parent, all children
        // of parent are processed.
        while (startNode == null) {
            processedNode = processedNode.getParentNode();
            if (processedNode == startParent) {
                return true;
            }
            // close parent node processing (processed node now)
            startNode = processedNode.getNextSibling();
        }
    }
    return true;
}

From source file:Main.java

/**
 * Filter node list for Element nodes of specified name.
 *//*  ww  w  .jav a  2s  .  c  om*/
public static List<Node> filterNodeListElements(NodeList nodeList, String nodeName) {
    List<Node> nodes = new ArrayList<Node>();
    for (int k = 0; k < nodeList.getLength(); k++) {
        Node node = nodeList.item(k);
        if (node.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }
        if (nodeName != null && (node.getNodeName().equals(nodeName) == false)) {
            continue;
        }
        nodes.add(node);
    }
    return nodes;
}