Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Regular Contributor
Posts: 67
Registered: ‎08-16-2011
My Device: 7185
My Carrier: Reliance

Re: Ksoap parsing

KSOAP PArsing using kxml Smiley Happy

 

Example Xml is :

<response>

	<error> 	</error>
	
	<result>
	   <data>
			 <fname>Ganesh </fname>
			 <lname>Pachpind </lname>
			 <add>Om Shanti Nagar</add>
	   </data>			
	   <data>
		    <fname>Mayur </fname>
		    <lname>Pachpind </lname>
		    <add>Om Shanti Nagar</add>
	   </data>
	</result>
	
</response>

 Code for Parsing :

// here XMLSTRING is the response String  from webservice which is as above

//Parsing logic

        byte[] xmlByteArray = XMLSTRING.getBytes();
		ByteArrayInputStream xmlStream = new ByteArrayInputStream( xmlByteArray );		
		InputStreamReader xmlReader = new InputStreamReader( xmlStream );
		
		// create new parser objet
		
		    KXmlParser parser = new KXmlParser();
			parser.setInput(xmlReader);
			parser.nextTag();     
			parser.require(XmlPullParser.START_TAG, null, "response");	
			
			// main tag is response
			while((parser.next()==XmlPullParser.START_TAG)&&(parser.getName().equalsIgnoreCase("error")))
			{
				 if(parser.nextText().equalsIgnoreCase(""))
				 {
					 //if error is empty then only it will check for result tag					 
						flag=true;
					 while((parser.next()==XmlPullParser.START_TAG)&&(parser.getName().equalsIgnoreCase("result")))
					 {
						 //resut tag parsing now
                         // as their exit one more xml structure in result tag we will parse it seperate
						 
						    byte[] xmlByteArray1 = (parser.nextText()).getBytes();
							ByteArrayInputStream xmlStream1 = new ByteArrayInputStream( xmlByteArray1 );
							InputStreamReader xmlReader1 = new InputStreamReader( xmlStream1 );
						    KXmlParser parser1 = new KXmlParser();
						   
							parser1.setInput(xmlReader1);		
							parser1.next();
							 
							 //DATA tag 
							parser1.require(XmlPullParser.START_TAG, null, "data");
							while ((parser1.nextTag () != XmlPullParser.END_TAG))
							{
					        	 readXMLData(parser1);
							}
							parser1.require(XmlPullParser.END_TAG, null, "data");
							parser1.next();					
							parser1.require(XmlPullParser.END_DOCUMENT, null, null);
					
					 }// result tag parsing end						
						    
					    				   
				 }
				 else 
				 {
					//if error tag is not empty do its parsing here
					
					
				 }
				 
			}// while error tag end 

 readXMLData function as follow :

private void readXMLData(KXmlParser parser)throws Exception
	{
		// TODO Auto-generated method stub.
		
		parser.require(XmlPullParser.START_TAG, null, "data");
       
        
        while (parser.nextTag() != XmlPullParser.END_TAG)
		{			
			parser.require(XmlPullParser.START_TAG, null, null);
			String name = parser.getName();           
			String text = parser.nextText();
			if (name.equals("fname"))
				System.out.println("First Name:"+name.equals("fname"));
			else if (name.equals("lname"))
				System.out.println("Last Name:"+name.equals("lname"));
			else if (name.equals("add"))
				System.out.println("Address:"+name.equals("add"));				
		parser.require(XmlPullParser.END_TAG, null, name);
		}		
	  parser.require(XmlPullParser.END_TAG, null, "data");
	  	   
   }

 

Highlighted
New Developer
Posts: 120
Registered: ‎05-20-2011
My Device: Touch 9800
My Carrier: Vodafone

Re: Ksoap parsing

I think if we use methods like getProperty() they are easy to handle and parse the data.Don't you think so.If we use in built ksoap methods parsing will be very much easier.

Amar
Trusted Contributor
Posts: 224
Registered: ‎08-09-2011
My Device: 9550
My Carrier: BBprogrammer

Re: Ksoap parsing

  Thank you for you replay..

 

 

     how i can establish this MportfolioSoap,where i am getting error..

Regular Contributor
Posts: 67
Registered: ‎08-16-2011
My Device: 7185
My Carrier: Reliance

Re: Ksoap parsing

can you  elaborate your question ???

i dodn't understand what you asked ??

New Developer
Posts: 120
Registered: ‎05-20-2011
My Device: Touch 9800
My Carrier: Vodafone

Re: Ksoap parsing

[ Edited ]

Just remove MportfolioSoap.Ojects[i] with objects[i]  you able to run this.....

 

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!

Amar
Trusted Contributor
Posts: 224
Registered: ‎08-09-2011
My Device: 9550
My Carrier: BBprogrammer

Re: Ksoap parsing

Actually i want to get data from this:

 

 

 

<ns:getColleagueProfileResponse xmlns:ns=cghdhcg" xmlns:ax23="cdxfgfgfgfgfgfgfgfgfgfgfgfgxdgf">
<ns:return xsi:type="ax23:Colleagues" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ax23:email>vhcv@ixfgxg</ax23:email>
<ax23:fullName>xgxgarri</ax23:fullName>
<ax23:jobTitle>java developer</ax23:jobTitle>
<ax23Smiley SurprisedfficeLocation>hyderabad</ax23Smiley SurprisedfficeLocation>
<ax23:totalCollegues>4</ax23:totalCollegues>
</ns:return>
<ns:return xsi:type="ax23:Colleagues" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ax23:email>vhcv@ixfgxg</ax23:email>
<ax23:fullName>xgxgarri</ax23:fullName>
<ax23:jobTitle>java developer</ax23:jobTitle>
<ax23Smiley SurprisedfficeLocation>hyderabad</ax23Smiley SurprisedfficeLocation>
<ax23:totalCollegues>4</ax23:totalCollegues>
</ns:return>
<ns:return xsi:type="ax23:Colleagues" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ax23:email>vhcv@ixfgxg</ax23:email>
<ax23:fullName>xgxgarri</ax23:fullName>
<ax23:jobTitle>java developer</ax23:jobTitle>
<ax23Smiley SurprisedfficeLocation>hyderabad</ax23Smiley SurprisedfficeLocation>
<ax23:totalCollegues>4</ax23:totalCollegues>
</ns:return>
<ns:return xsi:type="ax23:Colleagues" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ax23:email>vhcv@ixfgxg</ax23:email>
<ax23:fullName>xgxgarri</ax23:fullName>
<ax23:jobTitle>java developer</ax23:jobTitle>
<ax23Smiley SurprisedfficeLocation>hyderabad</ax23Smiley SurprisedfficeLocation>
<ax23:totalCollegues>4</ax23:totalCollegues>
</ns:return>
</ns:getColleagueProfileResponse>

 

 

 

 

 

for this i am writting this::

 

 

URL = Tools
.getURLURL;
SoapObject object = new SoapObject(
"http://profileupdate.gvh",
"getColleagueProfileResponse");
object.addProperty("app", "fgfgfgfg");
object.addProperty("count", "1");
SoapSerializationEnvelope envelope = new
SoapSerializationEnvelope(
SoapSerializationEnvelope.VER11);
envelope.setOutputSoapObject(object);
HttpTransport httpTransport = new HttpTransport(URL);
httpTransport.call("getColleagueProfileResponse", envelope);
org.ksoap2.serialization.SoapObject[] objects;
response = (SoapObject) envelope.getResponse();// response
Status.show("Count: "+response.getPropertyCount());
// will ke
// the
// response
// from
// ksoap
// object

objects = new org.ksoap2.serialization.SoapObject[response
.getPropertyCount()];
for (int ii = 0; ii < objects[response
.getPropertyCount()].getPropertyCount(); ii++) {
Status.show("Coun22t: "+response.getPropertyCount());
if
(!(objects[10].getProperty(ii).getClass().equals(objects[10].getClass())))
continue;
org.ksoap2.serialization.SoapObject temp =
(SoapObject)objects[10].getProperty(ii);
if (temp.getName().equals("anyType"))

{
if
(temp.equals(objects[10].getProperty("Colleagues")))
{
continue;
}
else
{
Status.show(temp.getProperty("email").toString());
// temphfm.add(createDayName(temp.getProperty("quantityHeld").toString()));
// temphfm.add(createDayName(temp.getProperty("costBasis").toString()));
// temphfm.add(createDayName(temp.getProperty("marketValue").toString()));
// temphfm.add(createDayName(temp.getProperty("valuation").toString()));
// temphfm.add(createDayName(temp.getProperty("gain").toString()));
// temphfm.add(createDayName(temp.getProperty("percentGain").toString()));

}

}

}

// }
// }

} catch (Exception e) {
// TODO: handle exception
}

 

 

 i am getting error  from this  ..

 

 

can you please give the solution for this..

 

 

New Developer
Posts: 120
Registered: ‎05-20-2011
My Device: Touch 9800
My Carrier: Vodafone

Re: Ksoap parsing

[ Edited ]

Simply try this for your each element tag:-

 

objects=new org.ksoap2.serialization.SoapObject[response.getPropertyCount()];
            for (int i = 0; i < response.getPropertyCount(); i++)
            {
                objects[i]=(SoapObject) response.getProperty(i);
                 org.ksoap2.serialization.SoapObject obj= (SoapObject) response.getProperty(0);
                 System.out.println("output======" + response.getProperty(i).toString());
                
                 System.out.println("orderDate======" +objects[i].getProperty("orderDate"));
                 System.out.println("Id======" +objects[i].getProperty("id"));
                 System.out.println("label======" +objects[i].getProperty("label"));
                 System.out.println("quantityPending======" +objects[i].getProperty("quantityPending"));
                 System.out.println("securityName======" +objects[i].getProperty("securityName"));
                 System.out.println("priceType======" +objects[i].getProperty("priceType"));
                 System.out.println("orderOrigin======" +objects[i].getProperty("orderOrigin"));
                 System.out.println("orderDate======" +objects[i].getProperty("orderStatus"));
             
            }

 

 

--------------------------------------------------

--------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!

Amar
Trusted Contributor
Posts: 224
Registered: ‎08-09-2011
My Device: 9550
My Carrier: BBprogrammer

Re: Ksoap parsing

try {
URL = Tools
.getURL(URL);
SoapObject object = new SoapObject(
"http://profileupdaxzgfxcomdcfg",
"getColleagueProfileResponse");
object.addProperty("app", "xzv");
object.addProperty("count", "1");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapSerializationEnvelope.VER11);
envelope.setOutputSoapObject(object);
HttpTransport httpTransport = new HttpTransport(URL);
httpTransport.call("getColleagueProfileResponse", envelope);
org.ksoap2.serialization.SoapObject[] objects;
response = (SoapObject) envelope.getResponse();// response
Status.show("Count: " + response.getPropertyCount());
// will ke
// the
// response
// from
// ksoap
// object

objects = new org.ksoap2.serialization.SoapObject[response
.getPropertyCount()];
objects = new org.ksoap2.serialization.SoapObject[response
.getPropertyCount()];
for (int i = 0; i < response.getPropertyCount(); i++) {
objects[i] = (SoapObject) response.getProperty(i);
org.ksoap2.serialization.SoapObject obj = (SoapObject) response
.getProperty(0);
Status.show("output======"
+ response.getProperty(i).toString());

Status.show("email======"
+ objects[i].getProperty("email"));
Status.show("fullName======"
+ objects[i].getProperty("fullName"));
Status.show("jobTitle======"
+ objects[i].getProperty("jobTitle"));
Status.show("officeLocation======"
+ objects[i].getProperty("officeLocation"));
Status.show("totalCollegues======"
+ objects[i].getProperty("totalCollegues"));

}

} catch (Exception e) {
// TODO: handle exception
}

 

i am trying what u have told but i got Exception ... 

 

i am not able to find out the error where it is.. please give furthrt solution.

 

 

New Developer
Posts: 120
Registered: ‎05-20-2011
My Device: Touch 9800
My Carrier: Vodafone

Re: Ksoap parsing

What type of error u are facing??

Amar
Trusted Contributor
Posts: 224
Registered: ‎08-09-2011
My Device: 9550
My Carrier: BBprogrammer

Re: Ksoap parsing

Excertion Occure at response.getProperty(int);