Java Code Examples for org.jdom.Element#getNamespaceURI()

The following examples show how to use org.jdom.Element#getNamespaceURI() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JDOMNodePointer.java    From commons-jxpath with Apache License 2.0 5 votes vote down vote up
/**
 * Get the ns uri of the specified node.
 * @param node Node to check
 * @return String
 */
private static String getNamespaceURI(Object node) {
    if (node instanceof Element) {
        Element element = (Element) node;
        String ns = element.getNamespaceURI();
        if ("".equals(ns)) {
            ns = null;
        }
        return ns;
    }
    return null;
}
 
Example 2
Source File: JDOMStreamReader.java    From cxf with Apache License 2.0 4 votes vote down vote up
public QName getName() {
    Element el = getCurrentElement();

    return new QName(el.getNamespaceURI(), el.getName(), el.getNamespacePrefix());
}
 
Example 3
Source File: StaxSerializer.java    From cxf with Apache License 2.0 4 votes vote down vote up
public void writeElement(Element e, XMLStreamWriter writer) throws XMLStreamException {
    // need to check if the namespace is declared before we write the
    // start element because that will put the namespace in the context.
    String elPrefix = e.getNamespacePrefix();
    String elUri = e.getNamespaceURI();

    String boundPrefix = writer.getPrefix(elUri);
    boolean writeElementNS = false;
    if (boundPrefix == null || !elPrefix.equals(boundPrefix)) {
        writeElementNS = true;
    }

    writer.writeStartElement(elPrefix, e.getName(), elUri);

    List<?> namespaces = e.getAdditionalNamespaces();
    for (Iterator<?> itr = namespaces.iterator(); itr.hasNext();) {
        Namespace ns = (Namespace)itr.next();

        String prefix = ns.getPrefix();
        String uri = ns.getURI();

        writer.setPrefix(prefix, uri);
        writer.writeNamespace(prefix, uri);

        if (elUri.equals(uri) && elPrefix.equals(prefix)) {
            writeElementNS = false;
        }
    }

    for (Iterator<?> itr = e.getAttributes().iterator(); itr.hasNext();) {
        Attribute attr = (Attribute)itr.next();
        String attPrefix = attr.getNamespacePrefix();
        String attUri = attr.getNamespaceURI();

        if (attUri == null || attUri.isEmpty()) {
            writer.writeAttribute(attr.getName(), attr.getValue());
        } else {
            writer.writeAttribute(attPrefix, attUri, attr.getName(), attr.getValue());

            if (!isDeclared(writer, attPrefix, attUri)) {
                if (elUri.equals(attUri) && elPrefix.equals(attPrefix)) {
                    if (writeElementNS) {
                        writer.setPrefix(attPrefix, attUri);
                        writer.writeNamespace(attPrefix, attUri);
                        writeElementNS = false;
                    }
                } else {
                    writer.setPrefix(attPrefix, attUri);
                    writer.writeNamespace(attPrefix, attUri);
                }
            }
        }
    }

    if (writeElementNS) {
        if (elPrefix == null || elPrefix.length() == 0) {
            writer.writeDefaultNamespace(elUri);
        } else {
            writer.writeNamespace(elPrefix, elUri);
        }
    }

    for (Iterator<?> itr = e.getContent().iterator(); itr.hasNext();) {
        Content n = (Content)itr.next();
        if (n instanceof CDATA) {
            writer.writeCData(n.getValue());
        } else if (n instanceof Text) {
            writer.writeCharacters(((Text)n).getText());
        } else if (n instanceof Element) {
            writeElement((Element)n, writer);
        } else if (n instanceof Comment) {
            writer.writeComment(n.getValue());
        } else if (n instanceof EntityRef) {
            // EntityRef ref = (EntityRef) n;
            // writer.writeEntityRef(ref.)
        }
    }

    writer.writeEndElement();
}