Example usage for org.w3c.dom Document appendChild

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

Introduction

In this page you can find the example usage for org.w3c.dom Document 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.enonic.esl.xml.XMLTool.java

public static Document createDocument(Element root) {
    Document doc = createDocument();
    doc.appendChild(doc.importNode(root, true));
    return doc;//from  www.ja v  a  2  s.c o  m
}

From source file:net.bpelunit.util.XMLUtilTest.java

@Test
public void testGetContentsOfTextOnlyNodeOnlyTextNodes() throws Exception {
    Document doc = XMLUtil.createDocument();

    Element e = doc.createElement("a");
    doc.appendChild(e);

    e.appendChild(doc.createTextNode("a"));
    e.appendChild(doc.createTextNode("b"));
    e.appendChild(doc.createTextNode("c"));

    assertEquals("abc", XMLUtil.getContentsOfTextOnlyNode(e));
}

From source file:net.bpelunit.util.XMLUtilTest.java

@Test
public void testGetContentsOfTextOnlyNodeNotOnlyTextNodes() throws Exception {
    Document doc = XMLUtil.createDocument();

    Element e = doc.createElement("a");
    doc.appendChild(e);

    e.appendChild(doc.createTextNode("a"));
    e.appendChild(doc.createElement("b"));
    e.appendChild(doc.createTextNode("c"));

    assertNull(XMLUtil.getContentsOfTextOnlyNode(e));
}

From source file:eu.scidipes.toolkits.palibrary.utils.XMLUtils.java

/**
 * Transforms a collection of {@link FormField} objects into a simple XML document
 * //from  www.  j av a  2 s. co m
 * @param fields
 *            a collection of form fields
 * @return an xml document representing the form fields, or null if the passed collection is null or empty, or all
 *         form fields have an empty value
 */
public static Node formFieldsToMetadata(final Collection<? extends FormField> fields) {
    if (fields != null && !fields.isEmpty()) {
        try {
            final Document xml;

            synchronized (documentBuilderFactory) {
                xml = documentBuilderFactory.newDocumentBuilder().newDocument();
            }

            xml.setXmlStandalone(true);
            final Element metaData = xml.createElement("metadata");
            int completedFieldCount = 0;

            for (final FormField field : fields) {
                if (!StringUtils.isEmpty(field.getValue())) {
                    completedFieldCount++;
                    final Element metaDataEntry = xml.createElement("metadataentry");
                    final Element entryName = xml.createElement("entryname");
                    final Element entryValue = xml.createElement("entryvalue");

                    entryName.setTextContent(field.getDisplayName());
                    entryValue.setTextContent(field.getValue());

                    metaDataEntry.appendChild(entryName);
                    metaDataEntry.appendChild(entryValue);
                    metaData.appendChild(metaDataEntry);
                }
            }

            if (completedFieldCount > 0) {
                xml.appendChild(metaData);
                return xml;
            } else {
                return null;
            }

        } catch (final ParserConfigurationException e) {
            LOG.error(e.getMessage(), e);
        }
    }
    return null;
}

From source file:de.uni_potsdam.hpi.bpt.promnicat.importer.sap_rm.SapReferenceModelImporter.java

public void splitEPML(File file)
        throws ParserConfigurationException, SAXException, IOException, TransformerException, JSONException {
    DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = fac.newDocumentBuilder();
    Document doc = builder.parse(file);
    NodeList epcNodes = doc.getElementsByTagName("epc");
    for (int i = 0; i < epcNodes.getLength(); i++) {
        String epcName = epcNodes.item(i).getAttributes().getNamedItem("name").getNodeValue();

        Model model = this.persistenceApi.loadCompleteModelWithImportedId(epcName);
        if (model != null) {
            continue;
        }/*from w w w. jav  a 2 s . co m*/

        Document newEpcDoc = builder.newDocument();
        Element epml = newEpcDoc.createElement("epml:epml");
        Element directory = newEpcDoc.createElement("directory");
        newEpcDoc.appendChild(epml);
        epml.appendChild(directory);
        directory.setAttribute("name", "ROOT");
        Node epcNode = newEpcDoc.importNode(epcNodes.item(i), true);
        directory.appendChild(epcNode);

        DOMSource epmlSource = new DOMSource(newEpcDoc);
        final File epml2eRDFxsltFile = new File("resources/xslt/EPML2eRDF.xslt");
        final Source epml2eRDFxsltSource = new StreamSource(epml2eRDFxsltFile);

        // Transformer Factory
        final TransformerFactory transformerFactory = TransformerFactory.newInstance();

        // Get the epml source
        Transformer transformer = transformerFactory.newTransformer(epml2eRDFxsltSource);
        StringWriter writer = new StringWriter();
        transformer.transform(epmlSource, new StreamResult(writer));
        String erdf = writer.toString();
        String rdf = erdfToRdf(erdf);
        rdf = rdf.replaceAll("ns[0-9]+:", "");
        rdf = rdf.replace("#resource", "#");
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        Document rdfDoc = builder.parse(new ByteArrayInputStream(rdf.getBytes("UTF-8")));
        String json = RdfJsonTransformation.toJson(rdfDoc, "").toString();

        //Diagram diagram = DiagramBuilder.parseJson(json);
        Representation representation = new Representation(Constants.FORMAT_BPMAI_JSON, Constants.NOTATION_EPC);
        Diagram parseJson = DiagramBuilder.parseJson(json);
        representation.setDataContent(json.getBytes());
        model = new Model(epcName, Constants.ORIGIN_SAP_RM);
        model.setImportedId(epcName);
        Revision revision = new Revision(0);
        revision.connectRepresentation(representation);
        model.connectLatestRevision(revision);
        revision.connectModel(model);
        model.connectLatestRevision(revision);
        persistenceApi.savePojo(model);

    }

}

From source file:be.fedict.eid.dss.document.zip.ZIPSignatureService.java

@Override
protected Document getEnvelopingDocument() throws ParserConfigurationException, IOException, SAXException {
    FileInputStream fileInputStream = new FileInputStream(this.tmpFile);
    ZipInputStream zipInputStream = new ZipInputStream(fileInputStream);
    ZipEntry zipEntry;//w ww .jav a2s  . c om
    while (null != (zipEntry = zipInputStream.getNextEntry())) {
        if (ODFUtil.isSignatureFile(zipEntry)) {
            Document documentSignaturesDocument = ODFUtil.loadDocument(zipInputStream);
            return documentSignaturesDocument;
        }
    }
    Document document = ODFUtil.getNewDocument();
    Element rootElement = document.createElementNS(ODFUtil.SIGNATURE_NS, ODFUtil.SIGNATURE_ELEMENT);
    rootElement.setAttributeNS(Constants.NamespaceSpecNS, "xmlns", ODFUtil.SIGNATURE_NS);
    document.appendChild(rootElement);
    return document;
}

From source file:com.nortal.jroad.endpoint.AbstractXTeeBaseEndpoint.java

protected Document parseQuery(SOAPMessage queryMsg) throws Exception {
    Node bodyNode = SOAPUtil.getFirstNonTextChild(queryMsg.getSOAPBody());
    if (XRoadProtocolVersion.V2_0 == version) {
        bodyNode = SOAPUtil.getNodeByXPath(bodyNode, "//keha");
        if (bodyNode == null) {
            throw new IllegalStateException(
                    "Service is not metaservice, but query is missing mandatory body ('//keha\')");
        }// w w w .j a v a  2s . c o m
    }

    Document query = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    bodyNode = query.importNode(bodyNode, true);
    query.appendChild(bodyNode);
    return query;
}

From source file:com.gargoylesoftware.htmlunit.libraries.Sarissa0993Test.java

/**
 * @throws Exception if the test fails//  w ww.  j av a  2 s.c o m
 */
@Test
@Browsers(Browser.NONE)
public void xslt() throws Exception {
    final String input = "<root><element attribute=\"value\"/></root>";
    final String style = "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">\n"
            + "<xsl:output method=\"xml\" omit-xml-declaration=\"yes\"/>\n"
            + "<xsl:param select=\"'anonymous'\" name=\"user\"/>\n" + "<xsl:template match=\"/\">\n"
            + "<p id=\"user\">User: <xsl:value-of select=\"$user\"/>\n" + "</p>\n" + "<xsl:apply-templates/>\n"
            + "<hr/>\n" + "</xsl:template>\n" + "<xsl:template match=\"greeting\">\n" + "<p>\n"
            + "<xsl:apply-templates/>\n" + "</p>\n" + "</xsl:template>\n" + "</xsl:stylesheet>";

    final Source xmlSource = new StreamSource(new StringReader(input));
    final Source xsltSource = new StreamSource(new StringReader(style));

    final Document containerDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    final Element containerElement = containerDocument.createElement("container");
    containerDocument.appendChild(containerElement);

    final DOMResult result = new DOMResult(containerElement);

    final Transformer transformer = TransformerFactory.newInstance().newTransformer(xsltSource);
    transformer.transform(xmlSource, result);
}

From source file:com.connexta.arbitro.utils.policy.PolicyBuilder.java

public String build(BasicPolicyDTO basicPolicyDTO) throws PolicyBuilderException {

    Document doc = null;
    try {// w w  w. j  a  va  2 s.c om
        doc = Utils.createNewDocument();
    } catch (ParserConfigurationException e) {
        throw new PolicyBuilderException("While creating Document Object", e);
    }
    if (doc != null) {
        doc.appendChild(BasicPolicyHelper.createPolicyElement(basicPolicyDTO, doc));
        try {
            return Utils.getStringFromDocument(doc);
        } catch (TransformerException e) {
            throw new PolicyBuilderException("Error while converting Policy element to String", e);
        }
    }
    return null;
}

From source file:com.connexta.arbitro.utils.policy.PolicyBuilder.java

public String build(PolicyElementDTO policyElementDTO) throws PolicyBuilderException {

    Document doc = null;
    try {//from w w w.j av  a 2s.  co  m
        doc = Utils.createNewDocument();
    } catch (ParserConfigurationException e) {
        throw new PolicyBuilderException("While creating Document Object", e);
    }
    if (doc != null) {
        doc.appendChild(PolicyUtils.createPolicyElement(policyElementDTO, doc));
        try {
            return Utils.getStringFromDocument(doc);
        } catch (TransformerException e) {
            throw new PolicyBuilderException("Error while converting Policy element to String", e);
        }
    }
    return null;
}