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.hphoto.server.ApiServlet.java

private static Element addNode(Document doc, Node parent, String name, String text) {
    Element child = doc.createElement(name);
    child.appendChild(doc.createTextNode(getLegalXml(text)));
    parent.appendChild(child);/*from   ww  w.j  a  v a  2s .  c  o m*/
    return child;
}

From source file:com.google.visualization.datasource.render.HtmlRenderer.java

/**
 * Renders a simple html for the given responseStatus.
 *
 * @param responseStatus The response message.
 *
 * @return A simple html for the given responseStatus.
 *///w  w w  .  j  ava  2  s .  co m
public static CharSequence renderHtmlError(ResponseStatus responseStatus) {
    // Get the responseStatus details.
    StatusType status = responseStatus.getStatusType();
    ReasonType reason = responseStatus.getReasonType();
    String detailedMessage = responseStatus.getDescription();

    // Create an xml document with head and an empty body.
    Document document = createDocument();
    Element bodyElement = appendHeadAndBody(document);

    // Populate the xml document.
    Element oopsElement = document.createElement("h3");
    oopsElement.setTextContent("Oops, an error occured.");
    bodyElement.appendChild(oopsElement);

    if (status != null) {
        String text = "Status: " + status.lowerCaseString();
        appendSimpleText(document, bodyElement, text);
    }

    if (reason != null) {
        String text = "Reason: " + reason.getMessageForReasonType(null);
        appendSimpleText(document, bodyElement, text);
    }

    if (detailedMessage != null) {
        String text = "Description: " + sanitizeDetailedMessage(detailedMessage);
        appendSimpleText(document, bodyElement, text);
    }

    return transformDocumentToHtmlString(document);
}

From source file:Main.java

/**
 * Convert an XML fragment from one namespace to another.
 *
 * @param from      element to translate
 * @param namespace namespace to be translated to
 * @return the element in the new namespace
 *
 * @since 8.4/*from   w w  w.j  av a2  s .  co m*/
 */
public static Element translateXML(Element from, String namespace) {
    Element to = from.getOwnerDocument().createElementNS(namespace, from.getLocalName());
    NodeList nl = from.getChildNodes();
    int length = nl.getLength();
    for (int i = 0; i < length; i++) {
        Node node = nl.item(i);
        Node newNode;
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            newNode = translateXML((Element) node, namespace);
        } else {
            newNode = node.cloneNode(true);
        }
        to.appendChild(newNode);
    }
    NamedNodeMap m = from.getAttributes();
    for (int i = 0; i < m.getLength(); i++) {
        Node attr = m.item(i);
        to.setAttribute(attr.getNodeName(), attr.getNodeValue());
    }
    return to;
}

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./* ww w .ja va 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:com.photon.phresco.impl.WindowsApplicationProcessor.java

private static void createNewItemGroup(Document doc, List<ArtifactGroup> artifactGroups) {
    Element project = doc.getDocumentElement();
    Element itemGroup = doc.createElement(ITEMGROUP);
    for (ArtifactGroup artifactGroup : artifactGroups) {
        if (artifactGroup.getType().name().equals(Type.FEATURE.name())) {
            Element reference = doc.createElement(REFERENCE);
            reference.setAttribute(INCLUDE, artifactGroup.getName());
            Element hintPath = doc.createElement(HINTPATH);
            hintPath.setTextContent(DOUBLE_DOT + COMMON + File.separator + artifactGroup.getName() + DLL);
            reference.appendChild(hintPath);
            itemGroup.appendChild(reference);
        }//  w  w w  . j a  v  a 2  s .c om
    }
    project.appendChild(itemGroup);
}

From source file:Main.java

/**
 * Set the text content of an element. All exisitng Text Node are
 * removed before adding a new Text Node containing the given text.
 *
 * @param element target element to set text content, cannot be null.
 * @param text content of the element, cannot be null.
 *///w w w . j  a v a  2  s  .co  m
public static void setElementText(Element element, String text) {

    // Remove all text element
    NodeList list = element.getChildNodes();
    int len = list.getLength();

    for (int i = 0; i < len; i++) {
        Node n = list.item(i);

        if (n.getNodeType() == Node.TEXT_NODE) {
            element.removeChild(n);
        }
    }
    Node child = element.getFirstChild();
    Node textnode = element.getOwnerDocument().createTextNode(text);

    // insert text node as first child
    if (child == null) {
        element.appendChild(textnode);
    } else {
        element.insertBefore(textnode, child);
    }
}

From source file:com.hphoto.server.ApiServlet.java

private static Element addNode(Document doc, Node parent, String ns, String name, String text) {
    Element child = doc.createElementNS((String) NS_MAP.get(ns), ns + ":" + name);
    child.appendChild(doc.createTextNode(getLegalXml(text)));
    parent.appendChild(child);// w w  w . jav a2s  .c om
    return child;
}

From source file:Main.java

public static Element appendResultSetToNode(Element root, String rowTag, ResultSet rs) throws SQLException {
    Document doc = root.getOwnerDocument();

    ResultSetMetaData meta = rs.getMetaData();
    int columnCount = meta.getColumnCount();
    int rowCount = 0;
    while (rs.next()) {
        Element rowElement = doc.createElement(rowTag);
        rowElement.setAttribute("row", "" + rowCount);
        for (int i = 1; i <= columnCount; i++) {
            rowElement.setAttribute(meta.getColumnName(i), rs.getString(i));
        }/*from  w  w w  . j a  va 2 s  .co m*/
        rowCount++;
        root.appendChild(rowElement);
    }

    return root;
}

From source file:com.msopentech.odatajclient.engine.data.json.GeospatialJSONHandler.java

private static Element deserializePoint(final Document document, final Iterator<JsonNode> itor) {
    final Element point = document.createElementNS(ODataConstants.NS_GML, ODataConstants.ELEM_POINT);

    final Element ppos = document.createElementNS(ODataConstants.NS_GML, ODataConstants.ELEM_POS);
    point.appendChild(ppos);
    if (itor.hasNext()) {
        ppos.appendChild(document.createTextNode(itor.next().asText() + " " + itor.next().asText()));
    }/* w  ww. j  a  v a 2s.co  m*/

    return point;
}

From source file:com.nortal.jroad.util.SOAPUtil.java

/**
 * Adds a new element to an existing element
 *
 * @param element The parent {@link Element}, which the child will be added to.
 * @param id Tag name of the new {@link Element}
 * @param type xsi type of the new {@link Element}
 * @param value Text value of the new {@link Element}
 * @return/*from w w  w  . jav a  2s .  c om*/
 * @throws SOAPException
 */
public static Element addElement(Element element, String id, String type, String value) throws SOAPException {
    Element child = element.getOwnerDocument().createElement(id);

    if (value != null) {
        child.setTextContent(value);
    }

    SOAPUtil.addTypeAttribute(child, type);

    element.appendChild(child);
    return child;
}