Example usage for org.w3c.dom Element getFirstChild

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

Introduction

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

Prototype

public Node getFirstChild();

Source Link

Document

The first child of this node.

Usage

From source file:Main.java

/**
 * Returns a list of the direct {@link Element} children of the given element whose names match
 * {@code tag}./*  ww w. ja  v a2  s.  c o m*/
 *
 * @param parent
 *            The {@link Element} to get the children from.
 * @param tag
 *            The element name of the children to look for.
 * @return A {@link LinkedList} of the children {@link Element}s.
 */
public static LinkedList<Element> getDirectChildren(Element parent, String tag) {
    LinkedList<Element> list = new LinkedList<>();
    for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) {
        if (child instanceof Element && tag.equals(child.getNodeName())) {
            list.add((Element) child);
        }
    }
    return list;
}

From source file:Main.java

public static List elements(Element element, String tagName) {
    ArrayList elements = new ArrayList();
    for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) {
        if (child instanceof Element && tagName.equals(child.getNodeName())) {
            elements.add(child);//w w w .java2 s  .c  o  m
        }
    }
    return elements;
}

From source file:Main.java

/**
 * Get the text content of an element./*from ww w .java2  s.com*/
 *
 * @param element
 *            The element.
 * @param sbuf
 *            The buffer to append to.
 * @param decend
 *            Whether to descend into child elements.
 */
public static void getText(final Element element, final StringBuilder sbuf, final boolean decend) {
    Node node = element.getFirstChild();

    while (node != null) {
        switch (node.getNodeType()) {
        case Node.TEXT_NODE:
            sbuf.append(node.getNodeValue());
            break;

        case Node.ELEMENT_NODE:
            if (decend) {
                getText((Element) node, sbuf, decend);
            }

            break;
        }

        node = node.getNextSibling();
    }
}

From source file:Main.java

/**
 * Find a child element./*from  w  w w .j  a v a2s . co m*/
 * @param e The element to search.
 * @param find The name to search for.
 * @return The element found, or null if not found.
 */
public static Element findElement(final Element e, final String find) {
    for (Node child = e.getFirstChild(); child != null; child = child.getNextSibling()) {
        if (!(child instanceof Element)) {
            continue;
        }
        final Element el = (Element) child;
        if (el.getNodeName().equals(find)) {
            return el;
        }
    }
    return null;
}

From source file:Main.java

public static List<Element> getChildElementList(Element parent, String nodeName) {
    List<Element> list = new ArrayList<>();
    for (org.w3c.dom.Node node = parent.getFirstChild(); node != null; node = node.getNextSibling()) {
        if (node.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE && nodeName.equals(node.getNodeName())) {
            list.add((Element) node);
        }//from  ww  w  . jav a2  s.com
    }
    return list;
}

From source file:Main.java

public static Element first(Element root, String namepaceUri, String localName) {
    if (root == null)
        return null;
    for (Node n1 = root.getFirstChild(); n1 != null; n1 = n1.getNextSibling()) {
        if (n1.getNodeType() != Node.ELEMENT_NODE)
            continue;
        Element e2 = Element.class.cast(n1);
        if (namepaceUri.equals(e2.getNamespaceURI()) && e2.getLocalName().equals(localName)) {
            return e2;
        }/*from ww  w. j a va2  s . c  om*/
    }
    return null;
}

From source file:Main.java

public static Collection<Element> getChildElementListNS(Element parent, String nodeName, String nsURI) {
    List<Element> list = new ArrayList<>();
    for (org.w3c.dom.Node node = parent.getFirstChild(); node != null; node = node.getNextSibling()) {
        if (node.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE && node.getLocalName().equals(nodeName)
                && node.getNamespaceURI().equals(nsURI)) {
            list.add((Element) node);
        }/*from www.  j  ava2 s .  com*/
    }
    return list;
}

From source file:Main.java

/**
 * Returns all child character data of the given element, including CDATA sections but NOT entity references.
 * @param parentEl the parent element.//from ww  w . j  a  va 2 s.  c om
 * @return the child character data of the element, or null if the element is null.
 */
static public String getChildCharacterData(Element parentEl) {
    if (parentEl == null) {
        return null;
    }

    Node tempNode = parentEl.getFirstChild();
    StringBuffer strBuf = new StringBuffer();
    CharacterData charData;

    while (tempNode != null) {
        switch (tempNode.getNodeType()) {
        case Node.TEXT_NODE:
        case Node.CDATA_SECTION_NODE:
            charData = (CharacterData) tempNode;
            strBuf.append(charData.getData());
            break;
        //            case Node.ENTITY_REFERENCE_NODE : strBuf.append("&").append(tempNode.getNodeName()).append(";");

        }
        tempNode = tempNode.getNextSibling();
    }
    return strBuf.toString();
}

From source file:Main.java

/**
 * Converts XML {@code <property name=""></property>} tags to Properties
 * object./*from  ww  w. ja  va2 s .co  m*/
 * 
 * @see java.util.XmlUtils.importProperties()
 * 
 * @param entries
 *            List of property nodes in the DOM
 */
public static Properties importProperties(NodeList entries) {
    Properties props = new Properties();
    int numEntries = entries.getLength();
    for (int i = 0; i < numEntries; i++) {
        Element entry = (Element) entries.item(i);
        if (entry.hasAttribute("name")) {
            Node n = entry.getFirstChild();
            String val = (n == null) ? "" : n.getNodeValue();
            props.setProperty(entry.getAttribute("name"), val);
        }
    }
    return props;
}

From source file:Main.java

public static String getElementText(Element e) {
    final StringBuilder val = new StringBuilder();
    for (Node n = e.getFirstChild(); n != null; n = n.getNextSibling()) {
        if (n.getNodeType() == Node.TEXT_NODE || n.getNodeType() == Node.CDATA_SECTION_NODE) {
            val.append(n.getNodeValue());
        }/* www .  j a va 2s . co  m*/
    }
    return val.toString();
}