Search code examples
androidxmlandroid-studioxmlpullparser

Ignore character which is not supporting in XML


How to Ignore '&' charterer while reading XML file. what I want is, First I read xml file and then Print Content to list view.. but in some case my content contain some special charecter Like

& ,' , "

etc..

how can I ignore? this is my code for read xml from sdcard.

MainActivity

 XmlPullHandler parser = new XmlPullHandler();
            File file = new File(Environment.getExternalStorageDirectory()+ "/myfile.xml");
            FileInputStream fis = new FileInputStream(file);

PullHendler class

 XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);

            XmlPullParser  parser = factory.newPullParser();

             parser.setInput(new InputStreamReader(is));

OR Is this Possible to replace character while reading file.. (i.e. & is replaced by &amp)

this is Myfile.Xml file

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
 <MyLine>   
<Line>Live your life sharing the Beauty & Happiness, that you have today.
</Line> 
</MyLine>

this is my pullHandlerclass

 int eventType = parser.getEventType();
      while (eventType != XmlPullParser.END_DOCUMENT) {
                    String tagname = parser.getName();
                    switch (eventType) {
                        case XmlPullParser.START_TAG:
                            if (tagname.equalsIgnoreCase("Line")) {

                                line = new Lines();
                            }
                            break;

                        case XmlPullParser.TEXT:
                            text = parser.getText();
                            break;

                        case XmlPullParser.END_TAG:
                            if (tagname.equalsIgnoreCase("Line")) {
                                lines.add(line);
                                lines.setLine(text);
                            }
                            break;

                        default:
                            break;
                    }
                    eventType = parser.next();
                }

            } catch (XmlPullParserException e) {e.printStackTrace();} catch (IOException e)
            {
                e.printStackTrace();
            } catch (NullPointerException e){
                e.printStackTrace();
            }

            return lines;
        }

Solution

  • You can try like this. Add replacements for other HTML entities after line = line.replaceAll("&", "&amp;")

    String fileData = "";
    String line = "";
    
    BufferedReader br=new BufferedReader(new InputStreamReader(is));
    
    while((line = br.readLine()) != null)
    {
        line = line.replace("&", "&amp;");
        fileData += line;
        fileData += "\n";
    }
    
    parser.setInput(new StringReader(fileData));  
    

    This is not efficient though as memory goes because it will load the entire XML file into a string. I'd be able to provide a better solution if you show what you're doing with your XML parser.