Hi guys i am using sax to extract data from an xml file but i can't seem to
discover how to extract <![CDATA[%s]]> from the xml below is the xml and the java code
//java
codepublic class XmlParser {
public XmlParser(String xml) {
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
DefaultHandler handler = new DefaultHandler() {
// boolean item = false;
boolean title = false;
boolean link = false;
boolean pubDate = false;
boolean cdata = false;
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
//
// if (qName.equalsIgnoreCase("ITEM")) {
// item = true;
// }
if (qName.equalsIgnoreCase("TITLE")) {
title = true;
}
if (qName.equalsIgnoreCase("LINK")) {
link = true;
}
if (qName.equalsIgnoreCase("PUBDATE")) {
pubDate = true;
}
if(qName.equalsIgnoreCase("![CDATA[%s]]")){
cdata = true;
}
}
public void endElement(String uri, String localName,
String qName) throws SAXException {
}
public void characters(char ch[], int start, int length) throws SAXException {
// if (item) {
// System.out.println("First Name : " + new String(ch, start, length));
// bfname = false;
// }
if (title) {
System.out.println("======================================");
System.out.println("title : " + new String(ch, start, length));
title = false;
}
if (link) {
System.out.println("link : " + new String(ch, start, length));
link = false;
}
if(cdata){
System.out.println("here");
}
if (pubDate) {
System.out.println("pubDate : " + new String(ch, start, length));
System.out.println("======================================");
pubDate = false;
}
}
};
StringReader a = new StringReader(xml);
InputSource s = new InputSource(a);
saxParser.parse(s, handler);
} catch (ParserConfigurationException | SAXException | IOException e) {
System.out.println("Error");
System.out.println(e);
}
}
}
This is the xml file
<item>
<title>
Financial constraints compel Kwabre East Assembly to put freeze on new projects
</title>
<link>
http://www.myjoyonline.com/news/2014/April-18th/financial-constraints-compel-kwabre-east-assembly-to-put-freeze-on-new-projects.php
</link>
<guid isPermaLink="false">...</guid>
<description>local news, ghana local news</description>
<![CDATA[
<img align="left" hspace="5" src="http://photos.myjoyonline.com/photos/news/201309/699543486_222885.jpg" alt="Financial constraints compel Kwabre East Assembly to put freeze on new projects" />
]]>
<pubDate>2014-04-18T07:04:43+00:00</pubDate>
</item>