4

Update:- Seems like I have to give the URL which is returning me the xml. Here it is - URL


I am getting SAXParseException when parsing an xml. The xml I am trying to parse is as follows -

<?xml version="1.0" encoding="utf-8"?>
<markers xmlns="">
    <marker name="" address1="" address2="" region="" country="" zip="" imgsrc="" lat="" lng="" distance="" />
    <marker name="" address1="" address2="" region="" country="" zip="" imgsrc="" lat="" lng="" distance="" />
    <marker name="" address1="" address2="" region="" country="" zip="" imgsrc="" lat="" lng="" distance="" />
    <marker name="" address1="" address2="" region="" country="" zip="" imgsrc="" lat="" lng="" distance="" />
    <marker name="" address1="" address2="" region="" country="" zip="" imgsrc="" lat="" lng="" distance="" />
</markers>

The code which I am using to parse this xml is as follows -

public Document getDomElement(String xml) {
    Document doc = null;
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {

        DocumentBuilder db = dbf.newDocumentBuilder();

        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(xml));
        doc = db.parse(is);

    } catch (ParserConfigurationException e) {
        e.printStackTrace();
        Log.e("ParserConfigurationException: ", e.getMessage());
        return null;
    } catch (SAXException e) {
        e.printStackTrace();
        Log.e("SAXException: ", e.getMessage());
        return null;
    } catch (IOException e) {
        e.printStackTrace();
        Log.e("IOException: ", e.getMessage());
        return null;
    }
    return doc;
}

I am using same code to parse other xml data and it works. I couldn't understand what's there in this xml data which is stopping it to parse the data properly. I have checked the xml with many xml validators online too.

I must be missing something obvious here.

EDIT:- The xml I am trying to parse is from a webservice(the name, address etc. are not actually empty). When I try to use the xml directly by hardcoding in the code itself, it works fine. But when trying to use it directly from webservice response, is not working for me.

The exception:-

10-09 20:39:50.328: W/System.err(16211): org.xml.sax.SAXParseException: Unexpected token (position:TEXT @1:2 in java.io.StringReader@41941010) 
10-09 20:39:50.335: W/System.err(16211):    at org.apache.harmony.xml.parsers.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:146)
10-09 20:39:50.335: W/System.err(16211):    at com.blackcobrastudios.ukash.WebService.XMLDataParser.getDomElement(XMLDataParser.java:159)
10-09 20:39:50.335: W/System.err(16211):    at com.blackcobrastudios.ukash.Manager.APICaller.processResult(APICaller.java:386)
10-09 20:39:50.335: W/System.err(16211):    at com.blackcobrastudios.ukash.Manager.APICaller.access$1(APICaller.java:385)
10-09 20:39:50.335: W/System.err(16211):    at com.blackcobrastudios.ukash.Manager.APICaller$SoapCaller.onPostExecute(APICaller.java:361)
10-09 20:39:50.335: W/System.err(16211):    at com.blackcobrastudios.ukash.Manager.APICaller$SoapCaller.onPostExecute(APICaller.java:1)
10-09 20:39:50.335: W/System.err(16211):    at android.os.AsyncTask.finish(AsyncTask.java:602)
10-09 20:39:50.343: W/System.err(16211):    at android.os.AsyncTask.access$600(AsyncTask.java:156)
10-09 20:39:50.343: W/System.err(16211):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
10-09 20:39:50.343: W/System.err(16211):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-09 20:39:50.343: W/System.err(16211):    at android.os.Looper.loop(Looper.java:137)
10-09 20:39:50.343: W/System.err(16211):    at android.app.ActivityThread.main(ActivityThread.java:4536)
10-09 20:39:50.343: W/System.err(16211):    at java.lang.reflect.Method.invokeNative(Native Method)
10-09 20:39:50.351: W/System.err(16211):    at java.lang.reflect.Method.invoke(Method.java:511)
10-09 20:39:50.351: W/System.err(16211):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
10-09 20:39:50.351: W/System.err(16211):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
10-09 20:39:50.351: W/System.err(16211):    at dalvik.system.NativeStart.main(Native Method)
10-09 20:39:50.351: E/SAXException:(16211): Unexpected token (position:TEXT @1:2 in java.io.StringReader@41941010) 
A Nice Guy
  • 2,676
  • 4
  • 30
  • 54
0xC0DED00D
  • 19,522
  • 20
  • 117
  • 184

4 Answers4

4

The problem was in the encoding. The following answer explains it properly -

https://stackoverflow.com/a/2869127/1079901

I just have to call this regex on the response string -

response = response.replaceAll("[^\\x20-\\x7e]", "");

Thanks to BalusC for such a perfect answer.

Community
  • 1
  • 1
0xC0DED00D
  • 19,522
  • 20
  • 117
  • 184
2

I recommend abandoning the sax parser and using the Android officially supported XMLPullParser. Sax parser is not supported after Android Gingerbread (2.3.3)

Roy Hinkley
  • 10,111
  • 21
  • 80
  • 120
  • I have tried it already, it's giving me XMLPullParserException. Please check the XML URL in the question. – 0xC0DED00D Oct 10 '13 at 07:16
  • You have `xmlns=""` If you want the parser to work, you need to define the xml namespace that defines the rules. Most XML is compliant with `http:www.w3.org/2001/XMLSchema` but you may need a different one. If you don't identify the namespace, the parser won't know the rules to parse your xml. – Roy Hinkley Oct 10 '13 at 13:00
  • How do I add the rules? I can't change the webservice, so I have to make changes in the resultant string. – 0xC0DED00D Oct 10 '13 at 14:10
  • You don't need to add the rules, the namespace already defines them. The parser uses the namespace to know which rules to use. – Roy Hinkley Oct 10 '13 at 14:20
  • 1
    My app kept on crashing because it `"doesn't support the required security features: http://xml.org/sax/features/external-general-entities"` .. now this explain why, thank you – McLan Feb 13 '17 at 13:22
2


If the XML contains Unicode characters such as arabic or Persian letters, StringReader would make an exception. In these cases, pass the InputStream straightly to the Document object

Robert
  • 5,278
  • 43
  • 65
  • 115
Arash moradabadi
  • 230
  • 1
  • 12
1

I would recommend trying to incorporate some XSD validation in your program before you run it through your code.

The following may help you in your efforts:

James Oravec
  • 19,579
  • 27
  • 94
  • 160
  • I checked it using http://www.validome.org/xml/validate/ at it is telling some missing definition error. – 0xC0DED00D Oct 10 '13 at 09:11
  • Try to simply your code. I'd start by removing the reference to utf-8 and then keep simplying until you get something that works, then build up to what you need. If you need additional resources for learning, I would recommend http://www.w3schools.com/schema/ and http://www.w3schools.com/xml/ – James Oravec Oct 11 '13 at 20:37