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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:14:33+00:00 2026-06-17T08:14:33+00:00

Java: 1.6 Woodstox: 4.1.4 I just want to skip part of xml file, while

  • 0

Java: 1.6
Woodstox: 4.1.4

I just want to skip part of xml file, while parsing.
Let’s look at that simple xml:

<family>
    <mom>
        <data height="160"/>
    </mom>
    <dad>
        <data height="175"/>
    </dad>
</family>

I just want do skip dad element. So it look’s like using skipElement method like shown below is a good idea:

FileInputStream fis = ...;
XMLStreamReader2 xmlsr = (XMLStreamReader2) xmlif.createXMLStreamReader(fis);

String currentElementName = null;
while(xmlsr.hasNext()){
            
    int eventType = xmlsr.next();
                        
    switch(eventType){
            
        case (XMLEvent2.START_ELEMENT):
            currentElementName = xmlsr.getName().toString();
                    
            if("dad".equals(currentElementName) == true){
                logger.info("isStartElement: " + xmlsr.isStartElement());
                logger.info("Element BEGIN: " + currentElementName);
                xmlsr.skipElement();
            }

                    ...
    }
}

We just find start of element dad, and skip it. But not so fast, because Exception will be thrown. This is the output:

isStartElement: true
Element BEGIN: dad
Exception in thread "main" java.lang.IllegalStateException: Current state not START_ELEMENT

That is not what expected. This is indeed very unexpected, because method skipElement is executed in START_ELEMENT state. What is going on?

  • 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-17T08:14:33+00:00Added an answer on June 17, 2026 at 8:14 am

    I’ve found the reason, why I was getting the IllegalStateException. The very useful was flup’s answer. Thanks a lot.
    It is worth to read answer given by Blaise too.

    But getting to the heart of the matter.
    The problem was not skipElement() method itself. The problem was caused becouse of methods used to read attributes. There are three dots (…) in my question. So let’s look what was there:

    switch(eventType){
    
    case (XMLEvent2.START_ELEMENT):
        currentElementName = xmlsr.getName().toString();
        logger.info("currentElementName: " + currentElementName);
    
    
        if("dad".equals(currentElementName) == true){
            logger.info("isStartElement: " + xmlsr.isStartElement());
            logger.info("Element BEGIN: " + currentElementName);
            xmlsr.skipElement();
        }
    
    
        case (XMLEvent2.ATTRIBUTE):
            int attributeCount = xmlsr.getAttributeCount(); 
            ...
            break;
    
    
    }
    

    Important thing. There is no break statement for START_ELEMENT. So every time START_ELEMENT event occurs the code for event ATTRIBUTE is also executed.
    That looks OK according to Java Docs, becouse methods getAttributeCount(), getAttributeValue() etc. can be executed for both START_ELEMENT and ATTRIBUTE.

    But after calling method skipElement(), event START_ELEMENT is changed to END_ELEMENT. So calling method getAttributeCount() is not allowed. This call is the reason why IllegalStateException is thrown.

    The simplest way to avoid that Exception is just calling break statement after calling skipElement() method. In that case code for getting attributes will not be executed, thus Exception will not be thrown.

            if("dad".equals(currentElementName) == true){
                logger.info("isStartElement: " + xmlsr.isStartElement());
                logger.info("Element BEGIN: " + currentElementName);
                xmlsr.skipElement();
                break;                  //the cure for IllegalStateException
            }
    

    I’m sorry I gave you no chance to answer my original question becouse of to much code hidden.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Java: 1.6 Woodstox: 4.1.4 I'm currently trying to make Woodstox xml parser my friend.
java - xpath - pom.xml I am using following pom.xml file as xml dom
JAVA Code Here is some part of my code that I have written in
I am looking for latest, memory efficient and high-performance java XML parsing API. I
Java Experts , Please look into the below split command code and let me
I am using woodstox to implement a StAX parser for XML files. Assume that
Java has a very simple way to serialize objects. couldn't find something similar in
java.util.logging.Logger override (overwrites) the file data instead of add to end of file. Is
Has anyone used the Stax implementation by Woodstox http://woodstox.codehaus.org and sjsxp https://sjsxp.dev.java.net/ . I
java.nio.file.Files.newByteChannel returns SeekableByteChannel . But actual class of the object returned is sun.nio.ch.FileChannelImpl (in

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.