Example usage for org.w3c.dom Element getChildNodes

List of usage examples for org.w3c.dom Element getChildNodes

Introduction

In this page you can find the example usage for org.w3c.dom Element getChildNodes.

Prototype

public NodeList getChildNodes();

Source Link

Document

A NodeList that contains all children of this node.

Usage

From source file:Main.java

/**
 * This method searches children of Element element for element with tagName
 * and namespaceURI nsName. It searchs one level down only.
 * //from  w  ww  .ja  v a  2s .  co  m
 * @param element
 *            The root element
 * @param nsName
 *            NamespaceURI
 * @param tagName
 *            A String representing the name of the tag to be searched for.
 * @return A List of elements that meet the criterial.
 */
public static List getElementsByTagNameNS1(Element element, String nsName, String tagName) {
    List list = new ArrayList();
    if (element != null) {
        NodeList nl = element.getChildNodes();
        int length = nl.getLength();
        Node child = null;
        String childName;
        String childNS;
        for (int i = 0; i < length; i++) {
            child = nl.item(i);
            childName = child.getLocalName();
            childNS = child.getNamespaceURI();
            if ((childName != null) && (childName.equals(tagName)) && (childNS != null)
                    && (childNS.equals(nsName))) {
                list.add(child);
            }
        }
    }
    return list;
}

From source file:Main.java

public static void setElementTextValue(Element e, String text, boolean cdata) {
    Document root = e.getOwnerDocument();
    e.normalize();//from w w w. jav a2  s  . c  om
    if (e.hasChildNodes()) {
        NodeList nl = e.getChildNodes();

        /* This suxx: NodeList Object is changed when removing children !!!
           I will store all nodes that should be deleted in a Vector and delete them afterwards */
        int length = nl.getLength();

        List<Node> v = new ArrayList<Node>(nl.getLength());
        for (int i = 0; i < length; i++)
            if (nl.item(i) instanceof CharacterData)
                v.add(nl.item(i));
        for (Node n : v)
            e.removeChild(n);
    }

    if (cdata) {
        e.appendChild(root.createCDATASection(text));
    } else {
        e.appendChild(root.createTextNode(text));
    }
}

From source file:Main.java

/**
 * Gets the value of an element. This method returns a concatenated String
 * from all its TEXT children.// ww w  . j a v a 2  s . co  m
 * 
 * @param element
 *            a DOM tree element.
 * @return A String that contained in its TEXT children; or null if an error
 *         occurred.
 */
public static String getElementValue(Element element) {
    if (element == null) {
        return null;
    }
    StringBuffer sb = new StringBuffer(1000);
    NodeList nl = element.getChildNodes();
    Node child = null;
    int length = nl.getLength();
    for (int i = 0; i < length; i++) {
        child = nl.item(i);
        if (child.getNodeType() == Node.TEXT_NODE) {
            sb.append(child.getNodeValue());
        }
    }

    return sb.toString().trim();
}

From source file:Main.java

/**
 * This method searches children of Element element for element with tagName
 * and namespaceURI nsName. It searchs one level down only.
 * @param element The root element/*w  w w.  j av a2s .c om*/
 * @param nsName NamespaceURI
 * @param tagName A String representing the name of the tag to be searched
 *                        for.
 * @return A List of elements that meet the criterial.
 */
@SuppressWarnings("unchecked")
public static List getElementsByTagNameNS1(Element element, String nsName, String tagName) {
    List list = new ArrayList();
    if (element != null) {
        NodeList nl = element.getChildNodes();
        int length = nl.getLength();
        Node child = null;
        String childName;
        String childNS;

        for (int i = 0; i < length; i++) {
            child = nl.item(i);
            childName = child.getLocalName();
            childNS = child.getNamespaceURI();

            if ((childName != null) && (childName.equals(tagName)) && (childNS != null)
                    && (childNS.equals(nsName))) {
                list.add(child);
            }
        }
    }

    return list;
}

From source file:Main.java

/**
 * Gets the value of an element. This method returns a concatenated String
 * from all its TEXT children./*  ww  w.  j a v  a  2  s  .com*/
 * 
 * @param element
 *            a DOM tree element.
 * @return A String that contained in its TEXT children; or null if an error
 *         occurred or the input contain non Node.TEXT_NODE node.
 */
public static String getElementString(Element element) {
    if (element == null) {
        return null;
    }
    StringBuffer sb = new StringBuffer(1000);
    NodeList nl = element.getChildNodes();
    Node child = null;
    for (int i = 0, length = nl.getLength(); i < length; i++) {
        child = nl.item(i);
        if (child.getNodeType() == Node.TEXT_NODE) {
            sb.append(child.getNodeValue());
        } else {
            return null;
        }
    }

    return sb.toString().trim();
}

From source file:Main.java

/**
 * Convert an XML fragment from one namespace to another.
 *
 * @param from      element to translate
 * @param namespace namespace to be translated to
 * @return the element in the new namespace
 *
 * @since 8.4//w w  w  . j  av  a 2  s.c  o m
 */
public static Element translateXML(Element from, String namespace) {
    Element to = from.getOwnerDocument().createElementNS(namespace, from.getLocalName());
    NodeList nl = from.getChildNodes();
    int length = nl.getLength();
    for (int i = 0; i < length; i++) {
        Node node = nl.item(i);
        Node newNode;
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            newNode = translateXML((Element) node, namespace);
        } else {
            newNode = node.cloneNode(true);
        }
        to.appendChild(newNode);
    }
    NamedNodeMap m = from.getAttributes();
    for (int i = 0; i < m.getLength(); i++) {
        Node attr = m.item(i);
        to.setAttribute(attr.getNodeName(), attr.getNodeValue());
    }
    return to;
}

From source file:Main.java

public static List getAllChildElements(Element parent, String name) {
    List elements = null;// www  . ja va 2s . c  om
    if (parent != null) {
        elements = Collections.synchronizedList(new ArrayList());
        NodeList childNodes = parent.getChildNodes();
        for (int current = 0; current < childNodes.getLength(); current++) {
            Node node = childNodes.item(current);
            if ((node instanceof Element) && node.getNodeName().equals(name)) {
                elements.add(node);
            }
        }
    }
    return elements;
}

From source file:Main.java

private static void traverseNodes(Element parent, String tagName, List<Element> lst) {
    if (parent.getTagName().indexOf(tagName) != -1) {
        lst.add(parent);/*from   www .  j av a2s.co m*/
        return;
    } else {
        NodeList nodeList = parent.getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node item = nodeList.item(i);
            if (item.getNodeType() == Node.ELEMENT_NODE) {
                traverseNodes((Element) item, tagName, lst);
            }
        }
    }
}

From source file:Main.java

public static String getTagValue(Node node, String name) {

    if (node.getNodeType() == Node.ELEMENT_NODE) {

        Element el = (Element) node;
        NodeList nodeList = el.getElementsByTagName(name);
        if (nodeList.getLength() > 0) {
            el = (Element) nodeList.item(0);
            nodeList = el.getChildNodes();

            if (nodeList.getLength() > 0) {
                return ((Node) nodeList.item(0)).getNodeValue();
            }//from  w  w  w. j  av  a 2 s.co  m
        }

    }
    return "";
}

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.
 * /*from ww w. ja v  a  2s .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;
}