Example usage for javax.xml.namespace QName QName

List of usage examples for javax.xml.namespace QName QName

Introduction

In this page you can find the example usage for javax.xml.namespace QName QName.

Prototype

public QName(String localPart) 

Source Link

Document

QName constructor specifying the local part.

If the local part is null an IllegalArgumentException is thrown.

Usage

From source file:Main.java

public static XMLEvent ee(String name) {
    return eventFactory.createEndElement(new QName(name), null);
}

From source file:Main.java

public static XMLEvent se(String name) {
    return eventFactory.createStartElement(new QName(name), null, null);
}

From source file:Main.java

public static StartElement se(String name) {
    return eventFactory.createStartElement(new QName(name), null, null);
}

From source file:Main.java

public static QName getXmlQName(final NamespaceContext context, final String value) {
    if (value == null) {
        return null;
    } else {//from w  w  w . j  a  v a2 s .  c  om
        final int colonIndex = value.indexOf(':');
        if (colonIndex == -1) {
            return new QName(value);
        } else {
            final String prefix = value.substring(0, colonIndex);
            final String name = value.substring(colonIndex + 1);
            final String namespaceUri = context.getNamespaceURI(prefix);
            return new QName(namespaceUri, name, prefix);
        }
    }
}

From source file:Main.java

public static void parseBeanToXmlFileByJAXB(String path, Object bean, Class clase) throws Exception {
    JAXBContext jc = JAXBContext.newInstance(clase);
    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    JAXBElement<Object> rootElement = new JAXBElement<Object>(new QName(clase.getSimpleName()), clase, bean);
    marshaller.marshal(rootElement, new FileOutputStream(path));
}

From source file:Main.java

public static String parseBeanToXmlStringByJAXB(Object bean, Class clase) throws Exception {
    JAXBContext jc = JAXBContext.newInstance(clase);
    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);
    JAXBElement<Object> rootElement = new JAXBElement<Object>(new QName(clase.getSimpleName()), clase, bean);

    OutputStream output = new OutputStream() {

        private StringBuilder string = new StringBuilder();

        public void write(int b) throws IOException {
            this.string.append((char) b);
        }/*  w  ww  . j  av a 2 s .  c o  m*/

        //Netbeans IDE automatically overrides this toString()
        public String toString() {
            return this.string.toString();
        }
    };

    marshaller.marshal(rootElement, output);

    return output.toString();

}

From source file:Main.java

public static String object2Xml(Object obj) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(obj.getClass());
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);

    StringWriter writer = new StringWriter();
    marshaller.marshal(new JAXBElement(new QName("xml"), obj.getClass(), obj), writer);

    return writer.toString();
}

From source file:Main.java

public static QName convertStringToQName(String expandedQName, String prefix) {
    int ind1 = expandedQName.indexOf('{');
    if (ind1 != 0) {
        return new QName(expandedQName);
    }/*w w  w  .  ja v  a2s .  com*/

    int ind2 = expandedQName.indexOf('}');
    if (ind2 <= ind1 + 1 || ind2 >= expandedQName.length() - 1) {
        return null;
    }
    String ns = expandedQName.substring(ind1 + 1, ind2);
    String localName = expandedQName.substring(ind2 + 1);
    return new QName(ns, localName, prefix);
}

From source file:Main.java

/**
 * @param xml//ww w.  j a  va  2s. c  o  m
 *            The Element whose QName will be returned.
 * @return The QName of the given Element definition.
 */
public static QName getElementQName(final Element xml) {
    final String uri = xml.getNamespaceURI();
    final String prefix = xml.getPrefix();
    final String name = xml.getLocalName();

    //
    // support for DOM Level 1 - no NS concept
    //
    if (name == null)
        return new QName(xml.getNodeName());

    //
    // prefix is not required, but it CANNOT be null
    //
    if (prefix != null && prefix.length() > 0)
        return new QName(uri, name, prefix);

    return new QName(uri, name);
}

From source file:Main.java

/**
 * Get the QName of the passed element. If the passed element has no namespace
 * URI, only the tag name is used. Otherwise namespace URI, local name and
 * prefix are used.//w  ww  .ja v a 2  s  .  co m
 *
 * @param aElement
 *        The element to be used. May not be <code>null</code>.
 * @return The created {@link QName}.
 * @since 8.4.1
 */
@Nonnull
public static QName getQName(@Nonnull final Element aElement) {
    final String sNamespaceURI = aElement.getNamespaceURI();
    if (sNamespaceURI == null)
        return new QName(aElement.getTagName());
    return new QName(sNamespaceURI, aElement.getLocalName(), getPrefix(aElement));
}