Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Parse CDATA with XMLParser in this specific case for Android

 
Igor Mandic
Greenhorn
Posts: 24
Eclipse IDE Google App Engine Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've seen quite a few posts about this, but actually I did not get any to work. I am building a simple TV guide android application. I simply Use RSS from a tvprofil.net to show whats on TV today. The problem is, I do not know how to Parse CDATA in XML. I am using some standard parser with DOM... at least I think so..

This is a bit of XML:


now, this is my main app:



and the parser class:



there is one more class for Single menu item.. but I think it's irrelevant in this case. Now, I'd just like to see no HTML tags after parsing it and dealing with CDATA... Anyone got idea about this one?

Maybe the thing is about the wrong line:


but, I tried with:



and even:




anyone got idea???
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13056
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that the type of a CDATA section is CDATA_SECTION_NODE, not TEXT_NODE
 
Igor Mandic
Greenhorn
Posts: 24
Eclipse IDE Google App Engine Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William Brogden wrote:Note that the type of a CDATA section is CDATA_SECTION_NODE, not TEXT_NODE


oh, that information changes much ... I think I get it why it does not work, but please, can you help me with my code to fix the problem..

I think it's in this part.. isn't it?

 
Paul Clapham
Sheriff
Pie
Posts: 20771
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're looking for an element which is in a namespace (which you are) then don't use the getElementsByTagName method, use the getElementsByTagNameNS method. Its parameters are namespace URI and element name.
 
g tsuji
Ranch Hand
Posts: 656
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[0] First, you're using a parser default to Namespace non-aware. That's by default. If you change you mind later to prefer a namespace-aware parser, set it at the factory level by adding this.

[1] With your namespace non-aware parser, the prefix _must_ exactly match that of the input rss. That is a big drawback as one day or another that prefix can be changed (that's the idea of the prefix itself which is quite arbitrary). So in this sense it is fine to use KEY_DESC as such with getElementsByTagName() method again as such.

[2] The problem is that you use getFirstChild to access to the data. That the conditional should take care of that fact in the case of a CDATA section.

[2.1] It is not the only way to get access to the text content. You can do directly with the elem itself (which is itself always an ELEMENT_NODE), like this under the condition that the implementation supports dom level 3 recommendation --- unfortunately, it looks like it was not yet on the Android at some time, thing can be changed since and this rests to be ascertained if you decide to use it.
 
Igor Mandic
Greenhorn
Posts: 24
Eclipse IDE Google App Engine Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now, I do not know if I am so stupid, or it's becouse I never have dealt myself with parsing, but, still, even I changed those few lines I can't set it to work. It still displays <title> and <pubDate> as it should, but the <description>, the one I need the most is still missing. I kneel before you, and beg you.... if you have any spare time, please fix my code so I can just use it directly... I tried so many things, but nothing seams to work. Honestly, I am on this same point for more than a week.
 
g tsuji
Ranch Hand
Posts: 656
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[3]
...even I changed those few lines I can't set it to work.

I suppose you faithfully represent the rss up to the detail of the prefix "content". The only plausible explanation is that dbf is namespace aware. If that is the case, that is quite ok, you can deal with it. But, for the first instance, I want to ascertain this fact by modifying least possible lines. Once this is verified, you can deal with the same problem with namespace proper: and that is not very complicated to improve in that front. That's for the future.

[3.1] Just a side-note to let another issue in peace. You've set coalescing to true. That should in principle have taken care of the test condition Node.CDATA_SECTION_NODE as the same node would then be treated as a TEXT_NODE. That is another possible hint that the parser might just have been a namespace aware parser for that to fail to getting the correct result.

[3.2] I suggest as an intermediate step of verifying what is wrong. Add this just after the setting up of dbf.

And then proceed with the same code at present. Try it and see what results.
 
Igor Mandic
Greenhorn
Posts: 24
Eclipse IDE Google App Engine Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much allmighty Master of programming.
I managed to make it somehow. I made a few new methods and made these:




and ofc. I changed this:


and just when I call it I use thing like this:



Thank you very much.. this works like a charm! You are my saviour!
 
Matt Partner
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone!

I have the same code for parsing RSS XML feed and works very nice! Thanks to all people who helped with this because I had the same problem as Igor Mandic.
But now I have a problem that I can't solve it.

I want to show the description ONLY in SingleMenuItemActivity.class because this code show you the description in Main and SingleMenuItemActivity
.
How can I do this? I tried to delete the KEY_DESC in the ListAdapter but not works. Deleting map.put(KEY_DESC, parser.getValue(e, KEY_DESC)); too, but doesn't works.

Any idea? Thanks to all ^^

See you!

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic