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

/**
 * Traverses the passed Element <var>e</var> and sets the "id" attribute to
 * be the user-defined ID attribute. This method recursively visits all
 * children of the parent Element <var>e</var>. This is used to find
 * elements by their ID//ww  w .j  a  v a2  s  . c o  m
 * 
 * @param e
 */
private static void defineIDAttribute(Element e) {
    if (e.getAttribute("id").length() > 0)
        e.setIdAttribute("id", true);
    for (Node child = e.getFirstChild(); child != null; child = child.getNextSibling())
        if (child.getNodeType() == Document.ELEMENT_NODE)
            defineIDAttribute((Element) child);
}

From source file:Main.java

public static String getContentText(Element element) {
    StringBuilder text = new StringBuilder();
    for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) {
        if (child instanceof CharacterData) {
            CharacterData characterData = (CharacterData) child;
            text.append(characterData.getData());
        }/*from  w  w  w.  j  a v a  2s . c  o  m*/
    }
    return text.toString();
}

From source file:Main.java

/**
 * Removes all child nodes from the given element
 *//*from   w  ww  .j a  v a  2 s.  c o  m*/
public static void clear(final Element element) {
    if (element == null) {
        return;
    }
    Node node;
    while ((node = element.getFirstChild()) != null) {
        element.removeChild(node);
    }
}

From source file:Main.java

public static Element getFirstChildElement(Element parent) {
    if (parent == null) {
        return null;
    }//from   w w w. j  a  v  a 2s.  c  o m

    for (Node n = parent.getFirstChild(); n != null; n = n.getNextSibling()) {
        if (n.getNodeType() == Node.ELEMENT_NODE) {
            return (Element) n;
        }
    }

    return null;
}

From source file:Main.java

public static String getCurrentLevelTextValue(Element ele, String tagName) {

    String textVal = "";

    List<Element> nl = getCurrentLevelChildNodes(ele, tagName);

    if (nl != null && nl.size() == 1) {
        Element el = (Element) nl.get(0);
        if (el.getFirstChild() != null) {
            textVal = el.getFirstChild().getNodeValue();
        }//from w  ww  .  j  a  v  a 2 s.c  om
    }

    return textVal;
}

From source file:ListMoviesXML.java

private static Movie getMovie(Element e) {
    String yearString = e.getAttribute("year");
    int year = Integer.parseInt(yearString);

    Element tElement = (Element) e.getFirstChild();
    String title = getTextValue(tElement).trim();

    Element pElement = (Element) tElement.getNextSibling();
    String pString = getTextValue(pElement).trim();
    double price = Double.parseDouble(pString);

    return new Movie(title, year, price);
}

From source file:Main.java

public static Element prependChildElement(Element parent, Element child, boolean addWhitespace, Document doc) {

    Node firstChild = parent.getFirstChild();
    if (firstChild == null) {
        parent.appendChild(child);/*from   w  w w.j a v  a 2  s .com*/
    } else {
        parent.insertBefore(child, firstChild);
    }

    if (addWhitespace) {
        Node whitespaceText = doc.createTextNode("\n");
        parent.insertBefore(whitespaceText, child);
    }
    return child;
}

From source file:Main.java

public static List<Element> getNamedChildElements(Element parent, String name) {
    List<Element> elements = new ArrayList<Element>();
    if (parent != null) {
        Node child = parent.getFirstChild();
        while (child != null) {
            if ((child.getNodeType() == Node.ELEMENT_NODE) && (child.getNodeName().equals(name))) {
                elements.add((Element) child);
            }//w  w  w  .j  ava 2 s .  c o  m
            child = child.getNextSibling();
        }
    }
    return elements;
}

From source file:Main.java

public static Collection<Element> getChildElementList(Element parent, String[] nodeNameList) {
    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) {
            for (int i = 0; i < nodeNameList.length; i++) {
                if (node.getNodeName().equals(nodeNameList[i])) {
                    list.add((Element) node);
                }//from ww  w.  j  av  a 2  s  .  c o  m
            }
        }
    }
    return list;
}

From source file:Main.java

private static Properties importProperties(NodeList nodeList) {
    Properties properties = new Properties();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Element entry = (Element) nodeList.item(i);
        if (entry.hasAttribute("name")) {
            String val = (entry == null) ? "" : entry.getFirstChild().getNodeValue();
            String key = entry.getAttribute("name");
            properties.setProperty(key, val);
        }//from ww w.  ja va 2 s  .c o m
    }
    return properties;
}