hakiko hakiko -4 years ago 63
Java Question

SAXParser - Handle tags with same text at different level in XML structure

I want to get some values from a news site with

. 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
tag's child tag:
? I don't want to show site title on my application. It is big issue for me.

XML Side


<title>Site Name</title>

<title>News Title!</title>


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;




catch (Exception e) {


Title : Site Name

Title : News Title

Answer Source

You can use XPath rather than parsing your XML using SAX.

XPath expression for your case is:


Example code:

import org.xml.sax.InputSource;

import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.io.StringReader;

public class XPathTest {

    public static void main(String[] args) throws XPathExpressionException {

        String xml = "<channel>\n" +
                "\n" +
                "   <title>Site Name</title>\n" +
                "\n" +
                "   <item>  \n" +
                "       <title>News Title!</title>       \n" +
                "   </item>\n" +
                "\n" +

        Object result = XPathFactory.newInstance().newXPath().compile("/channel/item/title").evaluate(new InputSource(new StringReader(xml)));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download