Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4018504
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:59:27+00:00 2026-05-20T09:59:27+00:00

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

  • 0

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)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T09:59:28+00:00Added an answer on May 20, 2026 at 9:59 am

    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());
        }       
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible using StAX (specifically woodstox) to format the output xml with newlines
I'm using the StAX event based API's to modify an XML stream. The stream
Using ASP.NET MVC there are situations (such as form submission) that may require a
Using C# and System.Data.SqlClient, is there a way to retrieve a list of parameters
Using VS2008, C#, .Net 2 and Winforms how can I make a regular Button
using CodeIgniter normally one has to specify the controllers in the config/routes.php file. This
Using javascript, one of the things I need to do is when a button
using Core Data, how would I list (i.e. return an NSArray of NSStrings) all
Using online interfaces to a version control system is a nice way to have
Using PyObjC , you can use Python to write Cocoa applications for OS X.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.