-1

I have implemented in my app reading a XML. It works fine. But I want to format the text. I've tried in the XML:

 <monumento>
     <horarios><b>L-V:</b> 10 a 20<br/>S-D: 11 a 15</horarios>
     <tarifas>4000</tarifas>
 </monumento>

But the only thing I get if I put HTML character is that the text does not display in my app. I'll have many xml so that I will not always know where to place <b>, <br/>...

How I can do?

Main

StringBuilder builder = new StringBuilder();

        for (HorariosTarifasObj post : helper.posts) {
            builder.append(post.getHorarios());
        } 
        horario2.setText(builder.toString());

        builder = new StringBuilder();
        for (HorariosTarifasObj post : helper.posts) {
            builder.append(post.getTarifas());
        }
        tarifa2.setText(builder.toString());

XMLReader

public void get() {

    try {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser parser = factory.newSAXParser();

        XMLReader reader = parser.getXMLReader();
        reader.setContentHandler(this);

        InputStream inputStream = new URL(URL + monumento + ".xml").openStream();
        reader.parse(new InputSource(inputStream));

    } catch (Exception e) {

    }
}

@Override
public void startElement(String uri, String localName, String qName,
        Attributes attributes) throws SAXException {

    currTag = true;
    currTagVal = "";

    if (localName.equals("monumento")) {
        post = new HorariosTarifasObj();
    }
}

@Override
public void endElement(String uri, String localName, String qName)
        throws SAXException {

    currTag = false;

    if(localName.equalsIgnoreCase("horarios")) {
        post.setHorarios(currTagVal);

    } else if(localName.equalsIgnoreCase("tarifas")) {
        post.setTarifas(currTagVal);

    } else if (localName.equalsIgnoreCase("monumento")) {
        posts.add(post);
    }
}

@Override
public void characters(char[] ch, int start, int length)
        throws SAXException {

    if (currTag) {
        currTagVal = currTagVal + new String(ch, start, length);
        currTag = false;
    }
}
Charlie
  • 325
  • 1
  • 7
  • 22

2 Answers2

1

Try CDATA:

<monumento>
     <horarios><![CDATA[<b>L-V:</b> 10 a 20<br/>S-D: 11 a 15]]></horarios>
     <tarifas>4000</tarifas>
 </monumento>
Manuel Spigolon
  • 11,003
  • 5
  • 50
  • 73
0

In XML, < and > characters are reserved for XML tags. You will need to protect them by replacing them with special encoding characters.

You can use &gt; for > and &lt; for <

(edit) Eomm answer is right, CDATA does this as well, and more simple

Also, to use HTML coding in TextView, you will need to use Html.fromHtml() method

For instance :

tarifa2.setText(Html.fromHtml(builder.toString()));
Yves Delerm
  • 785
  • 4
  • 10