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

ElementcreateRoot(Document document, String title)
create Root
Element node = document.createElement(title);
node.setAttribute("xmlns", "http://www.supermap.com.cn/desktop");
node.setAttribute("version", "9.0.x");
document.appendChild(node);
return node;
ElementcreateRootElement(Document doc, String name)
Create root element for specified document if it does not already exist and return it
return getRootElement(doc, true, name);
ElementcreateRootElement(Document doc, String name)
Creates a root element for the specified document.
Element result = doc.createElement(name);
doc.appendChild(result);
return result;
ElementcreateRootElement(Document doc, String rootTagName)
Create an root element with the specified name
if (doc.getDocumentElement() == null) {
    Element root = doc.createElement(rootTagName);
    doc.appendChild(root);
    return root;
return doc.getDocumentElement();
NodegetElement(String elementName, Node rootNode)
get Element
Node node = rootNode.getFirstChild();
while (node != null) {
    if (node.getNodeName().equalsIgnoreCase(elementName)) {
        return node;
    node = node.getNextSibling();
return null;
...
StringgetElementData(final Element root, final String elementName)
Gets the text value of the specified element.
NodeList nodes = root.getElementsByTagName(elementName);
if (nodes.getLength() < 1) {
    return null;
return getElementData(nodes.item(0));
StringgetElementData(final Element root, final String elementName)
Gets the text value of the specified element.
NodeList nodes = root.getElementsByTagName(elementName);
if (nodes.getLength() < 1) {
    return null;
return getElementData(nodes.item(0));
StringgetElementText(String elemName, Element root, boolean trim)
get Element Text
Element elem = getFirstElement(elemName, root);
if (elem != null) {
    return getSimpleElementText(elem, trim);
} else {
    return null;
StringgetElementValue(Element root, String name)
get Element Value
try {
    return root.getElementsByTagName(name).item(0).getTextContent();
} catch (Exception e) {
    if (DEBUG) {
        System.out.println("element: " + name);
        System.out.println(printXML(root));
    throw e;
...
longgetElementValueLong(Element root, String name)
get Element Value Long
String value = getElementValue(root, name).trim();
if (value.length() == 0 || "null".equalsIgnoreCase(value))
    return 0;
if ("unlimited".equalsIgnoreCase(value))
    return Long.MAX_VALUE;
try {
    return Long.parseLong(value);
} catch (Exception e) {
...