Example usage for org.w3c.dom Node ELEMENT_NODE

List of usage examples for org.w3c.dom Node ELEMENT_NODE

Introduction

In this page you can find the example usage for org.w3c.dom Node ELEMENT_NODE.

Prototype

short ELEMENT_NODE

To view the source code for org.w3c.dom Node ELEMENT_NODE.

Click Source Link

Document

The node is an Element.

Usage

From source file:Main.java

public static Element[] getChildren(Element parent, String name) {
    ArrayList<Element> al = new ArrayList<Element>();
    NodeList nl = parent.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node n = nl.item(i);//from  ww  w.  j  a  v  a 2s.co  m
        if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(name)) {
            al.add((Element) n);
        }
    }
    return al.toArray(new Element[0]);
}

From source file:Main.java

/**
 * Get the first child element of the specified element, null if it has no child elements.
 * /*from w w w.  j  av  a  2 s  .com*/
 * @param root The element to search.
 * @return The first child element, null if it has none.
 */
public static Element getFirstChild(Element root) {
    if (root == null)
        return null;
    NodeList lst = root.getChildNodes();
    final int n = lst.getLength();
    for (int i = 0; i < n; i++) {
        Node node = lst.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE)
            return (Element) node;
    }
    return null;
}

From source file:Main.java

/**
 * Returns the children elements with the specified tagName for the
 * specified parent element./*from ww  w .ja  v a2 s. c o  m*/
 *
 * @param parent The parent whose children we're looking for.
 * @param tagName the name of the child to find
 * @return List of the children with the specified name
 * @throws NullPointerException if parent or tagName are null
 */
public static List<Element> findChildren(Element parent, String tagName) {
    if (parent == null || tagName == null)
        throw new NullPointerException(
                "Parent or tagname were null! " + "parent = " + parent + "; tagName = " + tagName);

    List<Element> result = new ArrayList<Element>();
    NodeList nodes = parent.getChildNodes();
    Node node;
    int len = nodes.getLength();
    for (int i = 0; i < len; i++) {
        node = nodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) node;
            if (element.getNodeName().equals(tagName))
                result.add(element);
        }
    }

    return result;
}

From source file:Main.java

/**
 * Recursively strips the namespace information from a node.
 * @param node the starting node.//from w w w  .j a va  2  s  .  c o  m
 * @param document host document
 * @return clean node
 */
public static Node removeNamespaceRecursive(final Node node, final Document document) {
    Node newNode = null;

    if (node.getNodeType() == Node.ELEMENT_NODE) {
        newNode = document.renameNode(node, null, removeNsPrefix(node.getNodeName()));
    }

    final NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); ++i) {
        removeNamespaceRecursive(list.item(i), document);
    }

    return newNode;
}

From source file:Main.java

public static void testx() {
    try {//from  w ww. jav a 2s .  c o m
        File fXmlFile = new File("C:\\Users\\is96092\\Desktop\\music\\Megaman2.xml");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);

        //optional, but recommended
        //read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
        doc.getDocumentElement().normalize();

        System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

        NodeList nList = doc.getElementsByTagName("staff");

        System.out.println("----------------------------");

        for (int temp = 0; temp < nList.getLength(); temp++) {

            Node nNode = nList.item(temp);

            System.out.println("\nCurrent Element :" + nNode.getNodeName());

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

                Element eElement = (Element) nNode;

                System.out.println("Staff id : " + eElement.getAttribute("id"));
                System.out.println(
                        "First Name : " + eElement.getElementsByTagName("firstname").item(0).getTextContent());
                System.out.println(
                        "Last Name : " + eElement.getElementsByTagName("lastname").item(0).getTextContent());
                System.out.println(
                        "Nick Name : " + eElement.getElementsByTagName("nickname").item(0).getTextContent());
                System.out.println(
                        "Salary : " + eElement.getElementsByTagName("salary").item(0).getTextContent());

            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

private final static String getElement(String filePath, String key) throws Exception {

    String value = null;//  www.  j  a  va2 s  .  co m

    try {

        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();

        File xmlFile = new File(filePath);

        Document document = documentBuilder.parse(xmlFile);

        Element resourcesElement = document.getDocumentElement();
        resourcesElement.normalize();

        NodeList strings = resourcesElement.getChildNodes();

        int stringsCount = strings.getLength();

        for (int i = 0; i < stringsCount; i++) {

            Node node = strings.item(i);

            short nodeType = node.getNodeType();

            if (nodeType == Node.ELEMENT_NODE) {

                Element element = (Element) node;

                String attribute = element.getAttribute("name");

                if (attribute.equals(key)) {

                    value = element.getTextContent();
                }
            }
        }

    } catch (Exception ex) {

        throw ex;
    }

    return value;
}

From source file:Main.java

public static List<Element> elements(Element element, String tagName) {
    if (element == null || !element.hasChildNodes()) {
        return Collections.emptyList();
    }//from   w w w.  j  av  a 2 s  . co m

    List<Element> elements = new ArrayList<Element>();
    for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            Element childElement = (Element) child;
            String childTagName = childElement.getNodeName();
            if (tagName.equals(childTagName))
                elements.add(childElement);
        }
    }
    return elements;
}

From source file:Main.java

public static Element getFirstChildElementByName(Element root, String tagName) {
    NodeList nl = root.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node child = nl.item(i);/*  w  w  w.ja v a 2  s. c o m*/
        if (child.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }
        if (tagName.equals(child.getNodeName())) {
            return (Element) child;
        }
    }
    return null;
}

From source file:Main.java

public static Element getNextSiblingElement(final Node node) {
    if (node != null) {
        Node tmpResult = node.getNextSibling();

        while (tmpResult != null) {
            if (Node.ELEMENT_NODE == tmpResult.getNodeType()) {
                return (Element) tmpResult;
            }/*from  w  w  w  . j  av a 2  s .  com*/

            tmpResult = tmpResult.getNextSibling();
        }
    }

    return null;
}

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 ww w  .j av  a  2 s . c  om
            System.out.println("\tUnknown node type: '" + child.getNodeType() + "'");
            break;
        }
    }
}