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

  • SEARCH
  • Home
  • 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 7961271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:59:42+00:00 2026-06-04T04:59:42+00:00

I am trying to convert some code from using DOM (via jDOM) to use

  • 0

I am trying to convert some code from using DOM (via jDOM) to use StAX instead. At the same time I am migrating from DTD-based validation to XSD_based validation. Oh, and just for good measure I am introducing JAXB into the equation 🙂

Anyway, as an interim migration step I would like to allow users to still provide legacy documents (aka, using DTD and therefore no namespace). I will still validate the document using XSD, so the DTD is ignored. This works except that StAX (nor JAXB) seems to not like the non-namespaced document. I tried disabling namespace support (using javax.xml.stream.isNamespaceAware), but that did not have any effect. Explicitly adding xmlns to the document root fixed the problem, so I am fairly confident it is a namespacing issue.

Is there a way using StAX XMLEventReader to “introduce” a default namespace? Something along the lines of this approach (which is SAX specific), but for StAX…

Or any other ideas on how to achieve that?

An example document looks like:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.hibernate.test.abstractembeddedcomponents.cid">
    ...
</hibernate-mapping>

The code I am currently using to read these documents is:

public JaxbRoot unmarshal(InputStream stream, Origin origin) {
    try {
        XMLEventReader staxReader = staxFactory().createXMLEventReader( stream );
        try {
            return unmarshal( staxReader, origin );
        }
        finally {
            try {
                staxReader.close();
            }
            catch ( Exception ignore ) {
            }
        }
    }
    catch ( XMLStreamException e ) {
        throw new MappingException( "Unable to create stax reader", e, origin );
    }
}

private XMLInputFactory staxFactory;

private XMLInputFactory staxFactory() {
    if ( staxFactory == null ) {
        staxFactory = buildStaxFactory();
    }
    return staxFactory;
}

@SuppressWarnings( { "UnnecessaryLocalVariable" })
private XMLInputFactory buildStaxFactory() {
    XMLInputFactory staxFactory = XMLInputFactory.newInstance();
    // tried with and without, no effect
    //staxFactory.setProperty( "javax.xml.stream.isNamespaceAware", false );
    return staxFactory;
}

@SuppressWarnings( { "unchecked" })
private JaxbRoot unmarshal(XMLEventReader staxEventReader, final Origin origin) {
    XMLEvent event;
    try {
        event = staxEventReader.peek();
        while ( event != null && !event.isStartElement() ) {
            staxEventReader.nextEvent();
            event = staxEventReader.peek();
        }
    }
    catch ( Exception e ) {
        throw new MappingException( "Error accessing stax stream", e, origin );
    }

    if ( event == null ) {
        throw new MappingException( "Could not locate root element", origin );
    }

    final Schema validationSchema;
    final Class jaxbTarget;

    final String elementName = event.asStartElement().getName().getLocalPart();

    if ( "entity-mappings".equals( elementName ) ) {
        final Attribute attribute = event.asStartElement().getAttributeByName( ORM_VERSION_ATTRIBUTE_QNAME );
        final String explicitVersion = attribute == null ? null : attribute.getValue();
        validationSchema = validateXml ? resolveSupportedOrmXsd( explicitVersion ) : null;
        jaxbTarget = JaxbEntityMappings.class;
    }
    else {
        validationSchema = validateXml ? hbmSchema() : null;
        jaxbTarget = JaxbHibernateMapping.class;
    }

    final Object target;
    final ContextProvidingValidationEventHandler handler = new ContextProvidingValidationEventHandler();
    try {
        JAXBContext jaxbContext = JAXBContext.newInstance( jaxbTarget );
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        unmarshaller.setSchema( validationSchema );
        unmarshaller.setEventHandler( handler );
        target = unmarshaller.unmarshal( staxEventReader );
    }
    catch ( JAXBException e ) {
        throw new MappingException( ... );
    }

    return new JaxbRoot( target, origin );
}

In my testing the DTD being there or not has no effect. And like I said before, simply changing

<hibernate-mapping package="org.hibernate.test.abstractembeddedcomponents.cid">

to

<hibernate-mapping xmlns="http://www.hibernate.org/xsd/hibernate-mapping" package="org.hibernate.test.abstractembeddedcomponents.cid">

fixes the failures I see, which are:

[org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'hibernate-mapping'.]
    at ...
Caused by: org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'hibernate-mapping'.
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:131)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:384)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:318)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(XMLSchemaValidator.java:1916)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(XMLSchemaValidator.java:705)
    at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorHandlerImpl.startElement(ValidatorHandlerImpl.java:550)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.ValidatingUnmarshaller.startElement(ValidatingUnmarshaller.java:78)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.InterningXmlVisitor.startElement(InterningXmlVisitor.java:60)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.StAXEventConnector.handleStartElement(StAXEventConnector.java:247)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.StAXEventConnector.bridge(StAXEventConnector.java:116)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:394)
    ... 27 more
  • 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-06-04T04:59:43+00:00Added an answer on June 4, 2026 at 4:59 am

    This can be done by implementing a filter which adds a default namespace declaration to the first (i.e. root) StartELement event. StAX already provides the EventReaderDelegate utility class, where the peek() and nextEvent() methods need to be overridden.

    Here’s the code:

    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    
    import javax.xml.namespace.QName;
    import javax.xml.stream.XMLEventFactory;
    import javax.xml.stream.XMLEventReader;
    import javax.xml.stream.XMLStreamException;
    import javax.xml.stream.events.StartElement;
    import javax.xml.stream.events.XMLEvent;
    import javax.xml.stream.util.EventReaderDelegate;
    
    /**
     * Filter adding default namespace declaration to root element.
     */
    public class NamespaceAddingEventReader extends EventReaderDelegate {
        private final XMLEventFactory factory = XMLEventFactory.newInstance();
        private final String namespaceURI;
    
        private int startElementCount = 0;
    
        public NamespaceAddingEventReader(XMLEventReader reader, String namespaceURI) {
            super(reader);
            this.namespaceURI = namespaceURI;
        }
    
        /**
         * Duplicate event with additional namespace declaration.
         * @param startElement
         * @return event with namespace
         */
        private StartElement withNamespace(StartElement startElement) {
            List<Object> namespaces = new ArrayList<Object>();
            namespaces.add(factory.createNamespace(namespaceURI));
            Iterator<?> originalNamespaces = startElement.getNamespaces();
            while (originalNamespaces.hasNext()) {
                namespaces.add(originalNamespaces.next());
            }
            return factory.createStartElement(
                    new QName(namespaceURI, startElement.getName().getLocalPart()),
                    startElement.getAttributes(),
                    namespaces.iterator());
        }
    
        @Override
        public XMLEvent nextEvent() throws XMLStreamException {
            XMLEvent event = super.nextEvent();
            if (event.isStartElement()) {
                if (++startElementCount == 1) {
                    return withNamespace(event.asStartElement());
                }
            }
            return event;
        }
    
        @Override
        public XMLEvent peek() throws XMLStreamException {
            XMLEvent event = super.peek();
            if (startElementCount == 0 && event.isStartElement()) {
                return withNamespace(event.asStartElement());
            } else {
                return event;
            }
        }
    }
    

    To see how this is used, let’s copy some XML without namespace declaration to System.out using the event API:

    StringReader xml = new StringReader("<?xml version='1.0'?><alice>bob</alice>");
    XMLEventReader reader = XMLInputFactory.newInstance().createXMLEventReader(xml);
    reader = new NamespaceAddingEventReader(reader, "http://foo");
    XMLEventWriter writer = XMLOutputFactory.newInstance().createXMLEventWriter(System.out);
    writer.add(reader);
    writer.flush();
    

    Running the code will print

    <?xml version='1.0' encoding='UTF-8'?><alice xmlns="http://foo">bob</alice>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert some code from Richfaces 4 showcase to use CDI instead
There is some code that I'm trying to convert from IList to IEnumerable :
I am trying to convert some pages from my app to use cfc's, and
I'm trying to convert some code from Python to C++ in an effort to
I'm trying to convert some VC++ 6 code to a console app using only
I'm trying to write some code to convert data from a object type field
I'm trying to convert some php code into python and am using curl. I've
I trying to convert some loops in my code to use the for_each functionality
I'm trying to convert some data from long to wide using the reshape2 package,
I'm trying to use the geocoding code from here in my ASP.NET MVC 2

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.