Java Utililty Methods XML Element Root

List of utility methods to do XML Element Root

Description

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

Method

ElementgetRootElement(Document document)
Get root element of XML file
return document.getDocumentElement();
ElementgetRootElement(Document parent)
Gets the root element of a document.
return getFirstChildElement((Node) parent);
ElementgetRootElement(final Element descendant)
Walks the parent chain from the specified element to find the root node.
Element element = descendant;
while (element.getParentNode() != null && element.getParentNode() instanceof Element) {
    element = (Element) element.getParentNode();
return element;
ElementgetRootElement(final File definition)
Return the root element of the supplied file.
if (!definition.exists()) {
    throw new FileNotFoundException(definition.toString());
if (!definition.isFile()) {
    final String error = "Source is not a file: " + definition;
    throw new IllegalArgumentException(error);
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
...
ElementgetRootElement(final String respuestaPost)
Obtiene el nodo raiz del documento xml
return DocumentBuilderFactory.newInstance().newDocumentBuilder()
        .parse(new ByteArrayInputStream(respuestaPost.getBytes())).getDocumentElement();
StartElementgetRootElement(final XMLEventReader bufferedXmlEventReader)
get Root Element
XMLEvent rootElement;
try {
    rootElement = bufferedXmlEventReader.nextEvent();
    while (rootElement.getEventType() != XMLEvent.START_ELEMENT && bufferedXmlEventReader.hasNext()) {
        rootElement = bufferedXmlEventReader.nextEvent();
} catch (XMLStreamException e) {
    throw new RuntimeException("Failed to read root element from XML", e);
...
ElementgetRootElement(String elementName, String namespace, String prefix)
Returns a namespaced root element of a document Useful to create a namespace holder element
try {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    DOMImplementation impl = builder.getDOMImplementation();
    Document namespaceHolder = impl.createDocument(namespace,
            (prefix == null ? "" : prefix + ":") + elementName, null); 
    Element rootNS = namespaceHolder.getDocumentElement();
...
ElementgetRootElement(String location)
return the root Element of an XML file with given location
Element root = null;
try {
    FileInputStream fi = new FileInputStream(location);
    DocumentBuilderFactory builderFac = DocumentBuilderFactory.newInstance();
    builderFac.setNamespaceAware(true);
    root = builderFac.newDocumentBuilder().parse(fi).getDocumentElement();
} catch (SAXException ex) {
    notify(location, ex);
...
ElementgetRootElementFromString(String payload)
get Root Element From String
return getDocument(payload).getDocumentElement();
StringgetRootElementName(Class clazz)
get Root Element Name
XmlRootElement element = (XmlRootElement) clazz.getAnnotation(XmlRootElement.class);
return !DEFAULT_NAME.equals(element.name()) ? element.name() : clazz.getSimpleName();