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 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);// w  w  w  .j  a v  a 2  s .c  om
}

From source file:Main.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");
}

From source file:Main.java

public static void setText(Element element, String Text) {
    Node node;//  w  w w  .  j a  v a  2s.c o m
    node = element.getFirstChild();
    while (null != node) {
        if (Node.TEXT_NODE == node.getNodeType()) {
            Text text = (Text) node;
            text.setData(Text);
            return;
        }
    }
    Text text = element.getOwnerDocument().createTextNode(Text);
    element.appendChild(text);
}

From source file:Main.java

private static String getStringValue(Element element, String tagName) {
    String textValue = null;/*w ww.  j a v a  2  s.  c o  m*/
    NodeList nl = element.getElementsByTagName(tagName);
    if (nl != null && nl.getLength() > 0) {
        Element el = (Element) nl.item(0);
        textValue = el.getFirstChild().getNodeValue();
    }
    return textValue;
}

From source file:Main.java

public static String getElementValue(Element p_element) {
    Node n = p_element.getFirstChild();
    if (n != null) {
        return n.getNodeValue();
    }//from   w ww . ja  v a  2  s  . co  m
    return null;
}

From source file:Main.java

/**
 * Get the specified element's text value.
 * @param e The element.//from w  ww .j  a  va  2 s.co  m
 * @return The text value of the specified element.
 */
public static String getElementValue(final Element e) {
    for (Node child = e.getFirstChild(); child != null; child = child.getNextSibling()) {
        if (!(child instanceof Text)) {
            continue;
        }
        return child.getNodeValue();
    }
    return null;
}

From source file:Main.java

/**
 * Get first Text node of the Element as String value
 * @param elem//from  ww  w  .ja  v a2 s. c  o m
 * @return
 */
public static String getFirstTextValue(Element elem) {
    if (elem != null) {
        Node fc = elem.getFirstChild();
        if (null != fc && fc.getNodeType() == Node.TEXT_NODE) {
            return ((Text) fc).getData();
        }
    }
    return null;
}

From source file:Main.java

/**
 * adds a child at the first place /*ww w . j  a va 2 s.  c o  m*/
 * @param parent
 * @param child
 */
public static void prependChild(Element parent, Element child) {
    Node first = parent.getFirstChild();
    if (first == null)
        parent.appendChild(child);
    else {
        parent.insertBefore(child, first);
    }
}

From source file:Main.java

/**
 * Returns the first direct child of the given element whose name matches {@code tag}.
 * <p>/*from  w w  w .j a va 2 s .c om*/
 * Example: let the {@link Element} {@code root} correspond to:
 *
 * <pre>
 * {@code <root>
 *     <name>
 *         <first>John</first>
 *         <last>Smith</last>
 *     </name>
 *     <last>BlahBlah</last>
 * </root>
 * 
 * getFirstDirectChild(root, "last"); // returns the element corresponding to <last>BlahBlah</last>}
 * </pre>
 *
 * @param parent
 *            The {@link Element} to get the child from.
 * @param tag
 *            The element name of the child to look for.
 * @return The {@link Element} corresponding to the child if any was found, {@code null}
 *         otherwise.
 */
public static Element getFirstDirectChild(Element parent, String tag) {
    for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) {
        if (child instanceof Element && tag.equals(child.getNodeName())) {
            return (Element) child;
        }
    }
    return null;
}

From source file:Main.java

public static void dupAttributes(Document doc) {
    Element root = doc.getDocumentElement();
    Element personOne = (Element) root.getFirstChild();

    Attr deptAttr = personOne.getAttributeNode("dept");
    personOne.removeAttributeNode(deptAttr);
    String deptString = deptAttr.getValue();
    personOne.setAttribute("dept", deptString + "updated");

    String mailString = personOne.getAttribute("mail");
    personOne.setAttribute("mail", mailString + "updated");

    String titleString = personOne.getAttribute("title");
    //personOne.removeAttribute("title");
    personOne.setAttribute("title", titleString + "updated");
}