Noby
Noby

Reputation: 6602

How to use < symbol in xml file...?

I have a XML file in assets folder.

I am parsing it in my Activity and displaying it.

In XML file I has a data with < symbol, I use &lt; at < symbol.

But, the symbol is not displying and text after the symbol only i am getting.

ex "hi &lt; hello"

parsing result will be only hello

parsing code

try {
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            XMLReader xr = sp.getXMLReader();
            SecondHandler shandler = new SecondHandler();
            xr.setContentHandler(shandler);
            InputStream in = this.getAssets().open(fileName);
            xr.parse(new InputSource(in));
            itemlist = shandler.getParsedData();            
        } catch (Exception e) {
            System.out.println("Error : " + e);
        }

            Map<String, String> item = (Map<String, String>) list.get(5);
        String qus = item.get("question");
        String ans = item.get("answer");
    }

xml file..

..........
<dict>
            <question>hello</question>
            <answer>I am &lt; 5 you</answer>
        </dict>
......

handler code.

public class SecondHandler extends DefaultHandler {
    private String tagName;
    @SuppressWarnings("rawtypes")
    private ArrayList<Map> dataSet;
    private Map<String, String> dictionary;

    @SuppressWarnings("rawtypes")
    public ArrayList<Map> getParsedData() {
        return dataSet;
    }

    @Override
    public void startDocument() throws SAXException {

    }

    @Override
    public void endDocument() throws SAXException {
        // Nothing to do
    }

    @SuppressWarnings("rawtypes")
    @Override
    public void startElement(String namespaceURI, String localName,
            String qName, Attributes atts) throws SAXException {
        tagName = localName;
        if (localName.equals("array")) {
            this.dataSet = new ArrayList<Map>();
        } else if (localName.equals("dict")) {
            dictionary = new HashMap<String, String>();
        }
    }

    @Override
    public void endElement(String namespaceURI, String localName, String qName)
            throws SAXException {
        if (localName.equals("array")) {
        } else if (localName.equals("dict")) {
            dataSet.add(dictionary);
        }
    }

    @Override
    public void characters(char ch[], int start, int length) {
        String string = new String(ch, start, length);
        string = string.replaceAll(" ", "");
        string = string.replaceAll("\n", "");
        string = string.replaceAll("\t", "");       
        if (string.length() > 0 && string != null) {
            dictionary.put(tagName, new String(ch, start, length));
            // System.out.println("Dictionary : " + dictionary);
        }
    }

}

How to solve this problem

Thanks in advance...!

Upvotes: 0

Views: 795

Answers (2)

Michael Kay
Michael Kay

Reputation: 163458

A SAX parser can supply character data to the ContentHandler in as many calls of the characters() method as it chooses. Your characters() method is putting each of the substrings in the same hashtable entry, overwriting any previous substrings; you need to concatenate them.

Upvotes: 1

Jack Patoliya
Jack Patoliya

Reputation: 517

may be you directly use "<" in xml file write ,

So use Value-->String class

==>string name="temperature_lt" value is= Temperature & l t;(Note here ignore space)

and extractin xml file

==>android:text="@string/temperature_lt"

try it,

Upvotes: 0

Related Questions