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

public static Map<String, String> getTagValueMap(Element ele, String language) {
    Map<String, String> map = new LinkedHashMap<String, String>();
    NodeList children = ele.getChildNodes();
    Node current = null;//from w w  w .j  ava  2 s .c  om
    int count = children.getLength();
    for (int i = 0; i < count; i++) {
        current = children.item(i);
        if (current.getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) current;
            map.put(current.getNodeName(), element.getFirstChild().getNodeValue());
        }
    }
    return map;
}

From source file:Main.java

/**
 * Given an XML element, this method returns nested elements which are direct
 * children of the given element - but only those that have the element-name "childrenName" (the second parameter).
 * @param element//from ww  w.ja va2s  .c om
 * @param childrenName
 * @return
 */
public static List<Element> getChildElements(Element element, String childrenName) {
    List<Element> ret = new LinkedList<Element>();
    NodeList nodeList = element.getChildNodes();
    for (int index = 0; index < nodeList.getLength(); ++index) {
        Node node = nodeList.item(index);
        if (node instanceof Element) {
            Element elementOfNode = (Element) node;
            if (elementOfNode.getTagName().equals(childrenName)) {
                ret.add(elementOfNode);
            }

        }
    }
    return ret;
}

From source file:Main.java

protected static List<Element> createElementList(Element element) {
    List<Element> elementList = new ArrayList<>();

    NodeList childNodes = element.getChildNodes();
    int numChildren = childNodes.getLength();

    for (int i = 0; i < numChildren; i++) {
        Node childNode = childNodes.item(i);
        if (childNode.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }// ww  w. j a v a2  s .  c  o m
        elementList.add((Element) childNode);
    }

    return elementList;
}

From source file:Main.java

public static List getChildElementsByTagName(Element ele, String[] childEleNames) {
    List childEleNameList = Arrays.asList(childEleNames);
    NodeList nl = ele.getChildNodes();
    List childEles = new ArrayList();
    for (int i = 0; i < nl.getLength(); i++) {
        Node node = nl.item(i);/*from   ww w  .j  a va  2 s  .co  m*/
        if (node instanceof Element && nodeNameMatch(node, childEleNameList)) {
            childEles.add(node);
        }
    }
    return childEles;
}

From source file:Main.java

/**
 * Gets the elements./*from   w  w w . j  a  v  a  2  s .  com*/
 * 
 * @param topElm
 *        the top elm
 * @return the elements
 */
public static List<Element> getElements(Element topElm) {
    List<Element> retVals = new ArrayList<Element>();
    NodeList childNodes = topElm.getChildNodes();
    for (int i = 0, ll = childNodes.getLength(); i < ll; i++) {
        Node n = childNodes.item(i);
        if (n.getNodeType() == Node.ELEMENT_NODE) {
            retVals.add((Element) n);
        }
    }
    return retVals;
}

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./*  ww w  .java2 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, String elemName) {
    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) {
            if (elemName.equals(n.getNodeName())) {
                array.add(n);
            }
        }
    }
    Element[] elems = new Element[array.size()];

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

From source file:org.shareok.data.documentProcessor.FileHandlerFactory.java

/**
 *
 * @param fileExtension//from w  w w  .ja  va  2s  .com
 * @return
 */
public static FileHandler getFileHandlerByFileExtension(String fileExtension) {

    FileHandler fh = null;
    String beanName = "";

    try {
        String fileTypePath = DocumentProcessorUtil.getFilePathFromResources("filetypes.xml");
        Document fileTypeDoc = loadXMLFromString(fileTypePath);
        fileTypeDoc.getDocumentElement().normalize();
        Element docEle = fileTypeDoc.getDocumentElement();
        NodeList nl = docEle.getChildNodes();

        if (nl != null && nl.getLength() > 0) {
            for (int i = 0; i < nl.getLength(); i++) {
                if (nl.item(i).getNodeType() == Node.ELEMENT_NODE) {
                    Element el = (Element) nl.item(i);
                    String nodeVal = el.getTextContent();
                    if (nodeVal.equals(fileExtension)) {
                        beanName = el.getAttribute("bean");
                        break;
                    }
                }
            }
        }

        ApplicationContext context = new ClassPathXmlApplicationContext("documentProcessorContext.xml");
        fh = (FileHandler) context.getBean(beanName);

    } catch (Exception ex) {
        Logger.getLogger(DocumentProcessorUtil.class.getName()).log(Level.SEVERE, null, ex);
    }

    return fh;
}

From source file:Main.java

public static Element[] filterChildElements(Element parent, String ns, String lname) {

    List<Element> elems = new ArrayList<Element>();
    NodeList nlst = parent.getChildNodes();
    for (int i = 0, nlen = nlst.getLength(); i < nlen; i++) {
        Node n = nlst.item(i);/*from   ww w  . j  a v a2 s.c  o m*/
        if (n instanceof Element) {
            Element e = (Element) n;
            if ((ns == null || ns.equals(e.getNamespaceURI()))
                    && (lname == null || lname.equals(e.getLocalName()))) {
                elems.add(e);
            }
        }
    }
    return elems.toArray(new Element[elems.size()]);
}

From source file:Main.java

static void printElement(Element element, String indent) {
    System.out.println("Element '" + element.getNodeName() + "'");
    NodeList children = element.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        switch (child.getNodeType()) {
        case Node.ELEMENT_NODE:
            printElement((Element) child, indent + "\t");
            break;
        case Node.ATTRIBUTE_NODE:
            Attr attr = (Attr) child;
            System.out.println("\tAttribute: '" + attr.getName() + "' = '" + attr.getValue() + "'");
            break;
        case Node.COMMENT_NODE:
            Comment comment = (Comment) child;
            System.out.println("\tComment: '" + comment.getData() + "'");
            break;
        case Node.CDATA_SECTION_NODE:
            CharacterData cdata = (CharacterData) child;
            System.out.println("\tCDatat: '" + cdata.getData() + "'");
            break;
        case Node.TEXT_NODE:
            Text text = (Text) child;
            System.out.println("\tText: '" + text.getData() + "'");
            break;
        default://from  w w  w  .  java 2  s. c  o  m
            System.out.println("\tUnknown node type: '" + child.getNodeType() + "'");
            break;
        }
    }
}

From source file:Main.java

public static String textInElement(Element elem) {
    StringBuilder buf = new StringBuilder(100);
    elem.normalize();/*from   w w w.  j a va  2  s  . c  o m*/
    NodeList nlst = elem.getChildNodes();
    for (int i = 0, nlen = nlst.getLength(); i < nlen; i++) {
        Node n = nlst.item(i);
        if (n instanceof Text) {
            final String data = ((Text) n).getData();
            buf.append(data);
        }
    }
    return buf.toString();
}