views:

66

answers:

1

Hi!

I have a web service, .asmx that when consumed gives a response in the form:

<lData>
<name>...</name>
<posx>..</posx>
<posy>...</posy>
</lData>

I use Ksoap2 to consume the web service in Android, and I see the response as a string anyType {lData=anyType{name=....;posx=....;posy=....;};}

So, I get the right data from my web service, but i don't know how to parse it correctly. I want to find a way to iterate through the different nodes.

I have tried to use a SAXParser, but can't seem to understand what I should use instead of the url. In the tutotirals I have followed the link to a xml-file on a url, but my url is only part "finished" since i need to consume the web service first.

MyXMLHandler myXMLHandler = new MyXMLHandler();
xr.setContentHandler(myXMLHandler);
xr.parse(new InputSource(sourceUrl.openStream()));

Is this above something I should use, but have another openStream?

Thanks for any help!

AK

+1  A: 

The SAXParser wants an InputStream to parse. I think you are saying that you have a string of xml you want to parse? To do so, you need to turn your String into an input stream:

InputStream is = new ByteArrayInputStream(text.getBytes("UTF-8"));
Mayra
Hi!Well, the web service sends data in xml-form, so that if i take the response.toString() I can read it in a TextView. So I have seen that it is working, the web service connection that is. But now i want to take this information and use it in a listview later on. I tried to use this above, but in order to do this I first have to convert my envelope.getResponse(); to a string. I am thinking that maybe there is a way without that step? I didn't get it to work, my app now has this "sorry shut down" error, so I have to go over the code carefully to find what is wrong.
Anna-Karin
After the InputStream, should I use InputSource is2 = new InputSource(is); xr.parse(is2); ? Thanks again!
Anna-Karin
You implied you had it as a string already, which is why I suggested that. I agree that in general, converting it to a string should not be necessary. What object type do you have? As to the 2nd question, SAXParser has a parse method that takes an InputStream, so you do not need to turn it into an InputSource.
Mayra
This is my object: response = envelope.getResponse(); SAXParserFactory spf = SAXParserFactory.newInstance();SAXParser sp = spf.newSAXParser();XMLReader xr = sp.getXMLReader();MyXMLHandler myXMLHandler = new MyXMLHandler();xr.setContentHandler(myXMLHandler);// xr.parse(new InputSource(sourceUrl.openStream()));
Anna-Karin
Sorry, I don't know how to format the questions in these comment boxes. Instead of the inputSource(sourceURL.openStream), what do you suggest I should use?
Anna-Karin
xr.parse(inputStream); You didn't really answer the question though, what type is the object response?
Mayra
I am a beginner, so I don't really know what type the object response is, I just declare it Object response = null; and then use it to put the envelope response in. Maybe that is not the way to do it? I am using SoapObject to communicate with the web service? Is there a certain object type that would be best to use? If you know of a good tutorial, please tell me so I can go read up. I have found many, but none that has helped me figure this out!
Anna-Karin
Ok, until you figure out what the envelope.getResponse() is giving you, I can't tell you what you need to do to it to turn it into an inputStream. You could debug the app, put a breakpoint right after envelope.getResponse() and examine what it gives you.
Mayra
What I ended up doing was using the Object data1 = (SoapObject) response.getProperty (0); method and then I got the root element, and then getProperty of the data1 object to get the elements below. It is probably not the best way to do it cause I can't parse it with a for loop and "specify" element, but at least it works for now.
Anna-Karin