1

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 = "";

2 Answers 2

2

Add boolean variable isCountryBean;

boolean isCountryBean = false , isSecurityQuestion1 = false;

And in startElement(....) write:

if(localName.equalsIgnoreCase("countryBean")){
    isCountryBean = true;
    // other stuff
} else if (localName.equalsIgnoreCase("secutiryQuestion1")){
    isSecurityQuestion1 = true;
    isCountryBean = false;
}

And in endElement(...) check:

if(localName.equalsIgnoreCase("id")){
    if(isCountryBean){
        tempResponse.setCountryId(currentValue); 
     }
}
if(localName.equalsIgnoreCase("name")){
    if(isCountryBean){
        isCountryBean = false;
        tempResponse.setCountryName(currentValue);

     }
}

Do it for other most outward tags. Hope it will help you.

Sign up to request clarification or add additional context in comments.

Comments

1

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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.