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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:23:24+00:00 2026-06-07T04:23:24+00:00

I am trying to parse and get values from web service. The problem is

  • 0

I am trying to parse and get values from web service. The problem is that Web service has same element name tags which are creating problem to parse as I want to get their value but they are of same name So is difficult for me to mention them as localname.

 <countryBean>
        <id>236</id>
        <name>United State</name>
    </countryBean>
    <secutiryQuestion1>
        <id>2</id>
        <question>What was your dream job as a child?</question>
    </secutiryQuestion1>
    <secutiryQuestion2>
        <id>4</id>
        <question>What is the name, breed, and color of your pet?</question>
    </secutiryQuestion2>
    <stateBean>
        <country>236</country>
        <id>5</id>
        <name>California</name>
    </stateBean>
    <statusBean>
        <id>1</id>
        <name>Active</name>
    </statusBean>

I want to get value of ID tag and other adjacent tag like name,question in different variables. and Here are 5 Id tags.

My class code is like

public class XmlParser extends DefaultHandler {

        public RestfullResponse tempResponse = null;
        String currentValue = null;
        int ServiceType =0;

        @Override
        public void startElement(String uri, String localName, String qName,
                Attributes attributes) throws SAXException {
            if(localName.equalsIgnoreCase("countryBean")){
                tempResponse=new RestfullResponse();

                }           

        }


        /* (non-Javadoc)
         * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
         */
        @Override
        public void endElement(String uri, String localName, String qName)
                throws SAXException {
            if(ServiceType == 1)
            {
                if(localName.equalsIgnoreCase("id")){
                    tempResponse.setCountryId(currentValue);
                }
                if(localName.equalsIgnoreCase("name")){
                    tempResponse.setCountryName(currentValue);
                }

                if(localName.equalsIgnoreCase("id")){
                    tempResponse.setStateId(currentValue);
                }
                if(localName.equalsIgnoreCase("question")){
                    tempResponse.setstateBean(currentValue);
                }

                if(localName.equalsIgnoreCase("Id")){
                    tempResponse.setStatusId(currentValue);
                }
                if(localName.equalsIgnoreCase("requestSuccess")){
                    tempResponse.setStatus(currentValue);
                }
            }
        }

        @Override
        public void characters(char[] ch, int start, int length)
                throws SAXException {
            currentValue = new String(ch, start, length);
        }

        public RestfullResponse getResponse(){
            return tempResponse;
        }
}

please Help to store them in

String CountryName= "";
String CountryId = "";
String Statename = "";
String StateId ="";
String Status = "";
String StatusId = "";
String Username ="";
String SecurityQuestion = "";
String SecurityQuestionId = "";
  • 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-07T04:23:29+00:00Added an answer on June 7, 2026 at 4:23 am

    You need to maintain the stack of root nodes.

    Take a variable of type String root and maintain a stack of root element.

    Inside startElement method:

    public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException,
                IllegalArgumentException
    {
    
       // call to abstract method
       onStartElement(namespaceURI, localName, qName, atts);
    
       // Keep the trace of the root nodes
       if (root.length() > 0)
       {
        root= root.concat(",");
       }
       root= root.concat(localName);
    }
    

    Inside endElement method:

    public void endElement(String namespaceURI, String localName, String qName) throws SAXException
    {
       // remove the end element from stack of root elements
       int index = root.lastIndexOf(',');
       if (index > 0)
       {
        root= root.substring(0, index);
       }
       onEndElement(namespaceURI, localName, qName);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to parse a web service response using SAXParser and get certain values,
I am trying to return a value from an XML Web Service that I
I'm trying to get values from xml, and I have problem with empty value.
Im trying to get the values from my datepicker into a new script which
I was trying to parse a JSON string from USGS . However I get
I'm trying to parse through fixed-width formatted file extracting x,y values of points from
I'm trying to run a java class that reads off some values from a
I'm trying to fetch information from a JSON web service with this code, using
I am trying to screen scrape values from a website. # get the raw
I'm trying to figure out how to parse the <current></current> values from the first

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.