I want to get some values from a news site with SAXParser. But its' structure is hard to me, I am new at XML and SAX.
Issue: News Site using SAME TAG NAME for site name and news title for its XML.
When I run Java Code It is working without error but problem is about outputs.
How can I only get <item> tag's child tag: <title> ? I don't want to show site title on my application. It is big issue for me.
XML Side
<channel>
<title>Site Name</title>
<item>
<title>News Title!</title>
</item>
</channel>
Java Side
There is no error in java file :)
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
DefaultHandler handler = new DefaultHandler() {
boolean newsTitle = false;
public void startElement(String uri, String localName,
String qName, Attributes attributes)
throws SAXException {
//System.out.println("Start Element :" + qName);
if (qName.equalsIgnoreCase("title")) {
newsTitle = true;
}
}
public void endElement(String uri, String localName,
String qName) throws SAXException {
//System.out.println("End Element :" + qName);
}
public void characters(char ch[], int start, int length)
throws SAXException {
if (newsTitle) {
System.out.println("Title : "
+ new String(ch, start, length));
newsTitle = false;
}
}
};
saxParser
.parse("C:\\ntv.xml",handler);
}
catch (Exception e) {
e.printStackTrace();
}
OUTPUT:
Title : Site Name
Title : News Title