janki janki - 1 year ago 92
Android Question

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

& ,' , "


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


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

PullHendler class

XmlPullParserFactory factory = XmlPullParserFactory.newInstance();

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"?>
<Line>Live your life sharing the Beauty & Happiness, that you have today.

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();

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

case XmlPullParser.END_TAG:
if (tagname.equalsIgnoreCase("Line")) {

eventType = parser.next();

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

return lines;

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download