1

I have an xml parser that works perfectly when I load it from a file:

private void getParsedXML(int id, Context context) throws Exception {
        /* Get a SAXParser from the SAXPArserFactory. */
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();

        /* Get the XMLReader of the SAXParser we created. */
        XMLReader xr = sp.getXMLReader();
        /* Create a new ContentHandler and apply it to the XML-Reader */
        XMLHandlerLevel myExampleHandler = new XMLHandlerLevel();
        xr.setContentHandler(myExampleHandler);
        /* Load xml file from raw folder*/
        InputStream in = context.getResources().openRawResource(id); 

        /* Begin parsing */
        xr.parse(new InputSource(in));
        this.levelData = myExampleHandler.getLevelData();
        in.close();
    }

But now I also get somme content form a server. The XML is then send as a String and this is where it blocks:

private void getParsedXML(String s) throws Exception{
        if(this.levelData == null){
            InputStream in = StringToStream(s);
            /* Get a SAXParser from the SAXPArserFactory. */
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            XMLHandlerLevel myExampleHandler = new XMLHandlerLevel();
            XMLReader xr = sp.getXMLReader();
            /* Begin parsing */
            xr.parse(new InputSource(in));



            this.levelData = myExampleHandler.getLevelData();
            Log.e("jason",(levelData!=null)?levelData.toString():null);
            in.close();
        }
    }

and here is the StringToString Function

    public static InputStream StringToStream(String text) {
            InputStream is = null;
            /*
             * Convert String to InputStream using ByteArrayInputStream 
             * class. This class constructor takes the string byte array 
             * which can be done by calling the getBytes() method.
             */
            try {
                is = new ByteArrayInputStream(text.getBytes("UTF-8"));
                 is.reset();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (Exception e){
                Log.e("jason","failed again");
            }
            return is;
        }

But this doesn't work...

I am sure of the content of the String. and I tested the StringToStream function it works fine.

I also put logs in XMLHandlerLevel to check what was happening and NOTHING the function startDocument() isn't even called but I get no error message no exceptions so I'm completely lost to explain this.

to resume: My XMLParser works fine when the Stream comes from context.getResources().openRawResource(id); but not when it comes form a String Why?

Thanks for any ideas

Jason

5
  • What do you mean it doesn't work? Is an exception thrown? Commented Apr 14, 2011 at 11:31
  • 2
    You dont have the make the text into an InputStream, a StringReader would be better. But the pasrsing code is essentially the same - there must be something else wrong? In cases like this, make the parse method take in the xml source in such a way that the working and - non-working code share some logic, so that you can eliminitate the shared code as a problem.. Commented Apr 14, 2011 at 11:34
  • @kgiannakakis there is no Exception Thrown that is what is annoying me the most. @Thomas tis already what I've done the only differences is the creation on the InputSource Commented Apr 14, 2011 at 12:06
  • You don't appear to be setting the ContentHandler in the code path that takes a String (see my updated answer). Commented Apr 14, 2011 at 12:18
  • @Blaise Doughan: thanks ! I can't belive how blind I was on this one :S Commented Apr 14, 2011 at 12:33

2 Answers 2

4

You don't appear to be setting a content handler in the code path that takes a string.

Why not wrap the String in a StringReader and parse that instead?

InputSource is = new InputSource(new StringReader(string));
xr.parse(is);

You example (modified):

private void getParsedXML(String s) throws Exception{
    if(this.levelData == null){
        /* Get a SAXParser from the SAXPArserFactory. */
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLHandlerLevel myExampleHandler = new XMLHandlerLevel();
        XMLReader xr = sp.getXMLReader();
        /* Begin parsing */
        InputSource is = new InputSource(new StringReader(string));
        xr.parse(is);

        this.levelData = myExampleHandler.getLevelData();
        Log.e("jason",(levelData!=null)?levelData.toString():null);
        in.close();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

-1

i am not sure that it will help u but there is a external jar file called Jsoup.jar add it to your project, it have the APIs of parsing xml,html...see tutorial on internet....

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.