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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:12:46+00:00 2026-05-26T20:12:46+00:00

This is my program, I am using STAX Parser to parse an XML document

  • 0

This is my program,
I am using STAX Parser to parse an XML document .
When I use hardcoded value then only its working (item.account = “sss”;)
But when ever if I try to use
item.account = eventReader.getElementText();
The size of the List is 0
(List items = new ArrayList();)

import java.io.StringReader;
import java.util.*;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;

public class Pavan {
    static final String DATE        = "date";
    static final String ITEM        = "item";
    static final String MODE        = "mode";
    static final String UNIT        = "unit";
    static final String CURRENT     = "current";
    static final String INTERACTIVE = "interactive";

    @SuppressWarnings({ "unchecked", "null" })
    public static void main(String args[]) {
        List<Position> items = new ArrayList<Position>();
        try {
            String documentAsString = "<request>\r\n" + "<accountid>1234</accountid>\r\n" + "<accountid>234</accountid>\r\n" + "</request>";

            StringReader stringReadertext = new StringReader(documentAsString);
            XMLInputFactory inputFactory = XMLInputFactory.newInstance();
            XMLStreamReader reader = inputFactory.createXMLStreamReader(stringReadertext);

            XMLEventReader eventReader = inputFactory.createXMLEventReader(reader);
            Position item = null;

            while (eventReader.hasNext()) {
                XMLEvent event = eventReader.nextEvent();

                if (event.isStartElement()) {
                    StartElement startElement = event.asStartElement();
                    if (startElement.getName().toString().equals("accountid")) {

                        item = new Position();

                        item.account = "sss";
                        //item.account = eventReader.getElementText();
                    }

                }
                // If we reach the end of an item element we add it to the list
                if (event.isEndElement()) {
                    EndElement endElement = event.asEndElement();
                        if (endElement.getName().toString().equals("accountid")) {
                        items.add(item);
                    }
                }

            }
        } catch (XMLStreamException e) {
            e.printStackTrace();
        }
        System.out.println(items.size());
    }

}


class Position
{
String account ;
}
  • 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-26T20:12:46+00:00Added an answer on May 26, 2026 at 8:12 pm

    Because your end event was “eaten” and the if (event.isEndElement()) was never triggered. However, when you hardcode, “if (event.isEndElement())” is triggered because the missing of “eventReader.getElementText()”.

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

Sidebar

Related Questions

I'm working on a picture select/crop program and using this code below: http://www.androidworks.com/crop_large_photos_with_android/comment-page-1#comment-969 As
How do I get radar images to a .Net program using this service: http://www.nws.noaa.gov/forecasts/xml/
I am trying to write simple program using ONLY for loop ( if then
Using this program, I'm trying to set a 6x15 array to 0, then place
I need to write this program using a loop https://i.stack.imgur.com/gko6T.jpg this is what i
this is a small program using Java Scanner which reads a file of double
For this program #include <iostream> using std::cout; struct C { C() { cout <<
I have written this program, which sorts some ints using a functor: #include<iostream> #include<list>
I have this problem when trying to run hello world program using android SDK.
I am currently developing my program using sample BluetoothChat program on android device. This

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.