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

/**
 * Gets data from an xml element node/* ww w.ja v a2 s.c o m*/
 * @param e
 * @param returnOnNull
 * @return
 */
public static String getDataFromElement(Element e, String... returnOnNull) {
    assert returnOnNull.length <= 1;
    String nullValue = returnOnNull.length > 0 ? returnOnNull[0] : "?";

    Node child = e.getFirstChild();
    if (child instanceof CharacterData) {
        CharacterData cd = (CharacterData) child;
        return cd.getData();
    }
    return nullValue;
}

From source file:DOMEdit.java

public static void modifyingTextbyCuttingandPasting(Document doc) {
        Element root = doc.getDocumentElement();
        Element place = (Element) root.getFirstChild();
        Text name = (Text) place.getFirstChild().getFirstChild();
        Text directions = (Text) place.getLastChild().getFirstChild();

        //    name.deleteData(offset,count);
        name.deleteData(2, 3);/*from   w  w w  .  ja va2  s.  com*/

        //String bridge = directions.substringData(offset,count);
        String bridge = directions.substringData(2, 3);
        name.appendData(bridge);
    }

From source file:Main.java

public static List<Element> getNamedChildren(String namespaceURI, Element parent, String... childElementNames) {
    List<Element> res = new ArrayList<Element>();
    if (null != parent) {
        for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) {
            if (child instanceof Element) {
                for (String name : childElementNames) {
                    if ((namespaceURI != null && name.equals(child.getLocalName())
                            && namespaceURI.equals(child.getNamespaceURI()))
                            || (namespaceURI == null && name.equals(child.getNodeName()))) {
                        res.add((Element) child);
                        break;
                    }/*from  w  ww  .  j a v a  2s. c om*/
                }
            }
        }
    }
    return res;
}

From source file:DOMEdit.java

public static void insert(Document doc, String name, String phone, String email) {
    Element personNode = doc.createElement("person");

    Element nameNode = doc.createElement("name");
    personNode.appendChild(nameNode);//  w  w w.  j a v a 2  s .  com
    Text nametextNode = doc.createTextNode(name);
    nameNode.appendChild(nametextNode);

    Element phoneNode = doc.createElement("phone");
    personNode.appendChild(phoneNode);
    Text phonetextNode = doc.createTextNode(phone);
    phoneNode.appendChild(phonetextNode);

    Element emailNode = doc.createElement("email");
    personNode.appendChild(emailNode);
    Text emailtextNode = doc.createTextNode(email);
    emailNode.appendChild(emailtextNode);

    Element root = doc.getDocumentElement();
    Node firstChildNode = root.getFirstChild();
    root.insertBefore(personNode, firstChildNode);
}

From source file:DOMEdit.java

public static void importName(Document doc1, Document doc2) {
        Element root1 = doc1.getDocumentElement();
        Element personInDoc1 = (Element) root1.getFirstChild();

        Node importedPerson = doc2.importNode(personInDoc1, true);

        Element root2 = doc2.getDocumentElement();
        root2.appendChild(importedPerson);
    }/*from  ww  w. j a  v  a2  s .c om*/

From source file:DOMEdit.java

public static void editTextbyInsertionandReplacement(Document doc) {
        Element root = doc.getDocumentElement();
        Element place = (Element) root.getFirstChild();
        Text name = (Text) place.getFirstChild().getFirstChild();
        Text directions = (Text) place.getLastChild().getFirstChild();

        // Inserting the word "black"
        //    name.insertData(offset," black");
        name.insertData(3, " black");

        // Replace "left" with "right"
        //directions.replaceData(offset,count,"right");
        directions.replaceData(3, 3, "right");
    }/*  ww w.j  ava2 s  .c o m*/

From source file:Main.java

/**
 * Get all the direct children elements of an element that have a specific
 * tag name.//  w  ww .j  a v a 2 s . com
 *
 * @param parent
 *            The parent element.
 * @param name
 *            The tag name to match.
 *
 * @return A list of Element's.
 */
public static List<Element> getElements(final Element parent, final String name) {
    final LinkedList<Element> list = new LinkedList<Element>();

    Node node = parent.getFirstChild();

    while (node != null) {
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            final Element element = (Element) node;

            if (element.getTagName().equals(name)) {
                list.add(element);
            }
        }

        node = node.getNextSibling();
    }

    return list;
}

From source file:Main.java

private static String getText(Element elm) {
    Node node = elm.getFirstChild();
    if (!(node instanceof Text)) {
        return null;
    }/* w ww .j  a v  a  2  s .  c o m*/
    Text text = (Text) node;
    return text.getData().trim();
}

From source file:Main.java

public static List<Element> elements(Element root, String tagName) {
    List<Element> L = new ArrayList<Element>();
    if (root == null)
        return L;
    for (Node n1 = root.getFirstChild(); n1 != null; n1 = n1.getNextSibling()) {
        if (n1.getNodeType() != Node.ELEMENT_NODE)
            continue;
        Element e2 = Element.class.cast(n1);
        if (e2.getTagName().equals(tagName)) {
            L.add(e2);//  w  w  w . j  av  a  2s  .c  o  m
        }
    }
    return L;
}

From source file:Main.java

private static String getTextContent(Element element) {
    if (element == null) {
        return null;
    }//  w ww .  j a v a2  s  .com
    Node textNode = element.getFirstChild();
    if (textNode == null) {
        return "";
    }
    if (textNode.getNodeType() != Node.TEXT_NODE) {
        throw new RuntimeException("Element " + element.getTagName() + "does not have text content");
    }
    return textNode.getNodeValue();
}