This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes XML and Related Technologies and the fly likes How to read a cdata using sax parser Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "How to read a cdata using sax parser" Watch "How to read a cdata using sax parser" New topic
Author

How to read a cdata using sax parser

riaz rahman
Greenhorn

Joined: Nov 28, 2005
Posts: 17
I am new to XML & i want to read a query from xml.
which will be in cdata tag.
please help me by providing source code for reading cdata usin sax parser.
thanx in advance
Bajji Pat
Ranch Hand

Joined: Apr 05, 2005
Posts: 50
You can use the following snippet- the characters() method would catch any CDATA sections within XML:


[ December 07, 2005: Message edited by: Bajji Pat ]
Anand Gondhiya
Ranch Hand

Joined: Feb 24, 2004
Posts: 155
Thanks for providing the code.
Jeremie Juste
Greenhorn

Joined: Feb 22, 2013
Posts: 2
Hello,

I have still a lot to learn in programming and I'm sorry for this stupid question.
But i was wondering how to use this code ?

Can you modify it to read txt files or html?

Will anyone be kind enough to reply to this question?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41599
    
  55
XML *is* text. But text is not normally XML, and neither is HTML. So, no, you can't use a SAX parser for reading text files in general or HTML.

But the question is: why would you want to? What are you trying to accomplish?


Ping & DNS - my free Android networking tools app
Jeremie Juste
Greenhorn

Joined: Feb 22, 2013
Posts: 2
Many thanks for the help

In fact i'm doing some web scraping and want to get some data found in between the CData.

I have found a nice code doing part of this job.
http://www.java-tips.org/java-se-tips/org.xml.sax/accessing-character-data-cdata-of-xml-element.html

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41599
    
  55
HTML is not generally XML. I suspect that this code will not work for 99% of all web pages in existence (doesn't mean it won't work for any particular page you're interested in, of course, especially if that page happens to be XHTML).
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to read a cdata using sax parser