Search code examples
javaxmldomstax

Java XML: using DOM with StAX to construct a document


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)


Solution

  • 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());
        }       
    }