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

public static void delAttribute(Document doc) {
    Element root = doc.getDocumentElement();
    Element person = (Element) root.getFirstChild();
    person.removeAttribute("number");
    person.removeAttribute("dept");
}

From source file:Main.java

private static String getTextValue(Element ele, String tagName) {
    String textVal = null;/* w ww .  j  a  v a2s  .co m*/
    NodeList nl = ele.getElementsByTagName(tagName);
    if (nl != null && nl.getLength() > 0) {
        Element el = (Element) nl.item(0);
        textVal = el.getFirstChild().getNodeValue();
    }

    return textVal;
}

From source file:Main.java

public static void addCDATA(Document doc) {
    Element root = doc.getDocumentElement();
    Element place = (Element) root.getFirstChild();
    Element directions = (Element) place.getLastChild();
    String dirtext = "cdData.";
    CDATASection dirdata = doc.createCDATASection(dirtext);
    directions.replaceChild(dirdata, directions.getFirstChild());
}

From source file:Main.java

public static String getCharacterDataFromElement(Element e) {
    org.w3c.dom.Node child = e.getFirstChild();
    if (child instanceof CharacterData) {
        CharacterData cd = (CharacterData) child;
        return cd.getData();
    }//from w  w  w.  j  a  va  2s  .c om
    return "?";
}

From source file:Main.java

/**
 * Extracts the text from an <code>Element</code>.
 *///from   w  w  w.j a  v  a 2 s.c o  m
protected static String extractTextFrom(Element element) {
    Text text = (Text) element.getFirstChild();
    return (text == null ? "" : text.getData());
}

From source file:Main.java

public static void removeChilds(Element e) {
    while (e.hasChildNodes())
        e.removeChild(e.getFirstChild());
}

From source file:Main.java

public static void duplicatePerson(Document doc) {
    Element root = doc.getDocumentElement();
    Element origPerson = (Element) root.getFirstChild();
    Element newPerson = (Element) origPerson.cloneNode(true);
    root.appendChild(newPerson);// w w w.ja  v a2 s  .  co m
}

From source file:Main.java

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

    name.setData("newName");
    directions.setData("newDirection");
}

From source file:Main.java

public static boolean isTextOnly(Element element) {
    for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) {
        if (child.getNodeType() == Node.ELEMENT_NODE)
            return false;
    }//from ww w  .  j a  va2s  .co  m
    return true;
}

From source file:Main.java

public static Element getElement2(Element parentElement, String nodeName) {
    Node node = parentElement.getFirstChild();
    while (null != node) {
        if ((Element.ELEMENT_NODE == node.getNodeType()) && (nodeName.equals(node.getNodeName()))) {
            return (Element) node;
        }//  w w w.jav a2s.c  o m
        node = node.getNextSibling();
    }
    return null;
}