I’m trying to parse an XML file (representing a TV Guide) which looks like the following…
<?xml version="1.0" encoding="utf-8"?>
<channels>
<channel>
<name>BBC ONE</name>
<oid>10029</oid>
...
<programmes>
<programme>
<description>Blah blah blah</description>
<end_time>2013-02-04 01:40:00</end_time>
<episode>9</episode>
<genres>Entertainment</genres>
<oid>10583734</oid>
<season>8</season>
<start_time>2013-02-04 00:15:00</start_time>
<title>The Celebrity Apprentice USA</title>
</programme>
<programme>
..
</programme>
</programmes>
</channel>
<channel>
...
</channel>
</channels>
I’m using two parsers – one for channels and another for programmes but obviously that means I need to retrieve the whole of <programmes>...</programmes> to pass it to the ‘programme’ parser.
I tried the following in the ‘channels’ parser…
public List<XMLTVChannel> parse() {
RootElement rootElement = new RootElement("channels");
final List<XMLTVChannel> channelsList = new ArrayList<XMLTVChannel>();
Element channelElement = rootElement.getChild("channel");
...
// Set the EndTextElementListeners for the <channel> child elements
channelElement.getChild(CHANNEL_OID).setEndTextElementListener(new EndTextElementListener() {
public void end(String body) {
currentChannel.setOid(body);
}
});
...
// HERE'S THE PROBLEM
channelElement.getChild("programmes").setEndTextElementListener(new EndTextElementListener() {
public void end(String body) {
// NEED TO INVOKE XMLTVProgrammeParser HERE
}
});
try {
Xml.parse(getInputStream(), Xml.Encoding.UTF_8, rootElement.getContentHandler());
} catch (Exception e) {
throw new RuntimeException(e);
}
return channelsList;
}
OK, so I’ve Googled and I know exactly what the issue is – the String body parameter passed into the end(...) method should only contain text whereas it’s a mixture of elements and their text.
I’ve read a few similar stackoverflow questions and articles which suggest I need to define my own ContentHandler but I haven’t found anything quite like what I’m trying to do. Is a custom ContentHandler my only option or is there another way?
Do you mean you want this output :
I have modified your xml file a bite :
The Java classes :
Channel
Programme
XMLManager
Main
UPDATE
Here is an example of how I did it using SAX.
Important : I’ve kept my classes Programme and Channel
ChannelsHandler
My new Main
Output in my console :
It is the first time i’m using SAX. Maybe you can find something more efficient but it’s working 🙂
I did not manage the duplicate OID tag for program or channel in my update.