0

I am trying to parse my xml file resource with SaxParser. I have created my DataHandler but I don't know how indicate to XmlReader the location of data.xml that is in res/xml/.

What is the correct parameter for InputSource object?

    XmlResourceParser parser = getResources().getXml(R.xml.data);       
    SAXParserFactory spf = SAXParserFactory.newInstance();
    SAXParser sp = spf.newSAXParser();
    XMLReader xr = sp.getXMLReader();

    // Create handler to handle XML Tags ( extends DefaultHandler ) 
    DataSaxHandler myXMLHandler = new DataSaxHandler();
    xr.setContentHandler(myXMLHandler);
    //R.xml.data is my xml file
InputSource is=new InputSource(getResources().getXml(R.xml.data));  //getResources... is wrong say Eclipse

    xr.parse(is);       

Thanks a lot.

1

1 Answer 1

1

The problem is that the call to getResources().getXml(int id) is returning a XmlResourceParser, and there is no InputSource constructor that takes an XmlResourceParser.

If you want to stick with the SaxParser, you'll need to open up an InputStream via Resources#openRawResource(int id), and then pass that to the InputSource constructor. You'll also need to move the file to res/raw to use the openRawResource function.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.