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:hu.bme.mit.sette.common.descriptors.eclipse.EclipseProjectDescriptor.java

/**
 * Creates the XML document for the .project file.
 *
 * @return The XML document./*from  www. ja  va  2s .  com*/
 * @throws ParserConfigurationException
 *             If a DocumentBuilder cannot be created which satisfies the
 *             configuration requested.
 */
public Document createXmlDocument() throws ParserConfigurationException {
    // create document object
    Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    document.setXmlVersion("1.0");
    document.setXmlStandalone(true);

    // add root tag
    Element rootTag = document.createElement("projectDescription");
    document.appendChild(rootTag);

    // add name and comment
    rootTag.appendChild(document.createElement("name")).setTextContent(name);
    rootTag.appendChild(document.createElement("comment")).setTextContent(comment);

    // add the projects tag
    rootTag.appendChild(document.createElement("projects"));

    // add the Java builder
    Element buildSpecTag = document.createElement("buildSpec");
    rootTag.appendChild(buildSpecTag);

    Element buildCommandTag = document.createElement("buildCommand");
    buildSpecTag.appendChild(buildCommandTag);

    buildCommandTag.appendChild(document.createElement("name"))
            .setTextContent(EclipseProjectDescriptor.JAVA_BUILDER);
    buildCommandTag.appendChild(document.createElement("arguments"));

    // add the Java nature
    Element naturesTag = document.createElement("natures");
    rootTag.appendChild(naturesTag);

    naturesTag.appendChild(document.createElement("nature"))
            .setTextContent(EclipseProjectDescriptor.JAVA_NATURE);

    return document;
}

From source file:com.adaptris.core.services.jdbc.XmlPayloadTranslator.java

private DocumentWrapper toDocument(JdbcResult rs, AdaptrisMessage msg)
        throws ParserConfigurationException, SQLException {
    XmlUtils xu = createXmlUtils(msg);/*  w  ww  .  j  a  va  2s .c  om*/
    DocumentBuilderFactoryBuilder factoryBuilder = documentFactoryBuilder(msg);
    DocumentBuilderFactory factory = factoryBuilder.configure(DocumentBuilderFactory.newInstance());
    DocumentBuilder builder = factoryBuilder.configure(factory.newDocumentBuilder());
    Document doc = builder.newDocument();
    DocumentWrapper result = new DocumentWrapper(doc, 0);
    ColumnStyle elementNameStyle = getColumnNameStyle();

    Element results = doc.createElement(elementNameStyle.format(ELEMENT_NAME_RESULTS));
    doc.appendChild(results);

    if (isPreserveOriginalMessage()) {
        Element originalMessage = doc.createElement(elementNameStyle.format(ORIGINAL_MESSAGE_ELEMENT));

        if (xu.isDocumentValid()) {
            Node n = xu.getSingleNode("/").getFirstChild();
            originalMessage.appendChild(doc.importNode(n, true));
        } else {
            // Not XML, so let's add it in as a CDATA node.
            originalMessage.appendChild(createTextNode(doc, msg.getContent(), true));
        }
        results.appendChild(originalMessage);
    }
    for (JdbcResultSet rSet : rs.getResultSets()) {
        List<Element> elements = createListFromResultSet(builder, doc, rSet);
        for (Element element : elements) {
            results.appendChild(element);
        }
        result.resultSetCount += elements.size();
    }
    return result;
}

From source file:it.unibas.spicy.persistence.xml.operators.ExportXSD.java

private void processRoot(INode root, Document document) {
    Element schema = document.createElementNS(XSD_NS, PREFIX + "schema");
    document.appendChild(schema);
    Attr elementFormDefault = document.createAttribute("elementFormDefault");
    elementFormDefault.setValue("qualified");
    schema.setAttributeNode(elementFormDefault);
}

From source file:org.getwheat.harvest.library.dom.DomHelper.java

/**
 * Adds a root {@link Element} to the {@link Document}.
 * /*  w  w  w  .jav a2s .co  m*/
 * @param document
 * @param tag
 * @return the created {@link Element}
 */
public Element addRootElement(final Document document, final XmlTag tag) {
    final Element element = document.createElement(tag.getElementName());
    document.appendChild(element);
    return element;
}

From source file:de.uzk.hki.da.metadata.MetadataStructure.java

public void toEDM(HashMap<String, HashMap<String, List<String>>> indexInfo, File file,
        PreservationSystem preservationSystem, String objectID, String urn) {
    try {// w ww. ja  v a  2s  .  c  o  m
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

        Document edmDoc = docBuilder.newDocument();
        Element rootElement = edmDoc.createElement("rdf:RDF");
        edmDoc.appendChild(rootElement);

        addXmlNsToEDM(edmDoc, rootElement);

        for (String id : indexInfo.keySet()) {
            logger.debug("Index information about " + id + ": " + indexInfo.get(id));
            Element providedCHO = addEdmProvidedCHOtoEdm(preservationSystem, id, edmDoc, rootElement);
            Element aggregation = addOreAggregationToEdm(preservationSystem, id, edmDoc, rootElement);

            if (indexInfo.get(id).get(C.EDM_IS_PART_OF) == null) {
                List<String> root = new ArrayList<String>();
                root.add("is root element");
                indexInfo.get(id).put(C.EDM_HAS_TYPE, root);
            }

            if (indexInfo.get(id).get(C.EDM_IDENTIFIER) == null) {
                List<String> IDs = new ArrayList<String>();
                IDs.add(objectID);
                IDs.add(urn);
                indexInfo.get(id).put(C.EDM_IDENTIFIER, IDs);
            } else {
                indexInfo.get(id).get(C.EDM_IDENTIFIER).add(objectID);
                indexInfo.get(id).get(C.EDM_IDENTIFIER).add(urn);
            }

            for (String elementName : indexInfo.get(id).keySet()) {
                Element parentNode = null;
                if (elementName.startsWith("dc:") || elementName.startsWith("dcterms:")
                        || elementName.equals(C.EDM_HAS_TYPE)) {
                    parentNode = providedCHO;
                } else if (elementName.startsWith("edm:")) {
                    parentNode = aggregation;
                }
                if (parentNode != null) {
                    List<String> values = indexInfo.get(id).get(elementName);
                    for (String currentValue : values) {
                        if (!currentValue.equals("")) {
                            addNewElementToParent(preservationSystem, id, elementName, currentValue, parentNode,
                                    edmDoc);
                        }
                    }
                }
            }
        }

        javax.xml.transform.Source source = new javax.xml.transform.dom.DOMSource(edmDoc);
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        Result result = new javax.xml.transform.stream.StreamResult(file);
        transformer.transform(source, result);

    } catch (Exception e) {
        logger.error("Unable to create the edm file!");
        throw new RuntimeException(e);
    }

}

From source file:com.codebutler.farebot.mifare.Card.java

public Element toXML() throws Exception {
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = builder.newDocument();

    Element element = doc.createElement("card");
    element.setAttribute("type", String.valueOf(getCardType().toInteger()));
    element.setAttribute("id", Utils.getHexString(mTagId));
    element.setAttribute("scanned_at", Long.toString(mScannedAt.getTime()));
    doc.appendChild(element);

    return doc.getDocumentElement();
}

From source file:no.difi.sdp.client.asice.signature.CreateSignature.java

private void wrapSignatureInXADeSEnvelope(final Document document) {
    Node signatureElement = document.removeChild(document.getDocumentElement());
    Element xadesElement = document.createElementNS(asicNamespace, "XAdESSignatures");
    xadesElement.appendChild(signatureElement);
    document.appendChild(xadesElement);
}

From source file:hoot.services.controllers.osm.MapResource.java

private static Document generateExtentOSM(String maxlon, String maxlat, String minlon, String minlat) {
    SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
    Date now = new Date();
    String strDate = sdfDate.format(now);

    try {/*from   www.  ja  va2  s .  c o  m*/
        DocumentBuilderFactory dbf = XmlDocumentBuilder.getSecureDocBuilderFactory();
        dbf.setValidating(false);
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.newDocument();

        Element osmElem = doc.createElement("osm");
        osmElem.setAttribute("version", "0.6");
        osmElem.setAttribute("generator", "hootenanny");
        doc.appendChild(osmElem);

        Element boundsElem = doc.createElement("bounds");
        boundsElem.setAttribute("minlat", minlat);
        boundsElem.setAttribute("minlon", minlon);
        boundsElem.setAttribute("maxlat", maxlat);
        boundsElem.setAttribute("maxlon", maxlon);
        osmElem.appendChild(boundsElem);

        // The ID's for these fabricated nodes were stepping on the ID's of actual nodes, so their ID's need to be
        // made negative and large, so they have no chance of stepping on anything.

        long node1Id = Long.MIN_VALUE + 3;
        long node2Id = Long.MIN_VALUE + 2;
        long node3Id = Long.MIN_VALUE + 1;
        long node4Id = Long.MIN_VALUE;

        Element nodeElem = doc.createElement("node");
        nodeElem.setAttribute("id", String.valueOf(node1Id));
        nodeElem.setAttribute("timestamp", strDate);
        nodeElem.setAttribute("user", "hootenannyuser");
        nodeElem.setAttribute("visible", "true");
        nodeElem.setAttribute("version", "1");
        nodeElem.setAttribute("lat", maxlat);
        nodeElem.setAttribute("lon", minlon);
        osmElem.appendChild(nodeElem);

        nodeElem = doc.createElement("node");
        nodeElem.setAttribute("id", String.valueOf(node2Id));
        nodeElem.setAttribute("timestamp", strDate);
        nodeElem.setAttribute("user", "hootenannyuser");
        nodeElem.setAttribute("visible", "true");
        nodeElem.setAttribute("version", "1");
        nodeElem.setAttribute("lat", maxlat);
        nodeElem.setAttribute("lon", maxlon);
        osmElem.appendChild(nodeElem);

        nodeElem = doc.createElement("node");
        nodeElem.setAttribute("id", String.valueOf(node3Id));
        nodeElem.setAttribute("timestamp", strDate);
        nodeElem.setAttribute("user", "hootenannyuser");
        nodeElem.setAttribute("visible", "true");
        nodeElem.setAttribute("version", "1");
        nodeElem.setAttribute("lat", minlat);
        nodeElem.setAttribute("lon", maxlon);
        osmElem.appendChild(nodeElem);

        nodeElem = doc.createElement("node");
        nodeElem.setAttribute("id", String.valueOf(node4Id));
        nodeElem.setAttribute("timestamp", strDate);
        nodeElem.setAttribute("user", "hootenannyuser");
        nodeElem.setAttribute("visible", "true");
        nodeElem.setAttribute("version", "1");
        nodeElem.setAttribute("lat", minlat);
        nodeElem.setAttribute("lon", minlon);
        osmElem.appendChild(nodeElem);

        Element wayElem = doc.createElement("way");
        wayElem.setAttribute("id", String.valueOf(Long.MIN_VALUE));
        wayElem.setAttribute("timestamp", strDate);
        wayElem.setAttribute("user", "hootenannyuser");
        wayElem.setAttribute("visible", "true");
        wayElem.setAttribute("version", "1");

        Element ndElem = doc.createElement("nd");
        ndElem.setAttribute("ref", String.valueOf(node1Id));
        wayElem.appendChild(ndElem);

        ndElem = doc.createElement("nd");
        ndElem.setAttribute("ref", String.valueOf(node2Id));
        wayElem.appendChild(ndElem);

        ndElem = doc.createElement("nd");
        ndElem.setAttribute("ref", String.valueOf(node3Id));
        wayElem.appendChild(ndElem);

        ndElem = doc.createElement("nd");
        ndElem.setAttribute("ref", String.valueOf(node4Id));
        wayElem.appendChild(ndElem);

        ndElem = doc.createElement("nd");
        ndElem.setAttribute("ref", String.valueOf(node1Id));
        wayElem.appendChild(ndElem);

        /*
         * ndElem = doc.createElement("tag"); ndElem.setAttribute("k", "area");
         * ndElem.setAttribute("v", "yes"); wayElem.appendChild(ndElem);
         */

        osmElem.appendChild(wayElem);

        Transformer tf = TransformerFactory.newInstance().newTransformer();

        // Fortify may require this, but it doesn't work.
        // TransformerFactory transformerFactory =
        // XmlDocumentBuilder.getSecureTransformerFactory();
        tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        tf.setOutputProperty(OutputKeys.INDENT, "yes");

        try (Writer out = new StringWriter()) {
            tf.transform(new DOMSource(doc), new StreamResult(out));
            logger.debug("Layer Extent OSM: {}", out);
        }

        return doc;
    } catch (Exception e) {
        throw new RuntimeException("Error generating OSM extent", e);
    }
}

From source file:org.callimachusproject.server.RoundTripTest.java

public void testDocument() throws Exception {
    DocumentBuilderFactory builder = DocumentBuilderFactory.newInstance();
    Document doc = builder.newDocumentBuilder().newDocument();
    doc.appendChild(doc.createElement("root"));
    trip.document(doc);//from   w w  w  . j av a 2 s .  co m
    assertEquals(null, trip.document(null));
}

From source file:org.callimachusproject.server.RoundTripTest.java

public void testSetOfDocument() throws Exception {
    DocumentBuilderFactory builder = DocumentBuilderFactory.newInstance();
    Document doc = builder.newDocumentBuilder().newDocument();
    doc.appendChild(doc.createElement("root"));
    trip.setOfDocument(singleton(doc));/*from   w w w .j  a v  a 2s. co  m*/
    assertEquals(EMPTY_SET, trip.setOfDocument(EMPTY_SET));
}