Example usage for org.w3c.dom Element appendChild

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

Introduction

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

Prototype

public Node appendChild(Node newChild) throws DOMException;

Source Link

Document

Adds the node newChild to the end of the list of children of this node.

Usage

From source file:Main.java

private static Element getChildElement(Document document, Element origElement, String subElementName) {
    NodeList buildNodes = origElement.getChildNodes();
    int numChildrenOfOrig = buildNodes.getLength();
    for (int indexChildOfOrig = 0; indexChildOfOrig < numChildrenOfOrig; indexChildOfOrig++) {
        Node child = buildNodes.item(indexChildOfOrig);
        if (child.getNodeName().equals(subElementName)) {
            return (Element) child;
        }/*ww  w  . j  a  va 2 s  .  co m*/
    }

    //if we are here, the child node was never found, so create it now
    Element newElem = document.createElement(subElementName);
    origElement.appendChild(newElem);
    return newElem;

}

From source file:com.aurel.track.exchange.docx.exporter.CustomXML.java

/**
 * Creates a dom element//  www .j a va 2s  .  co  m
 * @param elementName
 * @param elementValue
 * @param dom
 * @return
 */
private static Element createDomElement(String elementName, String elementValue, Document dom) {
    Element element = null;
    try {
        try {
            element = dom.createElement(elementName);
        } catch (DOMException e) {
            LOGGER.warn("Creating an XML node with the element name " + elementName
                    + " failed with dom exception " + e);
        }
        if (element == null) {
            return null;
        }
        if (elementValue == null || "".equals(elementValue.trim())) {
            element.appendChild(dom.createTextNode(""));
        } else {
            try {
                element.appendChild(dom.createTextNode(Html2Text.stripNonValidXMLCharacters(elementValue)));
            } catch (DOMException e) {
                LOGGER.info("Creating the node for text element " + elementName + " and the value "
                        + elementValue + " failed with dom exception " + e);
                element.appendChild(dom.createTextNode(""));
            }
        }
    } catch (Exception e) {
        LOGGER.warn("Creating an XML node with the element name " + elementName + " failed with " + e);
    }
    return element;
}

From source file:com.photon.phresco.impl.WindowsApplicationProcessor.java

private static void updateContent(Document doc, List<ArtifactGroup> artifactGroups, List<Node> itemGroup,
        String elementName) {//from   www . ja  v a2 s  .co m
    for (Node node : itemGroup) {
        NodeList childNodes = node.getChildNodes();
        for (int j = 0; j < childNodes.getLength(); j++) {
            Node item = childNodes.item(j);
            if (item.getNodeName().equals(elementName)) {
                Node parentNode = item.getParentNode();
                for (ArtifactGroup artifactGroup : artifactGroups) {
                    if (artifactGroup.getType().name().equals(Type.FEATURE.name())) {
                        Element content = doc.createElement(elementName);
                        if (elementName.equalsIgnoreCase(REFERENCE)) {
                            content.setAttribute(INCLUDE, artifactGroup.getName() + DLL);
                            Element hintPath = doc.createElement(HINTPATH);
                            hintPath.setTextContent(
                                    DOUBLE_DOT + COMMON + File.separator + artifactGroup.getName() + DLL);
                            content.appendChild(hintPath);
                        } else {
                            content.setAttribute(INCLUDE, artifactGroup.getName() + DLL);
                        }
                        parentNode.appendChild(content);
                    }
                }
                break;
            }
        }
    }
}

From source file:ConfigFiles.java

public static void addTag(String tagname, String tagcontent, Element root, boolean blank, Document document) {
    Element rootElement = document.createElement(tagname);
    root.appendChild(rootElement);
    String temp;/*from w w  w  .  j av a  2  s  .  c  o  m*/
    if (blank)
        temp = "";
    else
        temp = tagcontent;
    rootElement.appendChild(document.createTextNode(temp));
}

From source file:DomUtil.java

/**
 * Sets the text content of a DOM <code>Element</code>.
 * //  w ww  .j  a v a  2  s. c  om
 * @param element The <code>Element</code> to modify.
 * @param value The new text value.
 */
public static void setElementText(Element element, String value) {
    NodeList children = element.getChildNodes();
    int childCount = children.getLength();
    for (int index = 0; index < childCount; ++index) {
        if (children.item(index) instanceof Text) {
            Text text = (Text) children.item(index);
            text.setData(value);
            return;
        }
    }
    Text text = element.getOwnerDocument().createTextNode(value);
    element.appendChild(text);
}

From source file:Main.java

public static void setElementTextValue(Element e, String text, boolean cdata) {
    Document root = e.getOwnerDocument();
    e.normalize();//ww w  .  java 2 s . c  o m
    if (e.hasChildNodes()) {
        NodeList nl = e.getChildNodes();

        /* This suxx: NodeList Object is changed when removing children !!!
           I will store all nodes that should be deleted in a Vector and delete them afterwards */
        int length = nl.getLength();

        List<Node> v = new ArrayList<Node>(nl.getLength());
        for (int i = 0; i < length; i++)
            if (nl.item(i) instanceof CharacterData)
                v.add(nl.item(i));
        for (Node n : v)
            e.removeChild(n);
    }

    if (cdata) {
        e.appendChild(root.createCDATASection(text));
    } else {
        e.appendChild(root.createTextNode(text));
    }
}

From source file:XMLUtils.java

/**
 * Adds an element as a child of a given element and sets the text value.
 * The child is created with the same namespace as the parent. 
 * /*from   w ww . j  ava 2 s  .c  om*/
 * @param parent
 * @param name
 * @param textValue
 * @return
 */
public static Element addElement(Element parent, String name, String textValue) {

    Element child = addElement(parent, name);
    // create a text node
    if (textValue == null) {
        textValue = "";
    }
    Text txt = child.getOwnerDocument().createTextNode(textValue);
    child.appendChild(txt);
    return child;
}

From source file:com.amalto.core.storage.hibernate.DefaultStorageClassLoader.java

private static void addEvent(Document document, Node sessionFactoryElement, String eventType,
        String listenerClass) {/*from w  ww . j  av  a  2 s.  com*/
    Element event = document.createElement("event"); //$NON-NLS-1$
    Attr type = document.createAttribute("type"); //$NON-NLS-1$
    type.setValue(eventType);
    event.getAttributes().setNamedItem(type);
    {
        Element listener = document.createElement("listener"); //$NON-NLS-1$
        Attr clazz = document.createAttribute("class"); //$NON-NLS-1$
        clazz.setValue(listenerClass);
        listener.getAttributes().setNamedItem(clazz);
        event.appendChild(listener);
    }
    sessionFactoryElement.appendChild(event);
}

From source file:com.ephesoft.dcma.util.XMLUtil.java

/**
 * To append Leaf Child./*from  w  w w .j a va2 s.  co m*/
 * 
 * @param doc Document
 * @param parent Node
 * @param childName String
 * @param childData String
 */
public static void appendLeafChild(Document doc, Node parent, String childName, String childData) {
    Element child = doc.createElement(childName);
    if (childData != null && childData.length() != 0) {
        Text text = doc.createTextNode(childData);

        child.appendChild(text);
    }
    parent.appendChild(child);
}

From source file:de.betterform.connector.file.FileURIResolver.java

/**
 * Returns a plain file listing as a document.
 *
 * @param directory the directory to list.
 * @return a plain file listing as a document.
 *//*w  ww . j  av  a 2s.co m*/
public static Document buildDirectoryListing(File directory) {
    Document dirList = DOMUtil.newDocument(false, false);
    Element root = dirList.createElement("dir");
    root.setAttribute("path", directory.toURI().toString());
    root.setAttribute("parentDir", directory.getParentFile().toURI().toString());

    File[] fileList = directory.listFiles();
    File file;
    Element element;
    for (int i = 0; i < fileList.length; i++) {
        file = fileList[i];

        if (file.isDirectory()) {
            element = dirList.createElement("dir");
        } else {
            element = dirList.createElement("file");
        }

        element.setAttribute("name", file.getName());
        element.setAttribute("path", file.toURI().toString());
        root.appendChild(element);
    }

    dirList.appendChild(root);
    return dirList;
}