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:com.msopentech.odatajclient.engine.data.ODataBinder.java

private static Element toPrimitivePropertyElement(final String name, final ODataPrimitiveValue value,
        final Document doc, final boolean setType) {

    final Element element = doc.createElement(ODataConstants.PREFIX_DATASERVICES + name);
    if (setType) {
        element.setAttribute(ODataConstants.ATTR_M_TYPE, value.getTypeName());
    }//from w  ww  .jav  a2s  .  c om

    if (value instanceof ODataGeospatialValue) {
        element.appendChild(doc.importNode(((ODataGeospatialValue) value).toTree(), true));
    } else {
        element.setTextContent(value.toString());
    }

    return element;
}

From source file:main.java.vasolsim.common.GenericUtils.java

/**
 * Creates a text node within a created element and then attaches the date to a parent.
 *
 * @param elementName   the name for the new element
 * @param nodeData      the data for the new text node
 * @param parentElement the parent element
 * @param doc           the document (root)
 *//*from  ww w . j  a  v a2s  .  com*/
public static void appendSubNode(String elementName, String nodeData, Element parentElement, Document doc) {
    Element subElement = doc.createElement(elementName);
    subElement.appendChild(doc.createTextNode(nodeData));
    parentElement.appendChild(subElement);
}

From source file:main.java.vasolsim.common.GenericUtils.java

/**
 * Creates a cdata node within a created element and then attaches the date to a parent.
 *
 * @param elementName   the name for the new element
 * @param nodeData      the data for the new text node
 * @param parentElement the parent element
 * @param doc           the document (root)
 *///www. ja  v  a2s.  c om
public static void appendCDATASubNode(String elementName, String nodeData, Element parentElement,
        Document doc) {
    Element subElement = doc.createElement(elementName);
    subElement.appendChild(doc.createCDATASection(nodeData));
    parentElement.appendChild(subElement);
}

From source file:Main.java

/**
 * Set the text of the specified element to the given string.
 * //from ww  w  . ja  v  a2  s  .  c o  m
 * @param e The element.
 * @param text The text string.
 */
public static void setText(Element e, String text) {
    NodeList lst = e.getChildNodes();
    int size = lst.getLength();
    for (int i = 0; i < size; i++) {
        Node n = lst.item(i);
        if (n.getNodeType() == Node.TEXT_NODE) {
            Text t = (Text) n;
            t.setData(text.trim());
            return;
        }
    }
    Document doc = e.getOwnerDocument();
    // bit of a hack - we preserve the cdata on the way in so we can serialize correctly
    // This only works on xml to xml 
    // TODO need to have a "preserve format" or some such on the mdmi structure
    if (text.startsWith("<![CDATA[") && text.endsWith("]]>")) {
        CDATASection cdata = doc
                .createCDATASection(text != null ? text.substring(9, text.lastIndexOf("]]>")) : null);
        e.appendChild(cdata);
    } else {
        Text txt = doc.createTextNode(text != null ? text.trim() : null);
        e.appendChild(txt);
    }

}

From source file:XMLUtils.java

/**
 * Sets the text value for a given element as a CDATA section
 * @param el/*  w ww .  j av a2s . com*/
 * @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:com.msopentech.odatajclient.engine.data.ODataBinder.java

private static Element toComplexPropertyElement(final String name, final ODataComplexValue value,
        final Document doc, final boolean setType) {

    final Element element = doc.createElement(ODataConstants.PREFIX_DATASERVICES + name);
    if (value.getTypeName() != null && setType) {
        element.setAttribute(ODataConstants.ATTR_M_TYPE, value.getTypeName());
    }//from  ww  w . jav  a 2s .com

    for (ODataProperty field : value) {
        element.appendChild(toDOMElement(field, doc, true));
    }
    return element;
}

From source file:edu.kit.dama.mdm.content.util.DublinCoreHelper.java

/**
 * Create the Dublin Core document.//from w ww  .  j  a  va  2 s  . com
 *
 * @param theObject The object to create the DC information for.
 * @param pCreator A custom creator stored as author/publisher in Dublin
 * Core. If not provided, the object's uploader is used if available.
 *
 * @return The Dublin Core Document.
 *
 * @throws ParserConfigurationException If creating the Dublin Core document
 * failed.
 */
public static Document createDublinCoreDocument(DigitalObject theObject, UserData pCreator)
        throws ParserConfigurationException {
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.newDocument();

    Element root = doc.createElementNS("http://www.openarchives.org/OAI/2.0/oai_dc/", "oai_dc:dc");
    root.setAttribute("xmlns:dc", "http://purl.org/dc/elements/1.1/");
    root.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
    root.setAttribute("xsi:schemaLocation",
            "http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd");

    doc.appendChild(root);

    Element title = doc.createElementNS("http://purl.org/dc/elements/1.1/", "dc:title");
    title.setTextContent(StringEscapeUtils.escapeXml11(theObject.getLabel()));
    root.appendChild(title);

    if (pCreator != null) {
        Element creator = doc.createElementNS("http://purl.org/dc/elements/1.1/", "dc:creator");
        Element publisher = doc.createElementNS("http://purl.org/dc/elements/1.1/", "dc:publisher");
        creator.setTextContent(StringEscapeUtils.escapeXml11(pCreator.getFullname()));
        publisher.setTextContent(StringEscapeUtils.escapeXml11(pCreator.getFullname()));
        root.appendChild(creator);
        root.appendChild(publisher);
    } else if (theObject.getUploader() != null) {
        Element creator = doc.createElementNS("http://purl.org/dc/elements/1.1/", "dc:creator");
        Element publisher = doc.createElementNS("http://purl.org/dc/elements/1.1/", "dc:publisher");
        creator.setTextContent(StringEscapeUtils.escapeXml11(theObject.getUploader().getFullname()));
        publisher.setTextContent(StringEscapeUtils.escapeXml11(theObject.getUploader().getFullname()));
        root.appendChild(creator);
        root.appendChild(publisher);
    }

    for (UserData experimenter : theObject.getExperimenters()) {
        //don't list uploader a second time here
        if (theObject.getUploader() == null || !experimenter.equals(theObject.getUploader())) {
            Element contributor = doc.createElementNS("http://purl.org/dc/elements/1.1/", "dc:contributor");
            contributor.setTextContent(StringEscapeUtils.escapeXml11(experimenter.getFullname()));
            root.appendChild(contributor);
        }
    }

    if (theObject.getInvestigation() != null) {
        Element subject = doc.createElementNS("http://purl.org/dc/elements/1.1/", "dc:subject");
        subject.setTextContent(StringEscapeUtils.escapeXml11(theObject.getInvestigation().getTopic()));
        root.appendChild(subject);
        if (theObject.getInvestigation().getDescription() != null) {
            Element description = doc.createElementNS("http://purl.org/dc/elements/1.1/", "dc:description");
            description.setTextContent(
                    StringEscapeUtils.escapeXml11(theObject.getInvestigation().getDescription()));
            root.appendChild(description);
        }

        if (theObject.getInvestigation().getStudy() != null) {
            if (theObject.getInvestigation().getStudy().getLegalNote() != null) {
                Element rights = doc.createElementNS("http://purl.org/dc/elements/1.1/", "dc:rights");
                rights.setTextContent(
                        StringEscapeUtils.escapeXml11(theObject.getInvestigation().getStudy().getLegalNote()));
                root.appendChild(rights);
            }
        }
    }

    if (theObject.getStartDate() != null) {
        Element date = doc.createElementNS("http://purl.org/dc/elements/1.1/", "dc:date");
        date.setTextContent(df.format(theObject.getStartDate()));
        root.appendChild(date);
    }
    Element format = doc.createElementNS("http://purl.org/dc/elements/1.1/", "dc:format");
    format.setTextContent("application/octet-stream");
    root.appendChild(format);
    Element type = doc.createElementNS("http://purl.org/dc/elements/1.1/", "dc:type");
    type.setTextContent("Dataset");
    root.appendChild(type);
    Element identifier = doc.createElementNS("http://purl.org/dc/elements/1.1/", "dc:identifier");
    identifier.setTextContent(
            StringEscapeUtils.escapeXml11(theObject.getDigitalObjectId().getStringRepresentation()));
    root.appendChild(identifier);
    return doc;
}

From source file:XMLUtils.java

/**
 * Adds an element as a child of a given element. 
 * The child is created with the same namespace as the parent. 
 * @param parent/* ww  w .j a v a 2 s. c o  m*/
 * @param name
 * @return
 */
public static Element addElement(Element parent, String name) {
    Document doc = parent.getOwnerDocument();
    String qname;
    if (parent.getPrefix() != null) {
        qname = parent.getPrefix() + ":" + name;
    } else {
        qname = name;
    }
    Element child = doc.createElementNS(parent.getNamespaceURI(), qname);
    parent.appendChild(child);
    return child;
}

From source file:com.meltmedia.cadmium.core.util.WarUtils.java

/**
 * Adds a shiro environment listener to load the shiro config file.
 * @param doc The xml DOM document to create the new xml elements with.
 * @param root The xml Element node to add the listener to.
 *///from   w  w  w.  j  a  v a2  s  . c o  m
public static void addListener(Document doc, Element root) {
    Element listener = doc.createElement("listener");
    Element listenerClass = doc.createElement("listener-class");
    listener.appendChild(listenerClass);
    listenerClass.appendChild(doc.createTextNode("org.apache.shiro.web.env.EnvironmentLoaderListener"));

    addRelativeTo(root, listener, "listener", true);
}

From source file:com.granule.json.utils.XML.java

private static void convertJSONArray(Document doc, Element parent, JSONArray jArray, String tagName) {
    tagName = removeProblemCharacters(tagName);
    for (int i = 0; i < jArray.size(); i++) {
        Element element = doc.createElement(tagName);
        if (parent != null) {
            parent.appendChild(element);
        } else {//  w  ww. j a v a2  s  .c o m
            doc.appendChild(element);
        }

        Object obj = jArray.get(i);

        if (obj instanceof Number) {
            Node tNode = doc.createTextNode(obj.toString());
            element.appendChild(tNode);
        } else if (obj instanceof Boolean) {
            Node tNode = doc.createTextNode(obj.toString());
            element.appendChild(tNode);
        } else if (obj instanceof String) {
            Node tNode = doc.createTextNode(escapeEntityCharacters(obj.toString()));
            element.appendChild(tNode);
        } else if (obj instanceof JSONObject) {
            convertJSONObject(doc, element, (JSONObject) obj, "jsonObject");
        } else if (obj instanceof JSONArray) {
            convertJSONArray(doc, element, (JSONArray) obj, "jsonArray");
        }
    }
}