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

/**
 * Retrieve the value of an XML tag. For internal use only during parsing the accounts
 * configuration file./*from   w ww.  j  ava 2 s  .  c  o  m*/
 *
 * @param element The elemement containing the tag
 * @param tagName The string name of the tag
 * @return Tag value
 */
public static String getTagValue(Element element, String tagName) throws Exception {
    try {
        NodeList tagList = element.getElementsByTagName(tagName);
        Element tagElement = (Element) tagList.item(0);
        NodeList textTagList = tagElement.getChildNodes();

        return (textTagList.item(0)).getNodeValue().trim();
    } catch (Exception e) {
        throw new Exception(
                "Error in parsing the element \"" + element.toString() + "\" with the tag \"" + tagName + "\"");
    }
}

From source file:Main.java

/** Extract all of the child nodes as a Properties object from a node in an XML document */
public static Properties extractChildNodes(Document document) {
    try {//w  ww  .j  a va2  s .c  om
        Properties props = new Properties();
        Element top = document.getDocumentElement();
        NodeList children = top.getChildNodes();
        Node child;
        String name;
        String value;
        for (int i = 0; i < children.getLength(); i++) {
            child = children.item(i);
            name = child.getNodeName();
            value = child.getTextContent().trim();
            props.setProperty(name, value);
        }
        return props;
    } catch (Exception e) {
        return new Properties();
    }
}

From source file:Main.java

public static List<Element> elements(Element element) {
    List<Element> elements = new ArrayList();
    NodeList nodeList = element.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if ((node instanceof Element) && (element == node.getParentNode())) {
            elements.add((Element) node);
        }/* w w w  . j a  v  a  2s  .c  om*/
    }
    return elements;
}

From source file:Main.java

/**
 * Get all child elements/* ww  w .j ava2  s. co m*/
 * @param ele parent element
 * @return list of child elements
 */
public static List<Element> getChildElements(Element ele) {
    List<Element> list = new ArrayList<>();
    NodeList childNodes = ele.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        if (childNodes.item(i).getNodeType() == Node.ELEMENT_NODE) {
            list.add((Element) childNodes.item(i));
        }
    }
    return list;
}

From source file:Main.java

public static Element[] getChildrenByName(Element element, String paramString) {
    NodeList localNodeList = element.getChildNodes();
    int i = localNodeList.getLength();
    LinkedList<Node> nodes = new LinkedList<Node>();
    for (int j = 0; j < i; ++j) {
        Node localNode = localNodeList.item(j);
        if ((localNode.getNodeType() != 1) || (!localNode.getNodeName().equals(paramString)))
            continue;
        nodes.add(localNode);//from  ww  w  .  j  a v  a2s.co  m
    }
    return (Element[]) nodes.toArray(new Element[nodes.size()]);
}

From source file:Main.java

public static String getStringNodeValue(Node node, String nodeName) {
    Element fstElmnt = (Element) node;
    NodeList fstNmElmntLst = fstElmnt.getElementsByTagName(nodeName);

    if (fstNmElmntLst.getLength() == 0)
        return "";

    Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
    NodeList fstNm = fstNmElmnt.getChildNodes();

    if (fstNm.getLength() > 0) {
        String retValue = ((Node) fstNm.item(0)).getNodeValue();
        try {/*from  ww w . j ava 2 s .  c o  m*/
            return new String(retValue.getBytes("ISO-8859-1"));
        } catch (UnsupportedEncodingException e) {
        }
    }
    return "";
}

From source file:Main.java

public static List<Element> getChildElements(Element parent) {
    List<Element> ret = new ArrayList<Element>();
    NodeList childList = parent.getChildNodes();
    for (int i = 0; i < childList.getLength(); i++) {
        if (childList.item(i).getNodeType() != Node.ELEMENT_NODE)
            continue;
        Element child = (Element) childList.item(i);
        ret.add(child);//w w w  .ja v  a2  s. co  m
    }
    return ret;
}

From source file:Main.java

/***************************************************************************
 * Sets the value of the first child under the given element with the given
 * name. If the child has no nodes, one is created.
 * /*from  ww  w  .j  a v a 2 s  .  c om*/
 * @param doc
 * @param e
 * @param name
 * @param value
 * @return
 * @throws Exception
 **************************************************************************/
public static boolean setChildValueByName(Document doc, Element e, String name, String value) throws Exception {
    NodeList childNodes = e.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node node = childNodes.item(i);
        if (node.getNodeType() != Node.ELEMENT_NODE)
            continue;

        if (node.getNodeName().equals(name)) {
            Node child = node.getFirstChild();
            if (child == null)
                ((Element) node).appendChild(doc.createTextNode(value));
            else
                child.setNodeValue(value);

            return true;
        }
    }

    return false;
}

From source file:Main.java

/**
 * Method getFullTextChildrenFromElement
 *
 * @param element/* w  ww  .  j  a v  a2  s  .  co  m*/
 * @return the string of chi;ds
 */
public static String getFullTextChildrenFromElement(Element element) {

    StringBuffer sb = new StringBuffer();
    NodeList children = element.getChildNodes();
    int iMax = children.getLength();

    for (int i = 0; i < iMax; i++) {
        Node curr = children.item(i);

        if (curr.getNodeType() == Node.TEXT_NODE) {
            sb.append(((Text) curr).getData());
        }
    }

    return sb.toString();
}

From source file:Main.java

/**
 * This will get the text value of an element.
 *
 * @param node The node to get the text value for.
 * @return The text of the node.//  w ww  . j a  v  a2 s . c om
 */
public static String getNodeValue(Element node) {
    StringBuilder sb = new StringBuilder();
    NodeList children = node.getChildNodes();
    int numNodes = children.getLength();
    for (int i = 0; i < numNodes; i++) {
        Node next = children.item(i);
        if (next instanceof Text) {
            sb.append(next.getNodeValue());
        }
    }
    return sb.toString();
}