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 8001303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:03:39+00:00 2026-06-04T16:03:39+00:00

In the following code example, I use the STaX parser to parse a piece

  • 0

In the following code example, I use the STaX parser to parse a piece of XML. If I run the xml10 through it, it works as expected. The xml11 string (which is the same, except for the xml version) – it throws a NullPointerException. I’m running this on a Mac using JDK 1.6.

import javax.xml.namespace.QName;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamReader;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringReader;
import java.util.Stack;

/**
 */
public class StaxSucks {

    static String xml10 ="<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n"+
                        "<anElement/>";

    static String xml11 ="<?xml version=\"1.1\" encoding=\"utf-8\" ?>\n"+
            "<anElement/>";


    static void parse(InputStream is) throws Exception{
        final XMLInputFactory factory = XMLInputFactory.newInstance();
        factory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE);
        final XMLStreamReader xmlStreamReader = factory.createXMLStreamReader(is);
        Stack<QName> XMLDEPTH = new Stack<QName>();
        int eventType = xmlStreamReader.next();
        while(eventType != XMLStreamConstants.END_DOCUMENT){
            if(XMLStreamConstants.START_ELEMENT == eventType){
                QName eventName = xmlStreamReader.getName();
                XMLDEPTH.push(eventName);
            }else if(XMLStreamConstants.END_ELEMENT == eventType){
                //ends should always match the starts.
                QName eventName = xmlStreamReader.getName();
                if(XMLDEPTH.peek().equals(eventName)){
                    XMLDEPTH.pop();
                }else{
                    System.out.println("Hit an end with a non-matching beginning:"+eventName);
                }
            } else{
                System.out.println("Hit event type:"+eventType);
            }
            eventType = xmlStreamReader.next();
        }
        System.out.println("Stack is empty:"+XMLDEPTH.empty());

    }

    public static void main(String[] args) throws Exception{
        System.out.println("Starting XML1.0");
        InputStream is = new ByteArrayInputStream(xml10.getBytes("utf8"));
        parse(is);
        System.out.println("Starting XML1.1");
        is = new ByteArrayInputStream(xml11.getBytes("utf8"));
        parse(is);
    }
}

Stack Trace:

Exception in thread "main" java.lang.NullPointerException
    at com.sun.org.apache.xerces.internal.impl.XML11NSDocumentScannerImpl.scanStartElement(XML11NSDocumentScannerImpl.java:351)
    at com.sun.org.apache.xerces.internal.impl.XML11NSDocumentScannerImpl$NS11ContentDriver.scanRootElementHook(XML11NSDocumentScannerImpl.java:889)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3104)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:922)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
    at com.sun.org.apache.xerces.internal.impl.XML11NSDocumentScannerImpl.next(XML11NSDocumentScannerImpl.java:852)
    at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(XMLStreamReaderImpl.java:554)
    at StaxSucks.parse(StaxSucks.java:46)
    at StaxSucks.main(StaxSucks.java:74)
  • 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-04T16:03:41+00:00Added an answer on June 4, 2026 at 4:03 pm

    Hi This is a case of broken stax implementation in the Sun/Oracle JDK, IBM JDK works fine, or you can even just use the latest Xerces jars and you will be fine.

    You can download xerces jars from:
    http://xerces.apache.org/mirrors.cgi#binary

    dims@dims-laptop-520:~/test$ /usr/lib/jvm/java-6-sun/bin/java -cp . StaxSucks
    Starting XML1.0
    Stack is empty:true
    Starting XML1.1
    Exception in thread "main" java.lang.NullPointerException
        at com.sun.org.apache.xerces.internal.impl.XML11NSDocumentScannerImpl.scanStartElement(XML11NSDocumentScannerImpl.java:351)
        at com.sun.org.apache.xerces.internal.impl.XML11NSDocumentScannerImpl$NS11ContentDriver.scanRootElementHook(XML11NSDocumentScannerImpl.java:889)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3104)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:922)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
        at com.sun.org.apache.xerces.internal.impl.XML11NSDocumentScannerImpl.next(XML11NSDocumentScannerImpl.java:852)
        at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(XMLStreamReaderImpl.java:554)
        at StaxSucks.parse(StaxSucks.java:26)
        at StaxSucks.main(StaxSucks.java:54)
    dims@dims-laptop-520:~/test$ java -cp .:xercesImpl.jar:xml-apis.jar StaxSucks
    Starting XML1.0
    Stack is empty:true
    Starting XML1.1
    Stack is empty:true
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code example to use an object that receives the action
The following example code compiles under gcc and works as I would hope. It
for example I use following code: $(img).each(function(i){ x += $(img:eq( + i )).width(); if(some
I made the following code example to learn how to use a generics method
In the following code example how do the user/userParam references relate to the Customer
Please forgive the verbosity of the following code example. Using Delphi 2009, I created
Consider following SWT code example: http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet151.java?view=co How can I separate the inline defined class?
In the following (working) code example, the templated register_enum() function is used to iterate
When I execute this following code for example: cart.php?p=1&action=add <?php session_start(); if (isset($_GET['p']) &&
The following code is an example of what I think would qualify as pseudocode,

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.