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:Main.java

/**
 *
 * @param doc Document//from w w  w  .  j  a v  a 2  s.c  o  m
 * @param namespace String
 * @param parent Node
 * @param name String
 * @return Element
 */
public static Element appendChildNode(Document doc, String namespace, Node parent, String name) {
    Element child = doc.createElementNS(namespace, name);
    parent.appendChild(child);
    return child;
}

From source file:Main.java

public static Element createElement(Document doc, String tag, String nsURI, String prefix) {
    String qName = prefix == null ? tag : prefix + ":" + tag;
    return doc.createElementNS(nsURI, qName);
}

From source file:Main.java

private static Node convertFromNamespaceForm(final Node node) {
    if (node instanceof Element) {
        final Document document = node.getOwnerDocument();
        final Element newElement = document.createElementNS(null, node.getLocalName());
        final NodeList children = node.getChildNodes();

        for (int i = 0, n = children.getLength(); i < n; i++) {
            final Node oldChildNode = children.item(i);
            final Node newChildNode = convertFromNamespaceForm(oldChildNode);

            newElement.appendChild(newChildNode);
        }//ww w.j  a  va 2s .  c  o m

        final NamedNodeMap attributes = node.getAttributes();

        for (int i = 0, n = attributes.getLength(); i < n; i++) {
            final Attr attr = (Attr) attributes.item(i);
            final String attrQualifiedName = attr.getNodeName();
            final String attrLocalName = attr.getLocalName();

            if (!attrQualifiedName.equals(XMLNS) && !attrQualifiedName.startsWith(XMLNS_COLON)
                    && !attrLocalName.equals(XSI_SCHEMA_LOCATION_ATTR)) {
                newElement.setAttributeNS(null, attrLocalName, attr.getValue());
            }
        }

        return newElement;
    } else {
        return node.cloneNode(true);
    }
}

From source file:Main.java

/**
 *
 * @param doc Document/*w ww .j  a  v  a 2s  .  com*/
 * @param namespace String
 * @param parent Node
 * @param name String
 * @param value String
 * @return Element
 */
public static Element appendChildNode(Document doc, String namespace, org.w3c.dom.Node parent, String name,
        String value) {
    Element child = doc.createElementNS(namespace, name);
    child.appendChild(doc.createTextNode(value));
    parent.appendChild(child);
    return child;
}

From source file:DOMHelper.java

/**
 * Creates an element on the given document. Exceptions are as in
 * {@link Document#createElementNS(java.lang.String, java.lang.String)}. The
 * qualified name is obtained by {@code prefix}:{@code name} if the prefix
 * is not {@code null}./*w w w.j a va2 s  . com*/
 * 
 * @param doc
 *            the owner document
 * @param name
 *            the element's local name
 * @param prefix
 *            the element's prefix (may be {@code null})
 * @param namespaceURI
 *            the element's uri ({@code null} for no namespace)
 * @return the created element
 * 
 * @see Document#createElementNS(java.lang.String, java.lang.String)
 */
public static Element createElement(Document doc, String name, String prefix, String namespaceURI) {
    if (prefix != null)
        name = prefix + ":" + name;
    return doc.createElementNS(namespaceURI, name);
}

From source file:Main.java

public static Element createElement(final Document document, final String qName,
        final Optional<String> namespaceURI) {
    if (namespaceURI.isPresent()) {
        final Element element = document.createElementNS(namespaceURI.get(), qName);
        String name = XMLNS_ATTRIBUTE_KEY;
        if (element.getPrefix() != null) {
            name += ":" + element.getPrefix();
        }/*from www .j  av  a  2  s  .c o m*/
        element.setAttributeNS(XMLNS_URI, name, namespaceURI.get());
        return element;
    }
    return document.createElement(qName);
}

From source file:Main.java

public static void appendChild(Document document, Node root, String nsURI, String name, String value) {
    Node node = document.createElementNS(nsURI, name);
    node.appendChild(document.createTextNode(value != null ? value : ""));
    root.appendChild(node);//w w  w.  j a v  a  2 s. co m
    return;
}

From source file:Main.java

private static void convert(Node toCopy, Node saveTo, Document doc) {
    Node newNode;/* w  w w. j a v a2  s.  c o m*/
    switch (toCopy.getNodeType()) {
    case Node.ELEMENT_NODE:
        Element newElement = doc.createElementNS(toCopy.getNamespaceURI(), toCopy.getNodeName());
        newNode = newElement;
        Element baseElement = (Element) toCopy;
        NamedNodeMap children = baseElement.getAttributes();
        for (int i = 0; i < children.getLength(); i++) {
            convertAttribute((Attr) children.item(i), newElement, doc);
        }
        break;
    case Node.TEXT_NODE:
        newNode = doc.createTextNode(toCopy.getTextContent());
        break;
    default:
        newNode = null;
    }
    if (newNode != null) {
        NodeList children = toCopy.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            convert(children.item(i), newNode, doc);
        }

        saveTo.appendChild(newNode);
    }
}

From source file:Main.java

/**
 *  Create new XML document.//w w  w . j a  v a2 s .c o m
 *
 * @param  rootElementName  name of the root element to add, or <code>null</code> if the
 *      document should not have any root just yet
 * @throws ParserConfigurationException 
 */
public static Document createEmptyDocument(String rootElementName, String namespace)
        throws ParserConfigurationException {
    final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    final DocumentBuilder builder = factory.newDocumentBuilder();
    Document result = builder.newDocument();

    if (rootElementName != null) {
        Element rootElement;
        if (namespace != null && !namespace.isEmpty()) {
            rootElement = result.createElementNS(rootElementName, namespace);
        } else {
            rootElement = result.createElement(rootElementName);
        }
        result.appendChild(rootElement);
    }
    return result;
}

From source file:Main.java

public static Element createElement(Document document, String nsURI, String name, String value) {
    if (value != null) {
        Element element = (Element) document.createElementNS(nsURI, name);
        element.appendChild(document.createTextNode(value != null ? value : ""));
        return element;
    }//  w w w .j  av a  2s. co  m
    throw new IllegalArgumentException(
            "XMLDocumentUtils.createElement: value of " + name + " element can't be null.");
}