If anyone wonders, I found my problem. It is in the characters()
function, I am not taking into account the start and length
parameters. To fix this, I used:
String resourceName = new String(ch).substring
(start, start + length);
On Sep 12, 1:05 am, Felix Oghina <[email protected]> wrote:
> I'm trying to parse the XML document at this
> location:http://tastekid.com/ask/ws?q=placebo
> This is the result I am
> getting:http://i498.photobucket.com/albums/rr348/Znupi2/xml-bogus.png
> Here is my code (the part that parses the XML):
>
> public class XmlHandler extends DefaultHandler {
>
> private boolean inResults = false;
> private boolean inResource = false;
> private boolean inResourceName = false;
>
> public void startElement(String uri, String name, String qName,
> Attributes atts) {
> if (name == "results") inResults = true;
> if (name == "resource") inResource = true;
> if (name == "name") inResourceName = true;
> }
>
> public void characters(char ch[], int start, int length) {
> if (inResults && inResource && inResourceName) {
> String resourceName = new String(ch);
> debug.setText(debug.getText() + "\"" + resourceName +
> "\"\n");
> }
> }
>
> public void endElement(String uri, String name, String qName) {
> if (name == "results") inResults = false;
> if (name == "resource") inResource = false;
> if (name == "name") inResourceName = false;
> }
>
> }
>
> private void parseResponseXml(String xml) throws Exception {
> SAXParserFactory spf = SAXParserFactory.newInstance();
> SAXParser sp = spf.newSAXParser();
> XMLReader xr = sp.getXMLReader();
> XmlHandler handler = new XmlHandler();
> xr.setContentHandler(handler);
> xr.parse(new InputSource(new StringReader(xml)));
>
> }
>
> This is how I'm fetching the URL contents (maybe it has something to
> do with encodings):
>
> private String getUrlContent(String sUrl) throws Exception {
> URL url = new URL(sUrl);
> HttpURLConnection connection = (HttpURLConnection)
> url.openConnection();
> connection.setRequestMethod("GET");
> connection.setDoOutput(true);
> connection.connect();
> BufferedReader rd = new BufferedReader(new
> InputStreamReader(connection.getInputStream()));
> String content = "", line;
> while ((line = rd.readLine()) != null) {
> content += line + "\n";
> }
> return content;
>
> }
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~----------~----~----~----~------~----~------~--~---