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

/**
 * Extract the text in an element.//w ww  .  j  a  v a 2s . c o m
 * Concats data of Text and CDATASection elements.
 */
public static String getText(Element elem) {

    StringBuffer text = new StringBuffer();
    NodeList l = elem.getChildNodes();
    for (int i = 0; i < l.getLength(); i++) {
        Node n = l.item(i);
        if (n instanceof Text)
            text.append(((Text) n).getData());
        else if (n instanceof CDATASection)
            text.append(((CDATASection) n).getData());
    }
    return text.toString();
}

From source file:Utils.java

/**
 * //from w ww  .  ja v a  2  s  . c  o m
 */
public static void moveContent(Element from, Element to) {
    // lets move the child nodes across
    NodeList childNodes = from.getChildNodes();
    while (childNodes.getLength() > 0) {
        Node node = childNodes.item(0);
        from.removeChild(node);
        to.appendChild(node);
    }
}

From source file:Main.java

/**
 * Retrieves the given DOM element's first child element with the specified
 * name. If name is null, the first child of any type is returned.
 *//* w  w  w  . j a v  a 2 s.  c om*/
public static Element getFirstChild(final Element el, final String name) {
    final NodeList nodes = el.getChildNodes();
    final int len = nodes.getLength();
    for (int i = 0; i < len; i++) {
        final Node node = nodes.item(i);
        if (!(node instanceof Element))
            continue;
        final Element e = (Element) node;
        if (name == null || e.getTagName().equals(name))
            return e;
    }
    return null;
}

From source file:Main.java

public static String getXMLValueByName(String xmlFilePath, String name) {
    String returnValue = "No Value!";
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    try {//from w w  w .  j  a v  a2s .  c o m
        DocumentBuilder builder = builderFactory.newDocumentBuilder();
        InputStream is = new FileInputStream(xmlFilePath);
        Document doc = builder.parse(is);
        Element root = doc.getDocumentElement();
        NodeList nodes = root.getChildNodes();
        if (nodes != null) {
            for (int i = 0; i < nodes.getLength(); i++) {
                Node node = nodes.item(i);
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    String test = node.getNodeName();
                    if (test.equals(name)) {
                        returnValue = node.getTextContent().trim();
                    }
                }
            }
        }
    } catch (Exception e) {

    }
    return returnValue;
}

From source file:Main.java

/**
 * Extract the text symbol from the given DOM element, ignoring XML comments.
 * <p>Appends all CharacterData nodes and EntityReference nodes
 * into a single String symbol, excluding Comment nodes.
 * @see CharacterData//  w w w  . j a v a  2  s.  c  o m
 * @see EntityReference
 * @see Comment
 */
public static String getTextValue(Element valueEle) {
    StringBuffer value = new StringBuffer();
    NodeList nl = valueEle.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node item = nl.item(i);
        if (item instanceof CharacterData && !(item instanceof Comment) || item instanceof EntityReference)
            value.append(item.getNodeValue());
    }
    return value.toString();
}

From source file:Main.java

public static Element getFirstSubElementByName(Element element, String tagName) {
    NodeList childNodes = element.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        switch (childNodes.item(i).getNodeType()) {
        case Node.ELEMENT_NODE:
            if (((Element) childNodes.item(i)).getTagName().equalsIgnoreCase(tagName))
                return (Element) childNodes.item(i);
        }//from  www.  j a v  a  2  s  .c om
    }
    return null;
}

From source file:Main.java

public static Element getChildElement(Element parent, String childName) {
    NodeList children = parent.getChildNodes();
    int size = children.getLength();

    for (int i = 0; i < size; i++) {
        Node node = children.item(i);

        if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) node;

            if (childName.equals(element.getNodeName())) {
                return element;
            }/*from   w  ww . j  a v  a 2s.  c  o  m*/
        }
    }

    return null;
}

From source file:Main.java

public static String getText(Element element) {
    StringBuffer buf = new StringBuffer();
    NodeList list = element.getChildNodes();
    boolean found = false;
    for (int i = 0; i < list.getLength(); i++) {
        Node node = list.item(i);
        if (node.getNodeType() == Node.TEXT_NODE) {
            buf.append(node.getNodeValue());
            found = true;/*from ww w . ja  va2 s.  c  om*/
        }
    }
    return found ? buf.toString() : null;
}

From source file:Main.java

@SuppressWarnings("finally")
public static Map<String, String> getMapFromXml(String xmltext) {
    Map<String, String> map = new HashMap<String, String>();
    try {// w w w .  j  a  v  a 2s  .c om
        Element root = getElementFromXml(xmltext);
        NodeList nodeList = root.getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            String name = nodeList.item(i).getNodeName();
            String value = nodeList.item(i).getNodeValue();
            map.put(name, value);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        return map;
    }
}

From source file:TestDOM.java

public static String getSimpleElementText(Element node) {
    StringBuffer sb = new StringBuffer();
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child instanceof Text)
            sb.append(child.getNodeValue());
    }/*from  w w  w.  j a  va 2  s.  co m*/
    return sb.toString();
}