Example usage for javax.xml.stream XMLStreamReader getPrefix

List of usage examples for javax.xml.stream XMLStreamReader getPrefix

Introduction

In this page you can find the example usage for javax.xml.stream XMLStreamReader getPrefix.

Prototype

public String getPrefix();

Source Link

Document

Returns the prefix of the current event or null if the event does not have a prefix

Usage

From source file:org.apache.axis2.jaxws.message.util.impl.SAAJConverterImpl.java

/**
 * update the tag data of the SOAPElement
 *
 * @param NameCreator nc/*from ww w.  j a  va  2 s.c  om*/
 * @param element     SOAPElement
 * @param reader      XMLStreamReader whose cursor is at START_ELEMENT
 */
protected void updateTagData(NameCreator nc, SOAPElement element, XMLStreamReader reader, boolean newElement)
        throws SOAPException {
    String prefix = reader.getPrefix();
    prefix = (prefix == null) ? "" : prefix;

    // Make sure the prefix is correct
    if (prefix.length() > 0 && !element.getPrefix().equals(prefix)) {
        // Due to a bug in Axiom DOM or in the reader...not sure where yet,
        // there may be a non-null prefix and no namespace
        String ns = reader.getNamespaceURI();
        if (ns != null && ns.length() != 0) {
            element.setPrefix(prefix);
        }

    }

    if (!newElement) {
        // Add the namespace declarations from the reader for the missing namespaces
        int size = reader.getNamespaceCount();
        for (int i = 0; i < size; i++) {
            String pre = reader.getNamespacePrefix(i);
            String ns = reader.getNamespaceURI(i);
            if ((pre != null && pre.length() > 0) && (ns == null || ns.length() == 0)) {
                if (log.isDebugEnabled()) {
                    log.debug("The prefix is (" + pre + ") but there is no namespace.  "
                            + "This erroneous declaration is skipped.");
                }
            } else {
                String existingNS = element.getNamespaceURI(pre);
                if (!ns.equals(existingNS)) {
                    element.removeNamespaceDeclaration(pre); // Is it necessary to remove the existing prefix/ns
                    element.addNamespaceDeclaration(pre, ns);
                }
            }
        }
    } else {
        // Add the namespace declarations from the reader
        int size = reader.getNamespaceCount();
        for (int i = 0; i < size; i++) {
            String newPrefix = reader.getNamespacePrefix(i);
            String newNS = reader.getNamespaceURI(i);

            if ((newPrefix != null && newPrefix.length() > 0) && (newNS == null || newNS.length() == 0)) {
                // Due to a bug in Axiom DOM or the reader, I have
                // seen cases where the prefix is non-null but there is not
                // namespace.  Example: prefix is axis2ns3 and namespace is null.
                // This is an error..log, tolerate and continue
                if (log.isDebugEnabled()) {
                    log.debug("The prefix is (" + newPrefix + ") but there is no namespace.  "
                            + "This erroneous declaration is skipped.");
                }
            } else {
                element.addNamespaceDeclaration(newPrefix, newNS);
            }
        }
    }

    addAttributes(nc, element, reader);

    return;
}

From source file:org.apache.ode.utils.DOMUtils.java

private static void parse(XMLStreamReader reader, Document doc, Node parent) throws XMLStreamException {
    int event = reader.getEventType();

    while (reader.hasNext()) {
        switch (event) {
        case XMLStreamConstants.START_ELEMENT:
            // create element
            Element e = doc.createElementNS(reader.getNamespaceURI(), reader.getLocalName());
            if (reader.getPrefix() != null && reader.getPrefix() != "") {
                e.setPrefix(reader.getPrefix());
            }//from  www  .j a  v  a 2  s  .c  o  m
            parent.appendChild(e);

            // copy namespaces
            for (int ns = 0; ns < reader.getNamespaceCount(); ns++) {
                String uri = reader.getNamespaceURI(ns);
                String prefix = reader.getNamespacePrefix(ns);
                declare(e, uri, prefix);
            }

            // copy attributes
            for (int att = 0; att < reader.getAttributeCount(); att++) {
                String name = reader.getAttributeLocalName(att);
                String prefix = reader.getAttributePrefix(att);
                if (prefix != null && prefix.length() > 0) {
                    name = prefix + ":" + name;
                }
                Attr attr = doc.createAttributeNS(reader.getAttributeNamespace(att), name);
                attr.setValue(reader.getAttributeValue(att));
                e.setAttributeNode(attr);
            }
            // sub-nodes
            if (reader.hasNext()) {
                reader.next();
                parse(reader, doc, e);
            }
            if (parent instanceof Document) {
                while (reader.hasNext())
                    reader.next();
                return;
            }
            break;
        case XMLStreamConstants.END_ELEMENT:
            return;
        case XMLStreamConstants.CHARACTERS:
            if (parent != null) {
                parent.appendChild(doc.createTextNode(reader.getText()));
            }
            break;
        case XMLStreamConstants.COMMENT:
            if (parent != null) {
                parent.appendChild(doc.createComment(reader.getText()));
            }
            break;
        case XMLStreamConstants.CDATA:
            parent.appendChild(doc.createCDATASection(reader.getText()));
            break;
        case XMLStreamConstants.PROCESSING_INSTRUCTION:
            parent.appendChild(doc.createProcessingInstruction(reader.getPITarget(), reader.getPIData()));
            break;
        case XMLStreamConstants.ENTITY_REFERENCE:
            parent.appendChild(doc.createProcessingInstruction(reader.getPITarget(), reader.getPIData()));
            break;
        case XMLStreamConstants.NAMESPACE:
        case XMLStreamConstants.ATTRIBUTE:
            break;
        default:
            break;
        }

        if (reader.hasNext()) {
            event = reader.next();
        }
    }
}

From source file:org.apache.synapse.commons.json.JsonDataSource.java

public void serialize(XMLStreamWriter xmlWriter) throws XMLStreamException {
    XMLStreamReader reader = getReader();
    xmlWriter.writeStartDocument();/*from   w w w .  j a  v  a  2  s . com*/
    while (reader.hasNext()) {
        int x = reader.next();
        switch (x) {
        case XMLStreamConstants.START_ELEMENT:
            xmlWriter.writeStartElement(reader.getPrefix(), reader.getLocalName(), reader.getNamespaceURI());
            int namespaceCount = reader.getNamespaceCount();
            for (int i = namespaceCount - 1; i >= 0; i--) {
                xmlWriter.writeNamespace(reader.getNamespacePrefix(i), reader.getNamespaceURI(i));
            }
            int attributeCount = reader.getAttributeCount();
            for (int i = 0; i < attributeCount; i++) {
                xmlWriter.writeAttribute(reader.getAttributePrefix(i), reader.getAttributeNamespace(i),
                        reader.getAttributeLocalName(i), reader.getAttributeValue(i));
            }
            break;
        case XMLStreamConstants.START_DOCUMENT:
            break;
        case XMLStreamConstants.CHARACTERS:
            xmlWriter.writeCharacters(reader.getText());
            break;
        case XMLStreamConstants.CDATA:
            xmlWriter.writeCData(reader.getText());
            break;
        case XMLStreamConstants.END_ELEMENT:
            xmlWriter.writeEndElement();
            break;
        case XMLStreamConstants.END_DOCUMENT:
            xmlWriter.writeEndDocument();
            break;
        case XMLStreamConstants.SPACE:
            break;
        case XMLStreamConstants.COMMENT:
            xmlWriter.writeComment(reader.getText());
            break;
        case XMLStreamConstants.DTD:
            xmlWriter.writeDTD(reader.getText());
            break;
        case XMLStreamConstants.PROCESSING_INSTRUCTION:
            xmlWriter.writeProcessingInstruction(reader.getPITarget(), reader.getPIData());
            break;
        case XMLStreamConstants.ENTITY_REFERENCE:
            xmlWriter.writeEntityRef(reader.getLocalName());
            break;
        default:
            throw new OMException();
        }
    }
    xmlWriter.writeEndDocument();
    xmlWriter.flush();
    xmlWriter.close();
}

From source file:org.deegree.client.core.renderer.OutputXMLRenderer.java

private void encodeXML(ResponseWriter writer, String value) throws IOException {
    int depth = 0;
    try {//www  .  j av  a 2 s.  c o  m
        XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(value));
        boolean lastWasEndElement = false;
        boolean lastWasComment = false;
        while (reader.hasNext()) {
            switch (reader.getEventType()) {
            case XMLStreamConstants.START_ELEMENT:
                if (!lastWasComment) {
                    writer.startElement("br", null);
                    writer.endElement("br");
                }
                writer.write(getSpaces(depth));
                writer.startElement("span", null);
                writer.writeAttribute("class", "sign", null);
                writer.write("&lt;");
                writer.endElement("span");

                writer.startElement("span", null);
                writer.writeAttribute("class", "tag", null);
                String prefix = reader.getPrefix();
                writer.write(
                        (prefix != null && prefix.length() > 0 ? prefix + ":" : "") + reader.getLocalName());
                writer.endElement("span");

                for (int i = 0; i < reader.getAttributeCount(); i++) {
                    writer.startElement("span", null);
                    writer.writeAttribute("class", "attributeName", null);
                    writer.write("&#160;");
                    String attributePrefix = reader.getAttributePrefix(i);
                    writer.write(
                            (attributePrefix != null && attributePrefix.length() > 0 ? attributePrefix + ":"
                                    : "") + reader.getAttributeLocalName(i));
                    writer.endElement("span");

                    writer.startElement("span", null);
                    writer.writeAttribute("class", "sign", null);
                    writer.write("=\"");
                    writer.endElement("span");

                    writer.startElement("span", null);
                    writer.writeAttribute("class", "text", null);
                    writer.write(encodeString(reader.getAttributeValue(i)));
                    writer.endElement("span");

                    writer.startElement("span", null);
                    writer.writeAttribute("class", "sign", null);
                    writer.write("\"");
                    writer.endElement("span");
                }

                for (int i = 0; i < reader.getNamespaceCount(); i++) {
                    writer.startElement("span", null);
                    writer.writeAttribute("class", "attributeName", null);
                    writer.write("&#160;");
                    String nsPrefix = reader.getNamespacePrefix(i);
                    writer.write("xmlns");
                    if (nsPrefix != null && !nsPrefix.isEmpty()) {
                        writer.write(":" + nsPrefix);
                    }
                    writer.endElement("span");

                    writer.startElement("span", null);
                    writer.writeAttribute("class", "sign", null);
                    writer.write("=\"");
                    writer.endElement("span");

                    writer.startElement("span", null);
                    writer.writeAttribute("class", "text", null);
                    writer.write(reader.getNamespaceURI(i));
                    writer.endElement("span");

                    writer.startElement("span", null);
                    writer.writeAttribute("class", "sign", null);
                    writer.write("\"");
                    writer.endElement("span");
                }

                writer.startElement("span", null);
                writer.writeAttribute("class", "sign", null);
                writer.write("&gt;");
                writer.endElement("span");
                depth++;
                lastWasEndElement = false;
                lastWasComment = false;
                break;
            case XMLStreamConstants.CHARACTERS:
                String text = reader.getText();
                if (text.trim().length() > 0) {
                    writer.startElement("span", null);
                    writer.writeAttribute("class", "text", null);
                    writer.write(encodeString(text));
                    writer.endElement("span");
                    lastWasEndElement = false;
                    lastWasComment = false;
                }
                break;
            case XMLStreamConstants.END_ELEMENT:
                depth--;
                if (lastWasEndElement) {
                    writer.startElement("br", null);
                    writer.endElement("br");
                    writer.write(getSpaces(depth));
                }
                writer.startElement("span", null);
                writer.writeAttribute("class", "sign", null);
                writer.write("&lt;/");
                writer.endElement("span");

                writer.startElement("span", null);
                writer.writeAttribute("class", "tag", null);
                prefix = reader.getPrefix();
                writer.write(
                        (prefix != null && prefix.length() > 0 ? prefix + ":" : "") + reader.getLocalName());
                writer.endElement("span");

                writer.startElement("span", null);
                writer.writeAttribute("class", "sign", null);
                writer.write("&gt;");
                writer.endElement("span");
                lastWasEndElement = true;
                lastWasComment = false;
                break;
            case XMLStreamConstants.COMMENT:
                writer.startElement("div", null);
                writer.writeAttribute("class", "comment", null);
                writer.write("&lt;/!--" + reader.getText() + "--&gt;");
                writer.endElement("div");
                lastWasEndElement = false;
                lastWasComment = true;
                break;
            default:
                break;
            }
            reader.next();

        }
        reader.close();
    } catch (Throwable e) {
        if (depth == 0) {
            writer.writeText("Response could not parsed as XML.", null);
        } else {
            writer.writeText("... (if you want the complete document, please click the download button)", null);
        }
    }
}

From source file:org.deegree.services.csw.exporthandling.GetCapabilitiesHelper.java

private void writeTemplateElement(XMLStreamWriter writer, XMLStreamReader inStream,
        Map<String, String> varToValue) throws XMLStreamException {

    if (inStream.getEventType() != XMLStreamConstants.START_ELEMENT) {
        throw new XMLStreamException("Input stream does not point to a START_ELEMENT event.");
    }/*from  w w  w  .j  a va 2  s. co  m*/
    int openElements = 0;
    boolean firstRun = true;
    while (firstRun || openElements > 0) {
        firstRun = false;
        int eventType = inStream.getEventType();

        switch (eventType) {
        case CDATA: {
            writer.writeCData(inStream.getText());
            break;
        }
        case CHARACTERS: {
            String s = new String(inStream.getTextCharacters(), inStream.getTextStart(),
                    inStream.getTextLength());
            // TODO optimize
            for (String param : varToValue.keySet()) {
                String value = varToValue.get(param);
                s = s.replace(param, value);
            }
            writer.writeCharacters(s);

            break;
        }
        case END_ELEMENT: {
            writer.writeEndElement();
            openElements--;
            break;
        }
        case START_ELEMENT: {
            if (inStream.getNamespaceURI() == "" || inStream.getPrefix() == DEFAULT_NS_PREFIX
                    || inStream.getPrefix() == null) {
                writer.writeStartElement(inStream.getLocalName());
            } else {
                if (writer.getNamespaceContext().getPrefix(inStream.getPrefix()) == "") {
                    // TODO handle special cases for prefix binding, see
                    // http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/api/javax/xml/namespace/NamespaceContext.html#getNamespaceURI(java.lang.String)
                    writer.setPrefix(inStream.getPrefix(), inStream.getNamespaceURI());
                }
                writer.writeStartElement(inStream.getPrefix(), inStream.getLocalName(),
                        inStream.getNamespaceURI());
            }
            // copy all namespace bindings
            for (int i = 0; i < inStream.getNamespaceCount(); i++) {
                String nsPrefix = inStream.getNamespacePrefix(i);
                String nsURI = inStream.getNamespaceURI(i);
                writer.writeNamespace(nsPrefix, nsURI);
            }

            // copy all attributes
            for (int i = 0; i < inStream.getAttributeCount(); i++) {
                String localName = inStream.getAttributeLocalName(i);
                String nsPrefix = inStream.getAttributePrefix(i);
                String value = inStream.getAttributeValue(i);
                String nsURI = inStream.getAttributeNamespace(i);
                if (nsURI == null) {
                    writer.writeAttribute(localName, value);
                } else {
                    writer.writeAttribute(nsPrefix, nsURI, localName, value);
                }
            }

            openElements++;
            break;
        }
        default: {
            break;
        }
        }
        if (openElements > 0) {
            inStream.next();
        }
    }
}

From source file:org.flowable.bpmn.converter.BaseBpmnXMLConverter.java

@SuppressWarnings("unchecked")
protected ExtensionElement parseExtensionElement(XMLStreamReader xtr) throws Exception {
    ExtensionElement extensionElement = new ExtensionElement();
    extensionElement.setName(xtr.getLocalName());
    if (StringUtils.isNotEmpty(xtr.getNamespaceURI())) {
        extensionElement.setNamespace(xtr.getNamespaceURI());
    }/*  ww  w  .  j a  va2  s.c  o  m*/
    if (StringUtils.isNotEmpty(xtr.getPrefix())) {
        extensionElement.setNamespacePrefix(xtr.getPrefix());
    }

    BpmnXMLUtil.addCustomAttributes(xtr, extensionElement, defaultElementAttributes);

    boolean readyWithExtensionElement = false;
    while (readyWithExtensionElement == false && xtr.hasNext()) {
        xtr.next();
        if (xtr.isCharacters() || XMLStreamReader.CDATA == xtr.getEventType()) {
            if (StringUtils.isNotEmpty(xtr.getText().trim())) {
                extensionElement.setElementText(xtr.getText().trim());
            }
        } else if (xtr.isStartElement()) {
            ExtensionElement childExtensionElement = parseExtensionElement(xtr);
            extensionElement.addChildElement(childExtensionElement);
        } else if (xtr.isEndElement() && extensionElement.getName().equalsIgnoreCase(xtr.getLocalName())) {
            readyWithExtensionElement = true;
        }
    }
    return extensionElement;
}

From source file:org.mule.module.xml.util.XMLUtils.java

private static void writeStartElement(XMLStreamReader reader, XMLStreamWriter writer)
        throws XMLStreamException {
    String local = reader.getLocalName();
    String uri = reader.getNamespaceURI();
    String prefix = reader.getPrefix();
    if (prefix == null) {
        prefix = "";
    }/*from   w w  w. j  av  a  2 s . c  o m*/

    //        System.out.println("STAXUTILS:writeStartElement : node name : " + local +  " namespace URI" + uri);
    boolean writeElementNS = false;
    if (uri != null) {
        String boundPrefix = writer.getPrefix(uri);
        if (boundPrefix == null || !prefix.equals(boundPrefix)) {
            writeElementNS = true;
        }
    }

    // Write out the element name
    if (uri != null) {
        if (prefix.length() == 0 && StringUtils.isEmpty(uri)) {
            writer.writeStartElement(local);
            writer.setDefaultNamespace(uri);

        } else {
            writer.writeStartElement(prefix, local, uri);
            writer.setPrefix(prefix, uri);
        }
    } else {
        writer.writeStartElement(local);
    }

    // Write out the namespaces
    for (int i = 0; i < reader.getNamespaceCount(); i++) {
        String nsURI = reader.getNamespaceURI(i);
        String nsPrefix = reader.getNamespacePrefix(i);
        if (nsPrefix == null) {
            nsPrefix = "";
        }

        if (nsPrefix.length() == 0) {
            writer.writeDefaultNamespace(nsURI);
        } else {
            writer.writeNamespace(nsPrefix, nsURI);
        }

        if (nsURI.equals(uri) && nsPrefix.equals(prefix)) {
            writeElementNS = false;
        }
    }

    // Check if the namespace still needs to be written.
    // We need this check because namespace writing works
    // different on Woodstox and the RI.
    if (writeElementNS) {
        if (prefix.length() == 0) {
            writer.writeDefaultNamespace(uri);
        } else {
            writer.writeNamespace(prefix, uri);
        }
    }

    // Write out attributes
    for (int i = 0; i < reader.getAttributeCount(); i++) {
        String ns = reader.getAttributeNamespace(i);
        String nsPrefix = reader.getAttributePrefix(i);
        if (ns == null || ns.length() == 0) {
            writer.writeAttribute(reader.getAttributeLocalName(i), reader.getAttributeValue(i));
        } else if (nsPrefix == null || nsPrefix.length() == 0) {
            writer.writeAttribute(reader.getAttributeNamespace(i), reader.getAttributeLocalName(i),
                    reader.getAttributeValue(i));
        } else {
            writer.writeAttribute(reader.getAttributePrefix(i), reader.getAttributeNamespace(i),
                    reader.getAttributeLocalName(i), reader.getAttributeValue(i));
        }

    }
}

From source file:org.netbeans.jbatch.modeler.spec.core.Definitions.java

static void transformXMLStream(XMLStreamReader xmlStreamReader, XMLStreamWriter xmlStreamWriter) {
    try {/*from w  ww .  j  a v  a  2  s  . c  o  m*/
        //            TransformerFactory tf = TransformerFactory.newInstance();
        //            Transformer t = tf.newTransformer();
        //            StAXSource source = new StAXSource(xmlStreamReader);
        //            StAXResult result = new StAXResult(xmlStreamWriter);
        //            t.transform(source, result);
        System.out.println("Defnition Id : " + xmlStreamReader.getAttributeValue(null, "id"));
        boolean finish = false;
        while (xmlStreamReader.hasNext() && !finish) {
            switch (xmlStreamReader.getEventType()) {
            case XMLEvent.START_ELEMENT:
                String prefix = xmlStreamReader.getPrefix();
                String namespaceURI = xmlStreamReader.getNamespaceURI();
                if (namespaceURI != null) {
                    if (prefix != null) {
                        xmlStreamWriter.writeStartElement(xmlStreamReader.getPrefix(),
                                xmlStreamReader.getLocalName(), xmlStreamReader.getNamespaceURI());
                    } else {
                        xmlStreamWriter.writeStartElement(xmlStreamReader.getNamespaceURI(),
                                xmlStreamReader.getLocalName());
                    }
                } else {
                    xmlStreamWriter.writeStartElement(xmlStreamReader.getLocalName());
                }

                for (int i = 0; i < xmlStreamReader.getNamespaceCount(); i++) {
                    xmlStreamWriter.writeNamespace(xmlStreamReader.getNamespacePrefix(i),
                            xmlStreamReader.getNamespaceURI(i));
                }
                int count = xmlStreamReader.getAttributeCount();
                for (int i = 0; i < count; i++) {
                    //                            xmlStreamWriter.writeAttribute(xmlStreamReader.getAttributePrefix(i),
                    //                                    xmlStreamReader.getAttributeNamespace(i),
                    //                                    xmlStreamReader.getAttributeLocalName(i),
                    //                                    xmlStreamReader.getAttributeValue(i));

                    String attrNamespaceURI = xmlStreamReader.getAttributeNamespace(i),
                            attrPrefix = xmlStreamReader.getAttributePrefix(i);
                    if (attrNamespaceURI != null) {
                        if (attrPrefix != null) {
                            xmlStreamWriter.writeAttribute(attrPrefix, attrNamespaceURI,
                                    xmlStreamReader.getAttributeLocalName(i),
                                    xmlStreamReader.getAttributeValue(i));
                        } else {
                            xmlStreamWriter.writeAttribute(attrNamespaceURI,
                                    xmlStreamReader.getAttributeLocalName(i),
                                    xmlStreamReader.getAttributeValue(i));
                        }
                    } else {
                        xmlStreamWriter.writeAttribute(xmlStreamReader.getAttributeLocalName(i),
                                xmlStreamReader.getAttributeValue(i));
                    }

                }
                break;
            case XMLEvent.END_ELEMENT:
                xmlStreamWriter.writeEndElement();
                if (xmlStreamReader.getLocalName().equals("definitions")) {
                    finish = true;
                }
                break;
            case XMLEvent.SPACE:
            case XMLEvent.CHARACTERS:
                xmlStreamWriter.writeCharacters(xmlStreamReader.getTextCharacters(),
                        xmlStreamReader.getTextStart(), xmlStreamReader.getTextLength());
                break;
            case XMLEvent.PROCESSING_INSTRUCTION:
                xmlStreamWriter.writeProcessingInstruction(xmlStreamReader.getPITarget(),
                        xmlStreamReader.getPIData());
                break;
            case XMLEvent.CDATA:
                xmlStreamWriter.writeCData(xmlStreamReader.getText());
                break;

            case XMLEvent.COMMENT:
                xmlStreamWriter.writeComment(xmlStreamReader.getText());
                break;
            case XMLEvent.ENTITY_REFERENCE:
                xmlStreamWriter.writeEntityRef(xmlStreamReader.getLocalName());
                break;
            case XMLEvent.START_DOCUMENT:
                String encoding = xmlStreamReader.getCharacterEncodingScheme();
                String version = xmlStreamReader.getVersion();

                if (encoding != null && version != null) {
                    xmlStreamWriter.writeStartDocument(encoding, version);
                } else if (version != null) {
                    xmlStreamWriter.writeStartDocument(xmlStreamReader.getVersion());
                }
                break;
            case XMLEvent.END_DOCUMENT:
                xmlStreamWriter.writeEndDocument();
                break;
            case XMLEvent.DTD:
                xmlStreamWriter.writeDTD(xmlStreamReader.getText());
                break;

            }
            if (!finish) {
                xmlStreamReader.next();
            }
        }
    } catch (XMLStreamException ex) {
        Exceptions.printStackTrace(ex);
    }
}

From source file:org.netbeans.jbatch.modeler.specification.model.job.util.JobUtil.java

void transformXMLStream(XMLStreamReader xmlStreamReader, XMLStreamWriter xmlStreamWriter) {
    try {/*from w w  w . ja va2 s  .com*/
        //            TransformerFactory tf = TransformerFactory.newInstance();
        //            Transformer t = tf.newTransformer();
        //            StAXSource source = new StAXSource(xmlStreamReader);
        //            StAXResult result = new StAXResult(xmlStreamWriter);
        //            t.transform(source, result);

        boolean finish = false;
        while (xmlStreamReader.hasNext() && !finish) {
            switch (xmlStreamReader.getEventType()) {
            case XMLEvent.START_ELEMENT:
                String prefix = xmlStreamReader.getPrefix();
                String namespaceURI = xmlStreamReader.getNamespaceURI();
                if (namespaceURI != null) {
                    if (prefix != null) {
                        xmlStreamWriter.writeStartElement(xmlStreamReader.getPrefix(),
                                xmlStreamReader.getLocalName(), xmlStreamReader.getNamespaceURI());
                    } else {
                        xmlStreamWriter.writeStartElement(xmlStreamReader.getNamespaceURI(),
                                xmlStreamReader.getLocalName());
                    }
                } else {
                    xmlStreamWriter.writeStartElement(xmlStreamReader.getLocalName());
                }

                for (int i = 0; i < xmlStreamReader.getNamespaceCount(); i++) {
                    xmlStreamWriter.writeNamespace(xmlStreamReader.getNamespacePrefix(i),
                            xmlStreamReader.getNamespaceURI(i));
                }
                int count = xmlStreamReader.getAttributeCount();
                for (int i = 0; i < count; i++) {
                    //                            xmlStreamWriter.writeAttribute(xmlStreamReader.getAttributePrefix(i),
                    //                                    xmlStreamReader.getAttributeNamespace(i),
                    //                                    xmlStreamReader.getAttributeLocalName(i),
                    //                                    xmlStreamReader.getAttributeValue(i));

                    String attrNamespaceURI = xmlStreamReader.getAttributeNamespace(i),
                            attrPrefix = xmlStreamReader.getAttributePrefix(i);
                    if (attrNamespaceURI != null) {
                        if (attrPrefix != null) {
                            xmlStreamWriter.writeAttribute(attrPrefix, attrNamespaceURI,
                                    xmlStreamReader.getAttributeLocalName(i),
                                    xmlStreamReader.getAttributeValue(i));
                        } else {
                            xmlStreamWriter.writeAttribute(attrNamespaceURI,
                                    xmlStreamReader.getAttributeLocalName(i),
                                    xmlStreamReader.getAttributeValue(i));
                        }
                    } else {
                        xmlStreamWriter.writeAttribute(xmlStreamReader.getAttributeLocalName(i),
                                xmlStreamReader.getAttributeValue(i));
                    }

                }
                break;
            case XMLEvent.END_ELEMENT:
                xmlStreamWriter.writeEndElement();
                if (xmlStreamReader.getLocalName().equals("definitions")) {
                    finish = true;
                }
                break;
            case XMLEvent.SPACE:
            case XMLEvent.CHARACTERS:
                xmlStreamWriter.writeCharacters(xmlStreamReader.getTextCharacters(),
                        xmlStreamReader.getTextStart(), xmlStreamReader.getTextLength());
                break;
            case XMLEvent.PROCESSING_INSTRUCTION:
                xmlStreamWriter.writeProcessingInstruction(xmlStreamReader.getPITarget(),
                        xmlStreamReader.getPIData());
                break;
            case XMLEvent.CDATA:
                xmlStreamWriter.writeCData(xmlStreamReader.getText());
                break;

            case XMLEvent.COMMENT:
                xmlStreamWriter.writeComment(xmlStreamReader.getText());
                break;
            case XMLEvent.ENTITY_REFERENCE:
                xmlStreamWriter.writeEntityRef(xmlStreamReader.getLocalName());
                break;
            case XMLEvent.START_DOCUMENT:
                String encoding = xmlStreamReader.getCharacterEncodingScheme();
                String version = xmlStreamReader.getVersion();

                if (encoding != null && version != null) {
                    xmlStreamWriter.writeStartDocument(encoding, version);
                } else if (version != null) {
                    xmlStreamWriter.writeStartDocument(xmlStreamReader.getVersion());
                }
                break;
            case XMLEvent.END_DOCUMENT:
                xmlStreamWriter.writeEndDocument();
                break;
            case XMLEvent.DTD:
                xmlStreamWriter.writeDTD(xmlStreamReader.getText());
                break;

            }
            if (!finish) {
                xmlStreamReader.next();
            }
        }
    } catch (XMLStreamException ex) {
        Exceptions.printStackTrace(ex);
    }
}

From source file:org.osaf.cosmo.xml.DomReader.java

private static Element readElement(Document d, XMLStreamReader reader) throws XMLStreamException {
    Element e = null;//from ww  w .  jav a2 s.  c om

    String local = reader.getLocalName();
    String ns = reader.getNamespaceURI();
    if (ns != null) {
        String prefix = reader.getPrefix();
        String qualified = prefix != null ? prefix + ":" + local : local;
        e = d.createElementNS(ns, qualified);
    } else {
        e = d.createElement(local);
    }

    //if (log.isDebugEnabled())
    //log.debug("Reading element " + e.getTagName());

    for (int i = 0; i < reader.getAttributeCount(); i++) {
        Attr a = readAttribute(i, d, reader);
        if (a.getNamespaceURI() != null)
            e.setAttributeNodeNS(a);
        else
            e.setAttributeNode(a);
    }

    return e;
}