Example usage for javax.xml XMLConstants XMLNS_ATTRIBUTE_NS_URI

List of usage examples for javax.xml XMLConstants XMLNS_ATTRIBUTE_NS_URI

Introduction

In this page you can find the example usage for javax.xml XMLConstants XMLNS_ATTRIBUTE_NS_URI.

Prototype

String XMLNS_ATTRIBUTE_NS_URI

To view the source code for javax.xml XMLConstants XMLNS_ATTRIBUTE_NS_URI.

Click Source Link

Document

The official XML attribute used for specifying XML Namespace declarations, #XMLNS_ATTRIBUTE XMLConstants.XMLNS_ATTRIBUTE , Namespace name URI.

Usage

From source file:Utils.java

/**
 * Add a namespace prefix definition to an element.
 * //from   w w  w. ja v  a  2 s. co m
 * @param element
 * @param namespaceUri
 * @param prefix
 */
public static void addNamespacePrefix(Element element, String namespaceUri, String prefix) {
    element.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns:" + prefix, namespaceUri);
}

From source file:Main.java

/**
 * Sets the namespace to specific element.
 *
 * @param element//from  ww  w  . j  ava 2s .co m
 *          the element to set
 * @param namespace
 *          the namespace to set
 * @param schemaLocation
 *          the XML schema file location URI
 */
public static void setNamespace(Element element, String namespace, String schemaLocation) {
    element.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, XMLConstants.XMLNS_ATTRIBUTE, namespace);
    element.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, XMLNS_XSI,
            XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);
    element.setAttributeNS(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, XSI_SCHEMA_LOCATION, schemaLocation);
}

From source file:Utils.java

/**
 * Set a namespace/prefix on an element if it is not set already. First off, it searches for the element
 * for the prefix associated with the specified namespace. If the prefix isn't null, then this is
 * returned. Otherwise, it creates a new attribute using the namespace/prefix passed as parameters.
 * //from  w w w.  j a va2  s.c  om
 * @param element
 * @param namespace
 * @param prefix
 * @return the prefix associated with the set namespace
 */
public static String setNamespace(Element element, String namespace, String prefix) {
    String pre = getPrefixRecursive(element, namespace);
    if (pre != null) {
        return pre;
    }
    element.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns:" + prefix, namespace);
    return prefix;
}

From source file:MainClass.java

public SimpleNamespaceContext() {
    addNamespace(XMLConstants.XML_NS_PREFIX, XMLConstants.XML_NS_URI);
    addNamespace(XMLConstants.XMLNS_ATTRIBUTE, XMLConstants.XMLNS_ATTRIBUTE_NS_URI);
}

From source file:Main.java

private static Document documentFromSoapBody(SOAPBodyElement element,
        HashMap<String, String> namespaceDeclarations) throws ParserConfigurationException {
    Document document = dbf.newDocumentBuilder().newDocument();
    Node node = document.importNode(element, true);
    document.appendChild(node);//from   ww  w . j  av  a  2s . com

    for (String prefix : namespaceDeclarations.keySet()) {
        String uri = namespaceDeclarations.get(prefix);
        if (node.lookupNamespaceURI(prefix) == null) {
            document.getDocumentElement().setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns:" + prefix,
                    uri);
        }
    }
    return document;
}

From source file:com.googlecode.ddom.frontend.saaj.SOAPElementTest.java

/**
 * Test that a namespace declaration added using
 * {@link SOAPElement#addNamespaceDeclaration(String, String)} can be retrieved using
 * {@link Element#getAttributeNS(String, String)}.
 * /*from   w  w w . ja v a 2s  .  c o  m*/
 * @throws SOAPException
 */
@Validated
@Test
public void testAddNamespaceDeclarationNonDefaultNamespace() throws SOAPException {
    SOAPElement element = saajUtil.createSOAPElement("urn:test", "test", "p");
    element.addNamespaceDeclaration("ns", "urn:ns");
    assertEquals("urn:ns", element.getAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "ns"));
}

From source file:com.amalto.core.history.accessor.AttributeAccessor.java

private Attr createAttribute(Node parentNode, Document domDocument) {
    // Ensure xsi prefix is declared
    if (attributeName.indexOf(':') > 0) {
        String attributePrefix = StringUtils.substringBefore(attributeName, ":"); //$NON-NLS-1$
        String namespaceURI = domDocument.lookupNamespaceURI(attributePrefix);
        if (namespaceURI == null) {
            if ("xsi".equals(attributePrefix)) { //$NON-NLS-1$
                domDocument.getDocumentElement().setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI,
                        "xmlns:xsi", XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI); //$NON-NLS-1$
            } else if ("tmdm".equals(attributePrefix)) { //$NON-NLS-1$
                domDocument.getDocumentElement().setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI,
                        "xmlns:tmdm", SkipAttributeDocumentBuilder.TALEND_NAMESPACE); //$NON-NLS-1$
            } else {
                throw new IllegalArgumentException("Unrecognized attribute prefix: '" + attributePrefix + "'."); //$NON-NLS-1$ //$NON-NLS-2$
            }/*w ww  .j  a  v  a  2  s  . c o m*/
        }
    }
    QName qName = getQName(domDocument);
    Attr newAttribute = domDocument.createAttributeNS(qName.getNamespaceURI(), qName.getLocalPart());
    String prefix = qName.getPrefix();
    newAttribute.setPrefix(prefix);
    parentNode.getAttributes().setNamedItemNS(newAttribute);
    return newAttribute;
}

From source file:com.evolveum.midpoint.cli.common.ToolsUtils.java

public static void setNamespaceDeclaration(Element element, String prefix, String namespaceUri) {
    Document doc = element.getOwnerDocument();
    NamedNodeMap attributes = element.getAttributes();
    Attr attr;//from w w w  . j a  v a 2  s  . co  m
    if (prefix == null || prefix.isEmpty()) {
        // default namespace
        attr = doc.createAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, XMLConstants.XMLNS_ATTRIBUTE);
    } else {
        attr = doc.createAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI,
                XMLConstants.XMLNS_ATTRIBUTE + ":" + prefix);
    }
    checkValidXmlChars(namespaceUri);
    attr.setValue(namespaceUri);
    attributes.setNamedItem(attr);
}

From source file:com.googlecode.ddom.frontend.saaj.SOAPElementTest.java

@Validated
@Test//from  ww w.  jav a  2 s.  c o m
public void testAddNamespaceDeclarationDefaultNamespace() throws SOAPException {
    SOAPElement element = saajUtil.createSOAPElement("urn:test", "test", "p");
    element.addNamespaceDeclaration("", "urn:ns");
    assertEquals("urn:ns", element.getAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns"));
}

From source file:com.googlecode.ddom.frontend.saaj.SOAPElementTest.java

@Validated
@Test/*from   w  w w .j a  v  a  2s. c  om*/
public void testRemoveNamespaceDeclaration() throws SOAPException {
    SOAPElement element = saajUtil.createSOAPElement(null, "test", null);
    element.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns:p", "urn:ns");
    assertTrue(element.removeNamespaceDeclaration("p"));
    assertEquals(0, element.getAttributes().getLength());
}