wood burning stoves 2.0*
The moose likes XML and Related Technologies and the fly likes Parse CDATA with XMLParser in this specific case for Android Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Parse CDATA with XMLParser in this specific case for Android" Watch "Parse CDATA with XMLParser in this specific case for Android" New topic
Author

Parse CDATA with XMLParser in this specific case for Android

Igor Mandic
Greenhorn

Joined: Nov 15, 2011
Posts: 24

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???


Seems legit..
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
Note that the type of a CDATA section is CDATA_SECTION_NODE, not TEXT_NODE
Igor Mandic
Greenhorn

Joined: Nov 15, 2011
Posts: 24

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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Jan 18, 2011
Posts: 500
    
    3
[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

Joined: Nov 15, 2011
Posts: 24

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

Joined: Jan 18, 2011
Posts: 500
    
    3
[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

Joined: Nov 15, 2011
Posts: 24

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

Joined: Oct 24, 2012
Posts: 9
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!

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Parse CDATA with XMLParser in this specific case for Android
 
Similar Threads
parse content:encode
host unresolved error!!
Reading/Parsing an XML doc Question
Reading special Chars from EditText