org.xml.sax.SAXParseException: Unexpected token while parsing XML

noob

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) 
noob

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

XML parsing exception org.xml.sax.SAXParseException cvc-elt.1

From Dev

Handling org.xml.sax.SAXParseException while reading and writing an XML file using JAXB

From Dev

Handling org.xml.sax.SAXParseException while reading and writing an XML file using JAXB

From Dev

XML Parse ERROR : org.xml.sax.SAXParseException

From Dev

JAXB2 Maven Plugin xjc parse error: org.xml.sax.SAXParseException: Unexpected <xs:element> appears

From Dev

JAXB2 Maven Plugin xjc parse error: org.xml.sax.SAXParseException: Unexpected <xs:element> appears

From Dev

SAXParseException error in XML Parsing

From Dev

Groovy Codenarc.xml org.xml.sax.SAXParseException xsi:schemaLocation not associated with element type ruleset not bound

From Dev

org.xml.sax.SAXParseException: Content is not allowed in prolog when consuming service in .net

From Dev

One JSP getting org.xml.sax.SAXParseException; Attribute "var" was already specified for element "c:set"

From Dev

flaying saucer org.xml.sax.SAXParseException The declaration for the entity HTML.Version must end with >

From Dev

Eclipse - glassfish can't start because org.xml.sax.SAXParseException;

From Dev

How to fix the error "org.xml.sax.SAXParseException / Content is not allowed in prolog"?

From Dev

org.xml.sax.SAXParseException: Content is not allowed in prolog when consuming service in .net

From Dev

Java JaxB Unmarshaller giving org.xml.sax.SAXParseException Premature end of file

From Dev

Exception for no memory while parsing large XML file in SAX parser

From Dev

Android SAX XML parsing error

From Dev

Perl XML::SAX partial parsing

From Dev

org.xml.sax.SAXParseException; lineNumber: 6; columnNumber: 122; cvc-elt.1: Cannot find the declaration of element 'beans'

From Dev

org.xml.sax.SAXParseException;cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found

From Dev

Unexpected Token while Parsing JSON

From Dev

XML unexpected token

From Dev

Running JUnit test in Spring MVC throws: org.xml.sax.SAXParseException; systemId: http://www.springframework.org/schema/mvc/spring-mvc.xsd

From Dev

Running JUnit test in Spring MVC throws: org.xml.sax.SAXParseException; systemId: http://www.springframework.org/schema/mvc/spring-mvc.xsd

From Dev

Parsing XML using SAX Parser in Python 3

From Dev

Parsing and updating xml using SAX parser in java

From Dev

Parsing an XML file with Nokogiri SAX parser

From Dev

Java : Parsing xml file using SAX/XPATH

From Dev

Parsing XML using SAX Parser in Python 3

Related Related

  1. 1

    XML parsing exception org.xml.sax.SAXParseException cvc-elt.1

  2. 2

    Handling org.xml.sax.SAXParseException while reading and writing an XML file using JAXB

  3. 3

    Handling org.xml.sax.SAXParseException while reading and writing an XML file using JAXB

  4. 4

    XML Parse ERROR : org.xml.sax.SAXParseException

  5. 5

    JAXB2 Maven Plugin xjc parse error: org.xml.sax.SAXParseException: Unexpected <xs:element> appears

  6. 6

    JAXB2 Maven Plugin xjc parse error: org.xml.sax.SAXParseException: Unexpected <xs:element> appears

  7. 7

    SAXParseException error in XML Parsing

  8. 8

    Groovy Codenarc.xml org.xml.sax.SAXParseException xsi:schemaLocation not associated with element type ruleset not bound

  9. 9

    org.xml.sax.SAXParseException: Content is not allowed in prolog when consuming service in .net

  10. 10

    One JSP getting org.xml.sax.SAXParseException; Attribute "var" was already specified for element "c:set"

  11. 11

    flaying saucer org.xml.sax.SAXParseException The declaration for the entity HTML.Version must end with >

  12. 12

    Eclipse - glassfish can't start because org.xml.sax.SAXParseException;

  13. 13

    How to fix the error "org.xml.sax.SAXParseException / Content is not allowed in prolog"?

  14. 14

    org.xml.sax.SAXParseException: Content is not allowed in prolog when consuming service in .net

  15. 15

    Java JaxB Unmarshaller giving org.xml.sax.SAXParseException Premature end of file

  16. 16

    Exception for no memory while parsing large XML file in SAX parser

  17. 17

    Android SAX XML parsing error

  18. 18

    Perl XML::SAX partial parsing

  19. 19

    org.xml.sax.SAXParseException; lineNumber: 6; columnNumber: 122; cvc-elt.1: Cannot find the declaration of element 'beans'

  20. 20

    org.xml.sax.SAXParseException;cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found

  21. 21

    Unexpected Token while Parsing JSON

  22. 22

    XML unexpected token

  23. 23

    Running JUnit test in Spring MVC throws: org.xml.sax.SAXParseException; systemId: http://www.springframework.org/schema/mvc/spring-mvc.xsd

  24. 24

    Running JUnit test in Spring MVC throws: org.xml.sax.SAXParseException; systemId: http://www.springframework.org/schema/mvc/spring-mvc.xsd

  25. 25

    Parsing XML using SAX Parser in Python 3

  26. 26

    Parsing and updating xml using SAX parser in java

  27. 27

    Parsing an XML file with Nokogiri SAX parser

  28. 28

    Java : Parsing xml file using SAX/XPATH

  29. 29

    Parsing XML using SAX Parser in Python 3

HotTag

Archive