cavallo
cavallo

Reputation: 4414

How to parse xml with HTML tags in between android

Hi friends i got a xml file with lot of content and i am able to parse the content with pull parsing but the problem is there are html links in between the tags and while parsing i only get the text after the html link. The text before link is lost

for ex i have a tag

 <table> this is my text before html <a href="i got a link here">link</a> this is my text after link. 
 </table>

here i get the text after the link in the output, how to over come this...thanks in advance

Upvotes: 1

Views: 2632

Answers (3)

Giulio Piancastelli
Giulio Piancastelli

Reputation: 15808

Works for me, so you would have to show something more - the code you are using to parse the XML, or a real snippet from the original XML document, for instance - in order for us to be more helpful.

In the meantime, here is a quick demo/example that I run to see how things were working. I have an XML document in a fixed String called DOCUMENT that contains the following:

<root>
  <element>
    <table>text before <a href="url">link</a> text after</table>
  </element>
  <element>
    free text
  </element>
</root>

A very simple loop is in charge of parsing this document and showing in the log the text that the parser is able to extract for each element:

XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(new StringReader(DOCUMENT));
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
    if (eventType == XmlPullParser.START_DOCUMENT) {
        Log.d("XmlDemo", "Start document");
    } else if (eventType == XmlPullParser.START_TAG) {
        Log.d("XmlDemo", "Start tag: " + xpp.getName());
    } else if (eventType == XmlPullParser.END_TAG) {
        Log.d("XmlDemo", "End tag: " + xpp.getName());
    } else if (eventType == XmlPullParser.TEXT) {
        Log.d("XmlDemo", "Text: " + xpp.getText());
    }
    eventType = xpp.next();
}
Log.d("XmlDemo", "End document");

Then, on a run, the log shows (among others) the following entries:

02-06 15:45:38.981: D/XmlDemo(371): Start tag: table
02-06 15:45:39.001: D/XmlDemo(371): Text: text before 
02-06 15:45:39.001: D/XmlDemo(371): Start tag: a
02-06 15:45:39.021: D/XmlDemo(371): Text: link
02-06 15:45:39.021: D/XmlDemo(371): End tag: a
02-06 15:45:39.041: D/XmlDemo(371): Text:  text after
02-06 15:45:39.041: D/XmlDemo(371): End tag: table

As you see, the parser is able to extract the text before and after the link, albeit not in a single operation.

Upvotes: 0

Gangnus
Gangnus

Reputation: 24464

Use

XmlPullParser parser =...;
...
parser.getAttributeValue(null, "href");

for taking Attributes. Of course, you can't take them as Text or Tag

Upvotes: 1

Sergey Benner
Sergey Benner

Reputation: 4431

one of ways:

String a = "<table> this is my text before html <a href=\"http://stackoverflow.com/questions/9161924/how-to-parse-xml-with-html-tags-in-between-android\">link</a> this is my text after link.  </table>";
String link = a.substring(a.indexOf("href=\"")+6,a.indexOf("\">"));
System.out.println(link);

hope it helps.

Upvotes: 0

Related Questions