Example usage for org.w3c.dom Document createElementNS

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

Introduction

In this page you can find the example usage for org.w3c.dom Document createElementNS.

Prototype

public Element createElementNS(String namespaceURI, String qualifiedName) throws DOMException;

Source Link

Document

Creates an element of the given qualified name and namespace URI.

Usage

From source file:net.padaf.xmpbox.SaveMetadataHelper.java

/**
 * Serialize a schema into an Output stream
 * /* w w w.j a  v a2 s.c om*/
 * @param schema
 *            Schema concerned by the serialization processing
 * @param os
 *            Stream to save serialized schema
 * @throws TransformException
 *             When couldn't parse data to XML/RDF
 */
public static void serialize(XMPSchema schema, OutputStream os) throws TransformException {
    try {
        Document doc = XMLUtil.newDocument();
        Element rdf = doc.createElementNS("http://www.w3.org/1999/02/22-rdf-syntax-ns#", "rdf:RDF");
        Node schemContent = schema.getElement().cloneNode(true);
        doc.adoptNode(schemContent);
        rdf.appendChild(schemContent);
        XMLUtil.save(rdf, os, "UTF-8");
    } catch (TransformerException e) {
        throw new TransformException("Failed to parse defined XMP");
    } catch (IOException e) {
        throw new TransformException("Failed to create Document to contain Schema representation ",
                e.getCause());
    }

}

From source file:Main.java

public static Element appendNewElement(Document document, Element parent, String element, Object content,
        String namespace) {/*from  w  ww. j av a  2s  . com*/
    Element childElement;
    if (namespace != null) {
        childElement = document.createElementNS(namespace, element);
    } else {
        childElement = document.createElement(element);
    }

    if (content != null) {
        // TODO: We'll have that on Android 2.2:
        // childElement.setTextContent(content.toString());
        // Meanwhile:
        childElement.appendChild(document.createTextNode(content.toString()));
    }

    parent.appendChild(childElement);
    return childElement;
}

From source file:Main.java

public static void encodeStringList(Document doc, Element parent, List<String> list, String namespaceURI,
        String qualifiedName) {//from w  w w . ja  va2 s  .c  o  m
    Element currElm;
    Text currVal;

    if (list != null) {
        for (String item : list) {

            currElm = doc.createElementNS(namespaceURI, qualifiedName);
            currVal = doc.createTextNode(item);

            currElm.appendChild(currVal);
            parent.appendChild(currElm);
        }
    }
}

From source file:Main.java

/**
 * Creates a new Element having the specified qualified name. The element
 * must be {@link Document#adoptNode(Node) adopted} when inserted into
 * another Document.//from  w ww. jav  a 2s .  co  m
 *
 * @param qName A QName object.
 * @return An Element node (with a Document owner but no parent).
 */
public static Element createElement(QName qName) {
    Document doc = null;
    try {
        doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    } catch (ParserConfigurationException e) {
        throw new RuntimeException(e);
    }
    Element elem = doc.createElementNS(qName.getNamespaceURI(), qName.getLocalPart());
    return elem;
}

From source file:Main.java

/**
 * returns XML representation of a string, with specified XML node name
 * //w  w  w .  j a v a  2  s.  c  o  m
 * @param doc XML Document in which to build the XML element
 * @param namespaceURI the namespace URI
 * @param qname top level qualified XML node name in XML representation
 * @param str the string
 * @return XML representation of the string, with specified XML node name
 **/
public final static Element strToXML(final Document doc, final String namespaceURI, final String qname,
        final String str) {
    if (str == null /*|| str.length() == 0*/) {
        return null;
    }
    final Element xml = doc.createElementNS(namespaceURI, qname);
    xml.appendChild(doc.createTextNode(str));

    return xml;
}

From source file:com.msopentech.odatajclient.engine.data.Serializer.java

private static void xmlLink(final ODataLink link, final Writer writer) {
    try {/*from  w  w w.j a v  a  2 s  .co  m*/
        final DocumentBuilder builder = ODataConstants.DOC_BUILDER_FACTORY.newDocumentBuilder();
        final Document doc = builder.newDocument();
        final Element uri = doc.createElementNS(ODataConstants.NS_DATASERVICES, ODataConstants.ELEM_URI);
        uri.appendChild(doc.createTextNode(link.getLink().toASCIIString()));

        dom(uri, writer);
    } catch (Exception e) {
        throw new IllegalArgumentException("While serializing XML link", e);
    }
}

From source file:Main.java

/**
 * Creates a new DOM Tree with a root element containing the schema attributes.
 *
 * @param rootName        The name of the root element.
 * @param namespaceURI    The uri of the namespace of the document.
 * @param namespacePrefix The prefix to use for the namespace (ie. the part
 *                        before the ':' in the root element.
 * @param namespaceXSD    The name of the xsd file used to validate the file.
 *                        Should be given relative to xsdBase.
 *//*from  w ww . j  av  a2  s. c o m*/
public static Document newXMLTree(String rootName, String namespaceURI, String namespacePrefix, String xsdBase,
        String namespaceXSD) {
    try {
        DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();

        DocumentBuilder b = f.newDocumentBuilder();
        Document doc = b.newDocument();
        Element rootNode;
        if (namespaceURI == null) {
            rootNode = doc.createElement(rootName);
        } else {
            rootNode = doc.createElementNS(namespaceURI, rootName);

            if (namespacePrefix != null) {
                rootNode.setPrefix(namespacePrefix);
            }

            rootNode.setAttributeNS(xmlnsURI, "xmlns:xsi", schemaInstanceNS);
            if (namespacePrefix == null || namespacePrefix.isEmpty()) {
                rootNode.setAttributeNS(xmlnsURI, "xmlns", namespaceURI);
            } else {
                rootNode.setAttributeNS(xmlnsURI, "xmlns:" + namespacePrefix, namespaceURI);
            }

            if (xsdBase != null && namespaceXSD != null) {
                rootNode.setAttributeNS(schemaInstanceNS, "xsi:schemaLocation",
                        namespaceURI + " " + xsdBase + namespaceXSD);
            }
        }
        doc.appendChild(rootNode);
        return doc;
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

/**
 * Rename an element in a DOM document. It happens to involves a node
 * replication./*  w  ww  .ja va2s . c  om*/
 * 
 * @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:edu.kit.dama.mdm.content.util.DublinCoreHelper.java

/**
 * Create the Dublin Core document.//from w w  w  .  java  2s. co  m
 *
 * @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

public static Element createElementNS(Document root, String namespaceURI, String qualifiedName) {
    return root.createElementNS(namespaceURI, qualifiedName);
}