3

I am trying to parse an xml file withSaxParser on Android.

This is my xml file:

<?xml version="1.0" encoding="UTF-8"?>
<cars>
    <car model="CitroenC3">
        <maintenances>
            <xm:maintenance xmlns:xm="it.a.b.android.c.car.m" distance="" price="">
                <xm:type></xm:type>
            </xm:maintenance>
        </maintenances>
        <chargings>
            <xc:charging xmlns:xc="it.a.b.c.fuelconsumption.car.m" quantity="18" price="20" distance="400" consumption="14"/>
        </chargings>
    </car>
</cars>

And this is the code:

// Handling XML 
    SAXParserFactory spf = SAXParserFactory.newInstance();
    SAXParser sp = spf.newSAXParser();
    XMLReader xr = sp.getXMLReader();

    XmlResourceParser parser = getResources().getXml(R.xml.data);

    // Create handler to handle XML Tags ( extends DefaultHandler ) 
    DataSaxHandler myXMLHandler = new DataSaxHandler();
    xr.setContentHandler(myXMLHandler);
    InputStream is= getResources().openRawResource(R.xml.data);
    xr.parse(new InputSource(is));

After xr.parse I have the Exception:

03-22 15:24:04.248: INFO/System.out(415): XML Pasing Excpetion =

org.apache.harmony.xml.ExpatParser$ParseException: At line 1, column 0: not well-formed (invalid token)

What may be wrong? Thanks a lot.

skaffman
  • 398,947
  • 96
  • 818
  • 769
michele
  • 26,348
  • 30
  • 111
  • 168

1 Answers1

6

AFAIR, any xml file under res/ folder is compiled before it's placed in .apk. Try to move your XML-file to assets/ folder and load it from there:

xr.parse(new InputSource(getAssets().open("data.xml")));
GrAnd
  • 10,141
  • 3
  • 31
  • 43
  • I have returned java.io.FileNotFoundException: data.xml – michele Mar 22 '11 at 17:31
  • Maybe you did not refresh your project in Eclipce after you moved the file. Because in my project I have exactly the same situation (parsing xml file from resources) and just `assets/` folder works for me. – GrAnd Mar 22 '11 at 18:48