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:com.evolveum.liferay.usercreatehook.ws.ModelPortWrapper.java

private static Element createTextElement(QName qname, String value, Document doc) {
    Element element = doc.createElementNS(qname.getNamespaceURI(), qname.getLocalPart());
    element.setTextContent(value);/*from w  ww  . j  a  v a2 s  .c o m*/
    return element;
}

From source file:Main.java

/**
 * @param doc/*from w w  w  .ja v  a 2  s . c om*/
 *            The Document that is the factory for the new Element.
 * @param qname
 *            The QName of the new Element.
 * @return An empty Element whose owner is the given Document.
 */
private static Element createEmptyElement(final Document doc, final QName qname) {
    final String prefix = qname.getPrefix();
    String name = qname.getLocalPart();

    //
    // NOTE: The DOM API requires that elements with qualified names 
    //       be created with prefix:localName as the tag name. We 
    //       CANNOT just use the localName and expect the API to fill 
    //       in a prefix, nor can we use setPrefix after creation. For 
    //       parsing to work correctly, the second argument to the 
    //       createElementNS method MUST be a qualified name.
    //
    if (prefix != null && prefix.length() > 0)
        name = prefix + ':' + name;

    final String uri = qname.getNamespaceURI();

    return doc.createElementNS(uri, name);
}

From source file:XMLUtils.java

/**
 * Adds an element as a child of a given element. 
 * The child is created with the same namespace as the parent. 
 * @param parent/*from www .j  a v  a2 s. co m*/
 * @param name
 * @return
 */
public static Element addElement(Element parent, String name) {
    Document doc = parent.getOwnerDocument();
    String qname;
    if (parent.getPrefix() != null) {
        qname = parent.getPrefix() + ":" + name;
    } else {
        qname = name;
    }
    Element child = doc.createElementNS(parent.getNamespaceURI(), qname);
    parent.appendChild(child);
    return child;
}

From source file:net.sourceforge.dita4publishers.word2dita.Word2DitaValidationHelper.java

/**
 * @param documentDom//ww  w.j a  va 2  s .  c o  m
 * @param commentId
 * @param xpath
 * @throws XPathExpressionException
 */
static void addCommentRefToParaForXPath(Document documentDom, String commentId, String xpath)
        throws XPathExpressionException {
    /**
    <w:r>
     <w:rPr>
       <w:rStyle
         w:val="CommentReference"/>
     </w:rPr>
     <w:commentReference
       w:id="14"/>
    </w:r>
             
     */
    Node node = getWordParaForXPath(documentDom, xpath);

    Element p = (Element) node;
    Element commentRef = documentDom.createElementNS(wNs, "w:r");
    Element elem = (Element) commentRef.appendChild(documentDom.createElementNS(wNs, "w:rPr"));
    elem = (Element) elem.appendChild(documentDom.createElementNS(wNs, "w:rStyle"));
    elem.setAttributeNS(wNs, "w:val", "CommentReference");
    elem = (Element) commentRef.appendChild(documentDom.createElementNS(wNs, "w:commentReference"));
    elem.setAttributeNS(wNs, "w:id", commentId);
    p.appendChild(commentRef);
}

From source file:Main.java

/**
 * Creates an XIInclude element with a link to a file
 *
 * @param doc  The DOM Document to create the xi:include for.
 * @param file The file name/path to link to.
 * @return An xi:include element that can be used to include content from another file.
 *//*from  w  ww. j a  va2s.c  o  m*/
public static Element createXIInclude(final Document doc, final String file) {
    try {
        // Encode the file reference
        final StringBuilder fixedFileRef = new StringBuilder();
        final String[] split = file.split("/");
        for (int i = 0; i < split.length; i++) {
            if (i != 0) {
                fixedFileRef.append("/");
            }

            final String uriPart = split[i];
            if (uriPart.isEmpty() || uriPart.equals("..") || uriPart.equals(".")) {
                fixedFileRef.append(uriPart);
            } else {
                fixedFileRef.append(URLEncoder.encode(uriPart, "UTF-8"));
            }
        }

        // If the file ref ended with "/" then it wouldn't have been added as their was no content after it
        if (file.endsWith("/")) {
            fixedFileRef.append("/");
        }

        final Element xiInclude = doc.createElementNS("http://www.w3.org/2001/XInclude", "xi:include");
        xiInclude.setAttribute("href", fixedFileRef.toString());
        xiInclude.setAttribute("xmlns:xi", "http://www.w3.org/2001/XInclude");

        return xiInclude;
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
}

From source file:eu.semaine.util.XMLTool.java

/**
 * In the given document, create a new element with the given name and
 * the given namespace./*  w  ww  .  j a va2 s . c  om*/
 * @param doc 
 * @param elementName
 * @param namespace the namespace URI, e.g. <code>http://www.w3.org/2003/04/emma</code>,
 * or null if no namespace is to be associated with the new element.
 * @return an element which is not yet included in the tree hierarchy of the document.
 * @throws NullPointerException if doc or elementName is null.
 */
public static Element createElement(Document doc, String elementName, String namespace) {
    if (doc == null)
        throw new NullPointerException("Received null document");
    if (elementName == null)
        throw new NullPointerException("Received null elementName");
    return doc.createElementNS(namespace, elementName);
}

From source file:net.sourceforge.dita4publishers.word2dita.Word2DitaValidationHelper.java

/**
 * @param pkg/* ww w .j a va  2 s  . com*/
 * @param bosOptions
 * @throws Exception 
 */
static void addCommentFileRelationship(ZipComponents zipComponents, BosConstructionOptions bosOptions)
        throws Exception {
    ZipComponent comp = zipComponents.getEntry(DocxConstants.DOCUMENT_XML_RELS_PATH);
    Document doc = zipComponents.getDomForZipComponent(bosOptions, comp);
    Element docElem = doc.getDocumentElement();
    NodeList nl = docElem.getElementsByTagNameNS(DocxConstants.RELS_NS, "Relationship");
    boolean foundCommentRel = false;
    for (int i = 0; i < nl.getLength(); i++) {
        Element elem = (Element) nl.item(i);
        String type = elem.getAttribute("Type");
        if (DocxConstants.COMMENT_REL_TYPE.equals(type)) {
            foundCommentRel = true;
            break;
        }
    }
    if (!foundCommentRel) {
        Element elem = doc.createElementNS(DocxConstants.RELS_NS, "Relationship");
        elem.setAttribute("Type", DocxConstants.COMMENT_REL_TYPE);
        elem.setAttribute("Id", "rId" + (nl.getLength() + 1));
        elem.setAttribute("Target", "comments.xml");
        docElem.appendChild(elem);
        // System.out.println(IOUtils.toString(DomUtil.serializeToInputStream(doc, "utf-8")));
        comp.setDom(doc);
    }

}

From source file:eu.europa.esig.dss.DSSXMLUtils.java

/**
 * This method creates and adds a new XML {@code Element}
 *
 * @param document  root document/*from w w  w.ja  v a 2 s  .  co m*/
 * @param parentDom parent node
 * @param namespace namespace
 * @param name      element name
 * @return added element
 */
public static Element addElement(final Document document, final Element parentDom, final String namespace,
        final String name) {

    final Element dom = document.createElementNS(namespace, name);
    parentDom.appendChild(dom);
    return dom;
}

From source file:eu.europa.esig.dss.DSSXMLUtils.java

/**
 * This method creates and adds a new XML {@code Element} with text value
 *
 * @param document  root document/*from ww w. j av  a 2 s.  c  om*/
 * @param parentDom parent node
 * @param namespace namespace
 * @param name      element name
 * @param value     element text node value
 * @return added element
 */
public static Element addTextElement(final Document document, final Element parentDom, final String namespace,
        final String name, final String value) {

    final Element dom = document.createElementNS(namespace, name);
    parentDom.appendChild(dom);
    final Text valueNode = document.createTextNode(value);
    dom.appendChild(valueNode);
    return dom;
}

From source file:net.sourceforge.dita4publishers.word2dita.Word2DitaValidationHelper.java

/**
 * @param zipComponents/*  ww w . j a v a 2  s .c o m*/
 * @param bosOptions
 * @throws Exception 
 */
public static void addCommentFileContentType(ZipComponents zipComponents, BosConstructionOptions bosOptions)
        throws Exception {

    /*
     *   <Override
    PartName="/word/comments.xml"
    ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.comments+xml"/>
            
     */

    ZipComponent comp = zipComponents.getEntry("[Content_Types].xml");
    Document doc = zipComponents.getDomForZipComponent(bosOptions, comp);
    Element docElem = doc.getDocumentElement();
    String contentTypesNs = "http://schemas.openxmlformats.org/package/2006/content-types";
    NodeList nl = docElem.getElementsByTagNameNS(contentTypesNs, "Override");
    boolean foundCommentType = false;
    for (int i = 0; i < nl.getLength(); i++) {
        Element elem = (Element) nl.item(i);
        String partName = elem.getAttribute("PartName");
        if (DocxConstants.COMMENTS_PARTNAME.equals(partName)) {
            foundCommentType = true;
            break;
        }
    }
    if (!foundCommentType) {
        Element elem = doc.createElementNS(contentTypesNs, "Override");
        elem.setAttribute("PartName", DocxConstants.COMMENTS_PARTNAME);
        elem.setAttribute("ContentType", DocxConstants.COMMENTS_CONTENT_TYPE);
        docElem.appendChild(elem);
        comp.setDom(doc);
    }
}