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

/**
 * Return the text (node value) of the first node under this, works best if
 * normalized./*from w w w . j a v  a2 s.c om*/
 */
public static String elementValue(Element element) {
    if (element == null)
        return null;
    // make sure we get all the text there...
    element.normalize();
    Node textNode = element.getFirstChild();

    if (textNode == null)
        return null;
    // should be of type text
    return textNode.getNodeValue();
}

From source file:Main.java

public static List<Element> elements(Element root, String namepaceUri, String localName) {
    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 (namepaceUri.equals(e2.getNamespaceURI()) && e2.getLocalName().equals(localName)) {
            L.add(e2);//from ww  w .  j a va 2s .  c o m
        }
    }
    return L;
}

From source file:DOMEdit.java

private static void makeNamelist(Document doc) {
        String names = null;/*from w ww .ja  va2  s .c  om*/
        Element root = doc.getDocumentElement();
        NodeList nameElements = root.getElementsByTagName("name");
        for (int i = 0; i < nameElements.getLength(); i++) {
            Element name = (Element) nameElements.item(i);
            Text nametext = (Text) name.getFirstChild();
            if (names == null) {
                names = nametext.getData();
            } else {
                names += ", " + nametext.getData();
            }
        }
        Element namelist = doc.createElement("names");
        Text namelisttext = doc.createTextNode(names);
        namelist.appendChild(namelisttext);
        root.insertBefore(namelist, root.getFirstChild());
    }

From source file:Main.java

/**
 * Return a List of Element objects that have the given name and are immediate children of the
 * given element; if name is null, all child elements will be included.
 *//*from w  w  w  . j a  v a2 s  .  co  m*/
public static List<Element> childElementList(Element element, String childElementName) {
    if (element == null)
        return null;

    List<Element> elements = new LinkedList<Element>();
    Node node = element.getFirstChild();

    if (node != null) {
        do {
            if (node.getNodeType() == Node.ELEMENT_NODE
                    && (childElementName == null || childElementName.equals(node.getNodeName()))) {
                Element childElement = (Element) node;

                elements.add(childElement);
            }
        } while ((node = node.getNextSibling()) != null);
    }
    return elements;
}

From source file:Main.java

/**
 * Locates the element defined by the XPath expression in the XML file and replaces the child text with the passed value.
 * @param fileName The XML file to update.
 * @param xPathExpression An XPath expression that locates the element to update.
 * @param value The value to update the attribute to.
 */// w  ww . jav  a  2  s. c  o  m
public static void updateElementValueInXMLFile(String fileName, String xPathExpression, String value) {
    try {
        Document document = parseXML(new File(fileName));
        XPath xpath = XPathFactory.newInstance().newXPath();
        XPathExpression xpathExpression = xpath.compile(xPathExpression);
        Element element = (Element) xpathExpression.evaluate(document, NODE);
        element.getFirstChild().setNodeValue(value);
        writeElement(document.getDocumentElement(), fileName);
    } catch (Exception e) {
        throw new RuntimeException("Failed to extract element from:" + fileName, e);
    }
}

From source file:Main.java

public static void findByID(Document doc, String idName) {
        Element name = doc.getElementById(idName);
        if (name == null) {
            System.out.println("There is no element with the ID " + idName);
        } else {/*from  www .ja v a 2 s .  c  o  m*/
            Text text = (Text) name.getFirstChild();
            System.out.println("The ID " + idName + " locates the name " + text.getData());
        }
    }

From source file:Main.java

/**
 * Returns the string value specified with the given tag name in the specified element. Assumes
 * that there is only one tag of the speicified name in the element.
 * //from   w w  w .  j a  va  2s . c  o m
 * @param element
 * @param tagName
 * @return the string value specified with the given tag name in the specified element
 */
public static String getSingleTagValue(Element element, String tagName) {
    NodeList nodes = element.getElementsByTagName(tagName);
    if (!hasUniqueNode(nodes)) {
        return null;
    }

    Element e = (Element) nodes.item(0);
    if (e == null) {
        return null;
    }

    return e.getFirstChild().getNodeValue();
}

From source file:Main.java

public static String getElementText(Element ele) {

    // is there anything to do?
    if (ele == null) {
        return null;
    }//from  ww w  .j a v  a2s.  com
    // get children text
    Node child = ele.getFirstChild();
    if (child != null) {
        short type = child.getNodeType();
        if (type == Node.TEXT_NODE) {
            return child.getNodeValue();
        }
    }
    // return text value
    return null;
}

From source file:Main.java

public static synchronized Element getChildElement(Element element, String childName) {
    if (element == null || childName == null || childName.length() == 0) {
        return null;
    }/*from www .  j a v  a 2 s .c om*/
    Element childs = null;
    for (Node node = element.getFirstChild(); node != null; node = node.getNextSibling()) {
        if (node instanceof Element) {
            if (node.getNodeName().equals(childName)) {
                childs = (Element) node;
                break;
            }
        }
    }
    return childs;
}

From source file:Main.java

/**
 * Returns the string value specified with the given tag name in the specified XML text. Assumes
 * that there is only one tag of the speicified name in the XML text.
 * /*from w w w  .j  av  a  2s.com*/
 * @param xmlText
 * @param tagName
 * @return the string value specified with the given tag name in the specified XML text
 */
public static String getSingleTagValue(String xmlText, String tagName) {
    Element e = getSingleElement(xmlText, tagName);
    if (e == null) {
        return null;
    }

    return e.getFirstChild().getNodeValue();
}