Java Utililty Methods XML Element First

List of utility methods to do XML Element First

Description

The list of methods to do XML Element First are organized into topic(s).

Method

ElementgetFirstElement(Document document)
get first "real" element (ie not the document-rootelement, but the next one
Element workelement = null;
if (document.getDocumentElement() != null) {
    org.w3c.dom.Node tmp = document.getDocumentElement().getFirstChild();
    while (tmp != null) {
        tmp = tmp.getNextSibling();
        if (tmp.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
            workelement = (Element) tmp;
            break;
...
ElementgetFirstElement(Document document, String tagName)
This method returns the first element from the document.
NodeList nodes = document.getElementsByTagName(tagName);
if (nodes != null && nodes.getLength() != 0) {
    return (Element) nodes.item(0);
} else {
    return null;
ElementgetFirstElement(Element documentElement)
get First Element
Object node = null;
NodeList childs = documentElement.getChildNodes();
for (int i = 0; i < childs.getLength(); i++) {
    node = childs.item(i);
    if (node instanceof Element)
        return (Element) node;
throw new RuntimeException("Can't find any Element");
...
ElementgetFirstElement(String name, Element root)
get First Element
NodeList list = root.getElementsByTagName(name);
if (list != null) {
    return (Element) list.item(0);
} else
    return null;
OptionalgetFirstElementByTagName(Document doc, String tagName)
Returns the first element matching the given tag name.
NodeList nodes = doc.getElementsByTagName(tagName);
return nodes.getLength() > 0 ? Optional.ofNullable((Element) nodes.item(0)) : Optional.empty();
ElementgetFirstElementByTagName(Document document, String name)
get First Element By Tag Name
return getFirstElement(document.getElementsByTagName(name));
ElementgetFirstElementByTagName(Document document, String tag)
get First Element By Tag Name
NodeList nodeList = document.getElementsByTagName(tag);
if (nodeList == null) {
    p("no list of elements with tag=" + tag);
    return null;
Element element = (Element) nodeList.item(0);
if (element == null) {
    p("no element for tag=" + tag);
...
StringgetFirstElementText(Document document, String tagName)
This method returns the first element text from the document.
Element element = getFirstElement(document, tagName);
if (element != null) {
    return getText(element);
} else {
    return null;
ElementgetFirstImageTransformElement(final Document doc)
Finds the first Transform Element with an Image child node.
final NodeList transforms = doc.getElementsByTagName("Transform");
for (int i = 0; i < transforms.getLength(); ++i) {
    final Element transform = (Element) transforms.item(i);
    final NodeList children = transform.getChildNodes();
    for (int j = 0; j < children.getLength(); ++j) {
        if (children.item(j).getNodeName().equals("Image")) {
            return transform;
return null;
NodegetFirstNode(Document doc, String xPathExpression)
Get first node on a Document doc, give a xpath Expression
try {
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();
    XPathExpression expr;
    expr = xpath.compile(xPathExpression);
    NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
    return nl.item(0);
} catch (XPathExpressionException e) {
...