Java Utililty Methods XML Element First

List of utility methods to do XML Element First

  1. HOME
  2. Java
  3. X
  4. XML Element First

Description

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

Method

Element getFirstElement(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;
...
Element getFirstElement(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;
Element getFirstElement(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");
...
Element getFirstElement(String name, Element root)
get First Element
NodeList list = root.getElementsByTagName(name);
if (list != null) {
 return (Element) list.item(0);
} else
 return null;
Optional getFirstElementByTagName(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();
Element getFirstElementByTagName(Document document, String name)
get First Element By Tag Name
return getFirstElement(document.getElementsByTagName(name));
Element getFirstElementByTagName(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);
...
String getFirstElementText(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;
Element getFirstImageTransformElement(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;
Node getFirstNode(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) {
...


AltStyle によって変換されたページ (->オリジナル) /