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.sonar.maven.it.ItUtils.java

private static Element createPluginRepositories(Document doc) {
    Element repositories = doc.createElement("pluginRepositories");
    Element repository = doc.createElement("pluginRepository");
    Element id = doc.createElement("id");
    Element url = doc.createElement("url");

    id.setTextContent("sonarsource");
    url.setTextContent("https://repox.sonarsource.com/sonarsource");

    repositories.appendChild(repository);
    repository.appendChild(id);//from  www.ja  v  a  2 s  . com
    repository.appendChild(url);

    return repositories;
}

From source file:Main.java

/**
 * Rename an element in a DOM document. It happens to involves a node
 * replication./*w w  w  .  ja  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;
}

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

public static Element setChild(Document doc, Element parent, String name) {
    if (parent == null) {
        return null;
    }//w  w  w.  jav a 2  s . c  o  m
    Element child = getChildByName(parent, name);
    if (child == null) {
        child = doc.createElement(name);
        parent.appendChild(child);
    }
    return child;
}

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

/**
 * Appends &lt;html&gt;, &lt;head&gt;, &lt;title&gt;, and &lt;body&gt; elements to the document.
 *
 * @param document The containing document.
 *
 * @return The &lt;body&gt; element.
 *//*from www  .  j a va 2s .c  o  m*/
private static Element appendHeadAndBody(Document document) {
    Element htmlElement = document.createElement("html");
    document.appendChild(htmlElement);
    Element headElement = document.createElement("head");
    htmlElement.appendChild(headElement);
    Element titleElement = document.createElement("title");
    titleElement.setTextContent("Google Visualization");
    headElement.appendChild(titleElement);
    Element bodyElement = document.createElement("body");
    htmlElement.appendChild(bodyElement);
    return bodyElement;
}

From source file:at.gv.egovernment.moa.id.util.client.mis.simple.MISSimpleClient.java

public static List<MISMandate> sendGetMandatesRequest(String webServiceURL, String sessionId,
        SSLSocketFactory sSLSocketFactory) throws MISSimpleClientException {
    if (webServiceURL == null) {
        throw new NullPointerException("Argument webServiceURL must not be null.");
    }//from   w  w w  .  ja  v  a 2s.co  m
    if (sessionId == null) {
        throw new NullPointerException("Argument sessionId must not be null.");
    }

    // ssl settings
    if (sSLSocketFactory != null) {
        SZRGWSecureSocketFactory fac = new SZRGWSecureSocketFactory(sSLSocketFactory);
        Protocol.registerProtocol("https", new Protocol("https", fac, 443));
    }

    try {
        Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
        Element mirElement = doc.createElementNS(MIS_NS, "MandateIssueRequest");
        Element sessionIdElement = doc.createElementNS(MIS_NS, "SessionID");
        sessionIdElement.appendChild(doc.createTextNode(sessionId));
        mirElement.appendChild(sessionIdElement);

        // send soap request
        Element mandateIssueResponseElement = sendSOAPRequest(webServiceURL, mirElement);

        // check for error
        checkForError(mandateIssueResponseElement);

        // check for session id
        NodeList mandateElements = XPathAPI.selectNodeList(mandateIssueResponseElement,
                "//mis:MandateIssueResponse/mis:Mandates/mis:Mandate", NS_NODE);

        if (mandateElements == null || mandateElements.getLength() == 0) {
            throw new MISSimpleClientException("No mandates found in response.");
        }

        ArrayList<MISMandate> foundMandates = new ArrayList<MISMandate>();
        for (int i = 0; i < mandateElements.getLength(); i++) {
            Element mandate = (Element) mandateElements.item(i);

            MISMandate misMandate = new MISMandate();
            if (mandate.hasAttribute("ProfessionalRepresentative")) {
                //               System.out.println("OID: " + mandate.getAttribute("ProfessionalRepresentative"));
                misMandate.setProfRep(mandate.getAttribute("ProfessionalRepresentative"));
            }
            if (mandate.hasAttribute("OWbPK")) {
                misMandate.setOWbPK(mandate.getAttribute("OWbPK"));
                //               System.out.println("OWBPK: " + mandate.getAttribute("OWbPK"));
            }

            //misMandate.setMandate(Base64.decodeBase64(DOMUtils.getText(mandate)));
            misMandate.setMandate(Base64.decodeBase64(DOMUtils.getText(mandate).getBytes()));
            misMandate.setFullMandateIncluded(true);

            foundMandates.add(misMandate);
        }
        return foundMandates;
    } catch (ParserConfigurationException e) {
        throw new MISSimpleClientException("service.06", e);
    } catch (DOMException e) {
        throw new MISSimpleClientException("service.06", e);
    } catch (TransformerException e) {
        throw new MISSimpleClientException("service.06", e);
    }
}

From source file:com.viettel.ws.client.JDBCUtil.java

/**
 * Create document using DOM api//from ww  w  .j  a va 2 s . c  om
 *
 * @param rs a result set
 * @param doc a input document for append content
 * @param rsName name of the appended element
 * @return a document after append content
 * @throws ParserConfigurationException If error when parse XML string
 * @throws SQLException If error when read data from database
 */
public static Document add2Document1(ResultSet rs1, ResultSet rs2, Document doc, String rsName)
        throws ParserConfigurationException, SQLException {

    if (rs1 == null && rs2 == null) {
        return doc;
    }

    //Get root element
    Element root = doc.getDocumentElement();

    Element rsElement = doc.createElement(rsName);
    root.appendChild(rsElement);

    if (rs1 != null) {
        ResultSetMetaData rsmd = rs1.getMetaData();
        int colCount = rsmd.getColumnCount();

        while (rs1.next()) {
            Element row = doc.createElement("Row");
            rsElement.appendChild(row);
            try {
                for (int i = 1; i <= colCount; i++) {
                    String columnName = rsmd.getColumnName(i);
                    Object value = rs1.getObject(i);
                    if (value == null) {
                        value = "";
                    }

                    Element node = doc.createElement(columnName);
                    node.appendChild(doc.createTextNode(value.toString()));
                    row.appendChild(node);
                }
            } catch (Exception ex) {
                LogUtil.addLog(ex);//binhnt sonar a160901
                //                    logger.error(e, e);
            }
        }
    }

    if (rs2 != null) {
        ResultSetMetaData rsmd = rs2.getMetaData();
        int colCount = rsmd.getColumnCount();

        while (rs2.next()) {
            Element row = doc.createElement("Row");
            rsElement.appendChild(row);
            try {
                for (int i = 1; i <= colCount; i++) {
                    String columnName = rsmd.getColumnName(i);
                    Object value = rs2.getObject(i);
                    if (value == null) {
                        value = "";
                    }

                    Element node = doc.createElement(columnName);
                    node.appendChild(doc.createTextNode(value.toString()));
                    row.appendChild(node);
                }
            } catch (Exception ex) {
                LogUtil.addLog(ex);//binhnt sonar a160901
                //                    logger.error(e, e);
            }
        }
    }
    return doc;

}

From source file:Utils.java

public static Element findContainerWithAttributeValueElseCreate(Document document, Element parent,
        String element, String attributeName, String attributeValue) {

    NodeList nl = parent.getElementsByTagName(element);
    Element e;//from   w w w . j  av  a 2  s  .c om
    for (int i = 0; i < nl.getLength(); i++) {
        e = (Element) nl.item(i);
        if (e.getAttribute(attributeName).equals(attributeValue)) {
            return e;
        }
    }

    e = document.createElement(element);
    parent.appendChild(e);
    e.setAttribute(attributeName, attributeValue);

    return e;
}

From source file:Main.java

public static Element appendSingleValElementEncoded(Document owner, Element appendElement, String newElemName,
        String newElemVal) {//from   ww w  .  java  2s. c o  m
    Element newElem;
    newElem = owner.createElement(newElemName);
    if (newElemVal != null) {
        String encodedVal = "";
        try {
            encodedVal = URLEncoder.encode(newElemVal, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Text value = owner.createTextNode(encodedVal);
        newElem.appendChild(value);
        newElem.setAttribute("enc", "t");
        newElem.setAttribute("charSet", "UTF-8");
    }
    appendElement.appendChild(newElem);
    return (newElem);
}

From source file:com.nridge.core.base.std.XMLUtl.java

public static void makeElemBoolValue(Document aDocument, Element anElement, String aName, boolean aFlag) {
    String aValue;/*w  ww  . j  a v a 2 s. c o m*/
    Element subElement;

    if (StringUtils.isNotEmpty(aName)) {
        if (aFlag)
            aValue = XML_UPPER_YES;
        else
            aValue = XML_UPPER_NO;
        subElement = aDocument.createElement(aName);
        subElement.appendChild(aDocument.createTextNode(aValue));
        anElement.appendChild(subElement);
    }
}

From source file:Main.java

/**
 * Find an element using XPath-quotation expressions. Path must not including
 * the context element, path elements can be separated by / or .,
 * and namespace is NOT supported.//w w  w.ja v a  2  s  . c  om
 * @param context Element to start the search from, cannot be null.
 * @param path XPath-quotation expression, cannot be null.
 * @param create if true, new elements are created if necessary.
 * @return the first matched element if there are matches, otherwise
 * return null.
 */
public static Element getElementByPath(Element context, String path, boolean create) {
    Element cur = context;
    StringTokenizer tokens = new StringTokenizer(path, "/");

    while (tokens.hasMoreTokens()) {
        String name = tokens.nextToken();
        Element parent = cur;
        cur = getChildElement(cur, name);
        if (cur == null) {
            if (create) {
                // create the element
                Element newElement = context.getOwnerDocument().createElement(name);
                //context.appendChild(newElement);
                parent.appendChild(newElement);
                cur = newElement;
            } else {
                return null;
            }
        }
    }
    return cur;
}