Example usage for org.w3c.dom Element getFirstChild

List of usage examples for org.w3c.dom Element getFirstChild

Introduction

In this page you can find the example usage for org.w3c.dom Element getFirstChild.

Prototype

public Node getFirstChild();

Source Link

Document

The first child of this node.

Usage

From source file:no.kantega.commons.util.XMLHelper.java

public static Element setChildText(Document doc, Element parent, String name, String value) {
    Element child = getChildByName(parent, name);
    if (child == null) {
        child = doc.createElement(name);
        child.appendChild(doc.createTextNode(value == null ? "" : value));
        parent.appendChild(child);//from ww  w  . j  a va  2  s  .co m
    } else {
        Node text = child.getFirstChild();
        if (text != null) {
            text.setNodeValue(value);
        } else {
            child.appendChild(doc.createTextNode(value == null ? "" : value));
        }
    }
    return child;
}

From source file:com.github.fcannizzaro.resourcer.Resourcer.java

/**
 * Parse values xml files/* w  ww.jav  a  2  s. com*/
 */
private static void findValues() {

    values = new HashMap<>();

    File dir = new File(PATH_BASE + VALUES_DIR);

    if (!dir.isDirectory())
        return;

    File[] files = dir.listFiles();

    if (files == null)
        return;

    for (File file : files)

        // only *.xml files
        if (file.getName().matches(".*\\.xml$")) {

            Document doc = FileUtils.readXML(file.getAbsolutePath());

            if (doc == null)
                return;

            Element ele = doc.getDocumentElement();

            for (String element : elements) {

                NodeList list = ele.getElementsByTagName(element);

                if (values.get(element) == null)
                    values.put(element, new HashMap<>());

                for (int j = 0; j < list.getLength(); j++) {

                    Element node = (Element) list.item(j);
                    String value = node.getFirstChild().getNodeValue();
                    Object valueDefined = value;

                    switch (element) {
                    case INTEGER:
                        valueDefined = Integer.valueOf(value);
                        break;
                    case DOUBLE:
                        valueDefined = Double.valueOf(value);
                        break;
                    case FLOAT:
                        valueDefined = Float.valueOf(value);
                        break;
                    case BOOLEAN:
                        valueDefined = Boolean.valueOf(value);
                        break;
                    case LONG:
                        valueDefined = Long.valueOf(value);
                        break;
                    case COLOR:

                        if (value.matches("@color/.*")) {

                            try {
                                Class<?> c = Class.forName("com.github.fcannizzaro.material.Colors");
                                Object colors = c.getDeclaredField(value.replace("@color/", "")).get(c);
                                Method asColor = c.getMethod("asColor");
                                valueDefined = asColor.invoke(colors);

                            } catch (Exception e) {
                                System.out.println("ERROR Resourcer - Cannot bind " + value);
                            }

                        } else
                            valueDefined = Color.decode(value);
                        break;
                    }

                    values.get(node.getNodeName()).put(node.getAttribute("name"), valueDefined);

                }

            }
        }
}

From source file:Main.java

/**
 * Rename an element, replacing it in its document.
 * @param element the element to rename.
 * @param name the new element name./* w  w w .ja v a  2  s.  com*/
 * @return the renamed element.
 */
public static Element renameElement(Element element, String name) {
    if (element.getNodeName().equals(name))
        return element;
    Element el = element.getOwnerDocument().createElement(name);

    //Copy the attributes
    NamedNodeMap attributes = element.getAttributes();
    int nAttrs = attributes.getLength();
    for (int i = 0; i < nAttrs; i++) {
        Node attr = attributes.item(i);
        el.setAttribute(attr.getNodeName(), attr.getNodeValue());
    }

    //Copy the children
    Node node = element.getFirstChild();
    while (node != null) {
        Node clone = node.cloneNode(true);
        el.appendChild(clone);
        node = node.getNextSibling();
    }

    //Replace the element
    element.getParentNode().replaceChild(el, element);
    return el;
}

From source file:de.fraunhofer.iosb.ivct.IVCTcommander.java

private static ConfigParameters parseConfig(Document dom) {
    ConfigParameters configParameters = new ConfigParameters();

    Element elem = dom.getDocumentElement();
    for (Node child = elem.getFirstChild(); child != null; child = child.getNextSibling()) {
        String s = child.getNodeName();
        if (s.compareTo("pathNames") == 0) {
            for (Node child0 = child.getFirstChild(); child0 != null; child0 = child0.getNextSibling()) {
                if (child0.getNodeName().compareTo("sutDir") == 0) {
                    if (child0.getNodeType() == Node.ELEMENT_NODE) {
                        configParameters.pathSutDir = child0.getFirstChild().getNodeValue();
                    }/*from  w  w w  .  j  a  v  a  2s. co  m*/
                }
                if (child0.getNodeName().compareTo("testSuites") == 0) {
                    if (child0.getNodeType() == Node.ELEMENT_NODE) {
                        configParameters.pathTestsuite = child0.getFirstChild().getNodeValue();
                    }
                }
            }
        }
    }

    return configParameters;
}

From source file:Main.java

/**
 * Changes the tag name of an element./*from w w  w.j a  va2  s  . co m*/
 * 
 * @param elem Element which name should be changed
 * @param newName new tag name of the element
 * @return true if the name was changed successfully or false otherwise
 */
static public boolean changeTagName(Element elem, String newName) {
    if (elem == null)
        return false; // not Found!
    Document doc = elem.getOwnerDocument();
    Element newElem = doc.createElement(newName);

    // Copy the attributes to the new element
    NamedNodeMap attrs = elem.getAttributes();
    for (int i = 0; i < attrs.getLength(); i++) {
        Attr attr2 = (Attr) doc.importNode(attrs.item(i), true);
        newElem.getAttributes().setNamedItem(attr2);
    }

    // Copy all Child Elements
    for (Node node = elem.getFirstChild(); node != null; node = node.getNextSibling())
        newElem.appendChild(node.cloneNode(true));
    // insert
    Node parent = elem.getParentNode();
    parent.replaceChild(newElem, elem);
    return true;
}

From source file:org.unitedinternet.cosmo.dav.caldav.report.MultigetReport.java

private static String updateHrefElementWithRequestUrlUUID(Element hrefElement, String davHref,
        String resourceUUID) {/*from w w w  .  j a  va2  s.c  o  m*/
    if (StringUtils.isNotEmpty(resourceUUID)) {
        Matcher davEmailMatcher = HREF_EMAIL_PATTERN.matcher(davHref);
        if (davEmailMatcher.find()) {
            String email = davEmailMatcher.group(0);
            String newHref = davHref.replaceFirst(email.replaceAll("/", ""), resourceUUID.replaceAll("/", ""));
            hrefElement.getFirstChild().setNodeValue(newHref);
            return newHref;
        }
    }
    return davHref;
}

From source file:XMLUtils.java

/**
 * Sets the text value for a given element.
 * @param el/*from   w  ww. j a v a 2s .c  o  m*/
 * @param value
 */
public static void setText(Element el, String value) {
    // remove the children if already exist
    while (el.getFirstChild() != null) {
        el.removeChild(el.getFirstChild());
    }
    if (value == null) {
        value = "";
    }
    Text txt = el.getOwnerDocument().createTextNode(value);
    el.appendChild(txt);
}

From source file:XMLUtils.java

/**
 * Sets the text value for a given element as a CDATA section
 * @param el/*from www  .  ja v a 2  s  .  c o  m*/
 * @param value
 */
public static void setCDATA(Element el, String value) {
    // remove the children if already exist
    while (el.getFirstChild() != null) {
        el.removeChild(el.getFirstChild());
    }
    if (value == null) {
        value = "";
    }
    CDATASection txt = el.getOwnerDocument().createCDATASection(value);
    el.appendChild(txt);
}

From source file:be.fedict.eid.dss.ws.DSSUtil.java

public static Element getStorageInfoElement(StorageInfo storageInfo) {

    Document newDocument = documentBuilder.newDocument();
    Element newElement = newDocument.createElement("newNode");
    try {/*from  w w  w  .j a  v a2s  . c o  m*/
        artifactMarshaller.marshal(artifactObjectFactory.createStorageInfo(storageInfo), newElement);
    } catch (JAXBException e) {
        throw new RuntimeException("JAXB error: " + e.getMessage(), e);
    }
    return (Element) newElement.getFirstChild();
}

From source file:Main.java

/**
 * Rename an element in a DOM document. It happens to involves a node
 * replication./*w w w.  j  a v a  2  s  .  c  o m*/
 * 
 * @param document
 *            The document containing the element (some way to verify
 *            that?).
 */
public static Element renameElement(Document document, Element element, String newName, String namespace) {
    if (namespace == null) {
        throw new IllegalArgumentException("No namespace provided for element " + element);
    }
    Element newElement = document.createElementNS(namespace, newName);
    NamedNodeMap attributes = element.getAttributes();
    for (int i = 0, iEnd = attributes.getLength(); i < iEnd; i++) {
        Attr attr2 = (Attr) document.importNode(attributes.item(i), true);
        newElement.getAttributes().setNamedItem(attr2);
    }
    while (element.hasChildNodes()) {
        newElement.appendChild(element.getFirstChild());
    }
    element.getParentNode().replaceChild(newElement, element);
    return newElement;
}