0

I am trying to parse an xml file which is similar to string.xml file under res folder. But while parsing I am getting exception. Below is my xml file.

<resources>

    <!-- Enrollment Messages -->
    <string name="configure_email_text">No corporate email accounts have been configured on this device. To    configure them, click <b>Configure Email</b></string>
    <string name="invalid_credentials">Authentication failed. Enter valid credentials.</string>
    <string name="invalid_passcode">Authentication failed. Enter valid Passcode.</string>
     <string-array name="lang_support">
        <item>en_US</item>
        <item>en_GB</item>
    </string-array>
      <string name="picker_combined_view_fmt">Combined view (<xliff:g id="count">%s</xliff:g>)</string>
</resources>    

And below is my code which I am using to parse XML.

 private void loadXML()
    {
        // TODO Auto-generated method stub
        InputStream ins = null;
        try{
            //Debug.startMethodTracing("parsingdetail");

            ins=getAssets().open("strings.xml");
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            // factory.setNamespaceAware(true);
            XmlPullParser xpp = factory.newPullParser();
            xpp.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
            xpp.setInput(ins, null);
            parseXml(xpp);
            int j=hMap.size();
            if(j>0){
               System.out.println("Anuj Kumar Jha "+j);
            }
        }catch(XmlPullParserException e){
            System.out.println("anuj kumar jha"+e);
            e.printStackTrace();
        }catch(IOException e){
            System.out.println("anuj kumar jha"+e);
            e.printStackTrace();
        }
        finally{
            if(ins!=null){
                try {
                    ins.close();
                }
                catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

    }


    private void parseXml(XmlPullParser parser) throws XmlPullParserException,IOException
    {
        // TODO Auto-generated method stub
        int eventType=parser.getEventType();
        while(eventType!=XmlPullParser.END_TAG){
            String key=null;
            String value=null;
            switch(eventType){
            case XmlPullParser.START_TAG:
                String name=parser.getName();
                if(name.equalsIgnoreCase("string")){
                    key=parser.getAttributeValue(null,"name");
                    value=parser.nextText();
                    hMap.put(key, value);
                    if(parser.getEventType()!=XmlPullParser.END_TAG){
                        parser.nextTag();
                    }
                }
            }
            eventType=parser.next();
         }  
     }   

And Below is the Exception I am getting.

01-18 12:19:10.506: I/System.out(16115): anuj kumar jhaorg.xmlpull.v1.XmlPullParserException: END_TAG expected (position:START_TAG <b>@5:137 in java.io.InputStreamReader@42b54168) 

I think it is because of tag in first string tag. How to fix this issue?

6
  • Post the contents of parseXml(). Commented Jan 18, 2015 at 6:58
  • Your xml isn't valid. test here Commented Jan 18, 2015 at 7:52
  • Double check if you are parsing the intended XML. Parsing has failed since an end tag has not been encountered for a particular element. Commented Jan 18, 2015 at 7:53
  • It is happening because of presence of <b> tag nested inside <string> tag. But i need that data present iside <b> tag as whole. Commented Jan 18, 2015 at 8:04
  • What is the code for parseXml()? Or from where is it? Commented Jan 18, 2015 at 8:10

1 Answer 1

1

Follow this pattern (which I've found on http://developer.android.com/reference/org/xmlpull/v1/XmlPullParser.html):

     int eventType = xpp.getEventType();
     while (eventType != XmlPullParser.END_DOCUMENT) {
      if(eventType == XmlPullParser.START_TAG) {
          String name=parser.getName();
            if(name.equalsIgnoreCase("string")){
                key=parser.getAttributeValue(null,"name");
                String value = collectText(parser);
                hMap.put(key, value);
            }
      }
      eventType = xpp.next();
     }

private String collectText(XmlPullParser parser){
    StringBuilder sb = new StringBuilder();
    int eventType=parser.getEventType();
    while (eventType != XmlPullParser.END_TAG) {
      if(eventType == XmlPullParser.START_TAG){
        sb.append( collectText(parser) );
      } else if(eventType == XmlPullParser.TEXT) {
        sb.append( xpp.getText() );
      }       
      eventType = xpp.next();
     }
  return sb.toString(); 
}
Sign up to request clarification or add additional context in comments.

4 Comments

This still not working. You can check by using my input xml file.
Hope this works. Test with a "string" element where there is more than one b included with text before, between and after.
it might work for <b> but for <xliff> tag it will fail again. In short string.xml file can contain in a format which can be defined in string.xml file of res folder.
@AnujKumarJha It should not fail for xliff - you call xpp.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false); - I fixed the missing }. Bad connection.

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.