5

I am using StAX to construct an XML document using an XMLStreamWriter.

However, there are portions of my document where it is difficult to make calls to XMLStreamWriter's methods one-by-one, and it would be easier to construct a small document fragment using DOM, then write that out.

I know how to use DOM, but here's my question: is there an easy way to take an Element object and write it out to an XMLStreamWriter?

I can probably figure out how to "connect" the two methods, but it seems like it would be tedious & something should be out there already. (going the other way appears to be trivial: http://blogs.oracle.com/venu/entry/constructing_dom_using_stax_writers)

0

2 Answers 2

3

never mind, I got something that covers the basics (not sure it would work w/ namespaces but it seems to work w/ simple a DOM node tree):

public static void writeDomToXmlStreamWriter(Node node, XMLStreamWriter xmlw) throws XMLStreamException
{
    if (node != null)
    {
        switch (node.getNodeType())
        {
            case Node.ELEMENT_NODE:
            {
                Element element = (Element)node;
                if (element.getPrefix() != null)
                    xmlw.writeStartElement(element.getPrefix(), element.getLocalName(), element.getNamespaceURI());
                else if (element.getNamespaceURI() != null)
                    xmlw.writeStartElement(element.getNamespaceURI(), element.getLocalName());
                else if (element.getLocalName() != null)
                    xmlw.writeStartElement(element.getLocalName());
                else
                    xmlw.writeStartElement(element.getNodeName());
                writeDomAttributesToXmlStreamWriter(element, xmlw);
                for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling())
                    writeDomToXmlStreamWriter(child, xmlw);
                xmlw.writeEndElement();
            }
            break;
            case Node.TEXT_NODE:
            {
                xmlw.writeCharacters(node.getTextContent());
            }
        }
    }
}

private static void writeDomAttributesToXmlStreamWriter(Element element, XMLStreamWriter xmlw) throws XMLStreamException 
{
    NamedNodeMap map = element.getAttributes();
    for (int L = map.getLength(), i = 0; i < L; ++i)
    {
        Node attr = map.item(i);
        if (attr.getPrefix() != null)
            xmlw.writeAttribute(attr.getPrefix(), attr.getLocalName(), attr.getNamespaceURI(), attr.getNodeValue());
        else if (attr.getNamespaceURI() != null)
            xmlw.writeAttribute(attr.getNamespaceURI(), attr.getLocalName(), attr.getNodeValue());
        else if (attr.getLocalName() != null)
            xmlw.writeAttribute(attr.getLocalName(), attr.getNodeValue());
        else 
            xmlw.writeAttribute(attr.getNodeName(), attr.getNodeValue());
    }       
}
Sign up to request clarification or add additional context in comments.

Comments

0

You could use the javax.xml.transform libraries. The you could transform the DOM wrapped in a DOMSource onto a StAXResult wrapping your stream writer.

Essentially it is the write version of the approach described here:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.