List of utility methods to do XML Element Root
Element | getRootElement(Document document) Get root element of XML file return document.getDocumentElement();
|
Element | getRootElement(Document parent) Gets the root element of a document. return getFirstChildElement((Node) parent);
|
Element | getRootElement(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; |
Element | getRootElement(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(); ... |
Element | getRootElement(final String respuestaPost) Obtiene el nodo raiz del documento xml return DocumentBuilderFactory.newInstance().newDocumentBuilder() .parse(new ByteArrayInputStream(respuestaPost.getBytes())).getDocumentElement(); |
StartElement | getRootElement(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); ... |
Element | getRootElement(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(); ... |
Element | getRootElement(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); ... |
Element | getRootElementFromString(String payload) get Root Element From String return getDocument(payload).getDocumentElement();
|
String | getRootElementName(Class> clazz) get Root Element Name XmlRootElement element = (XmlRootElement) clazz.getAnnotation(XmlRootElement.class); return !DEFAULT_NAME.equals(element.name()) ? element.name() : clazz.getSimpleName(); |