Pregunta

I am trying to parse a response from a http request. This is the String I made from that response:

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://my-web-address.com/webservices/">&lt;NewDataSet&gt;
    &lt;Case&gt;
    &lt;ID&gt;260023277&lt;/ID&gt;
    &lt;CaseNumber&gt;8931-04-03&lt;/CaseNumber&gt;

     &lt;/Case&gt;

    .
    .
    .
    &lt;/NewDataSet&gt;</string>

All elements (represented by dots) are valid.
Here's how I'm parsing this xml using sax:

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

        InputSource inputSource = new InputSource( new StringReader( responce ) ); // responce is my String containing XML.
        CasesParser myXMLHandler = new CasesParser();
        xr.setContentHandler(myXMLHandler);
        xr.parse(inputSource);

CaseParser class extends DefaultHandler and implements all functions in conventional way.

So the exact error is that the parser is not detecting anything other than "string" element. It detects it in the startElement() method of CaseParser and then also in endElement() method. No other element is found by the parser in between.
Upon retrieving value of "string" I get only '<' character. Nothing else.

What might be the problem?

¿Fue útil?

Solución 2

This is the workaround I got based on Don's Answer.
I parsed the content inside <string> tag with XMLPullParser, stored the response in a String and then fed that String (which is now the XML without the <string> tag)
Here's a little sample if anyone is going through the same problem:

  XmlPullParserFactory factory = null;
      String parsed = "";

         try {
             factory = XmlPullParserFactory.newInstance();
             factory.setNamespaceAware(true);
             XmlPullParser xpp = factory.newPullParser();
            xpp.setInput(new StringReader (responce));
            int eventType = xpp.getEventType();
             while (eventType != XmlPullParser.END_DOCUMENT) {
              if(eventType == XmlPullParser.START_DOCUMENT) {
                  Log.d(Constants.TAG,"Start document");
              } else if(eventType == XmlPullParser.END_DOCUMENT) {
                  Log.d(Constants.TAG,"End document");
              } else if(eventType == XmlPullParser.START_TAG) {
                  Log.d(Constants.TAG,"Start tag "+xpp.getName());
              } else if(eventType == XmlPullParser.END_TAG) {
                  Log.d(Constants.TAG,"End tag "+xpp.getName());
              } else if(eventType == XmlPullParser.TEXT) {
                  Log.d(Constants.TAG,"Text "+xpp.getText());
                  parsed = xpp.getText();
              }
              eventType = xpp.next();
             }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
         xmlResponse = parsed;  

So from here, I feed in the xmlResponse object to my CaseParser object which was already written to handle and store the xml in my objects.
Hope it helps someone else too. Cheers!

Otros consejos

Your main problem is that all the xml tags are escaped, which makes them not really xml tags.

If possible, you might want to talk to whoever produces this, and get them to change it so this data is not escaped.

If that plea for better data is impossible, likely what you need is to read all the contents of the <string> tag into a string and then start another parser to parse the contents of that string, which the first parse will have unescaped.

If all you're getting is a single '<' character, you've likely made the common mistake of expecting the characters method to be called only once. It can be called multiple times, and you need to collect the data into a buffer which is then converted to a string in the endElement method.

For example code including this collection logic, see my answer to a related question.

Once you've got this parse properly getting all the contents of the <string> tag into a Java String, you'll need to use that string as the content for another parse.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top