Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9233797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:38:15+00:00 2026-06-18T06:38:15+00:00

I’m trying to parse an XML file (representing a TV Guide) which looks like

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T06:38:16+00:00Added an answer on June 18, 2026 at 6:38 am

    Do you mean you want this output :

     BBC ONE
    10029
    ------------------------
    The Celebrity Apprentice USA
    2013-02-04 00:15:00 - 2013-02-04 01:40:00
    Entertainment
    Season : 8 / Episode : 9
    Description:
    Blah blah blah
    10583734
    **********************
    The Celebrity Apprentice USA
    2013-02-04 01:45:00 - 2013-02-04 02:25:00
    Entertainment
    Season : 8 / Episode : 10
    Description:
    Blah blah blah
    10583735
    **********************
    //////////////////////////
    BBC TWO
    10030
    ------------------------
    American Dad
    2013-02-04 00:30:00 - 2013-02-04 01:25:00
    Cartoon
    Season : 14 / Episode : 1
    Description:
    Blah blah blah
    10583734
    **********************
    American Dad
    2013-02-04 01:30:00 - 2013-02-04 02:15:00
    Cartoon
    Season : 14 / Episode : 2
    Description:
    Blah blah blah
    10583735
    **********************
    //////////////////////////
    

    I have modified your xml file a bite :

        <?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>
            <description>Blah blah blah</description>
            <end_time>2013-02-04 02:25:00</end_time>
            <episode>10</episode>
            <genres>Entertainment</genres>
            <oid>10583735</oid>
            <season>8</season>
            <start_time>2013-02-04 01:45:00</start_time>
            <title>The Celebrity Apprentice USA</title>
          </programme>
        </programmes>
      </channel>
      <channel>
          <name>BBC TWO</name>
          <oid>10030</oid>
          <programmes>
          <programme>
            <description>Blah blah blah</description>
            <end_time>2013-02-04 01:25:00</end_time>
            <episode>1</episode>
            <genres>Cartoon</genres>
            <oid>10583734</oid>
            <season>14</season>
            <start_time>2013-02-04 00:30:00</start_time>
            <title>American Dad</title>
          </programme>
           <programme>
            <description>Blah blah blah</description>
            <end_time>2013-02-04 02:15:00</end_time>
            <episode>2</episode>
            <genres>Cartoon</genres>
            <oid>10583735</oid>
            <season>14</season>
            <start_time>2013-02-04 01:30:00</start_time>
            <title>American Dad</title>
          </programme>
        </programmes>
      </channel>
    </channels>
    

    The Java classes :

    Channel

    public class Channel {
    
            private String name;
            private String oid;
            private ArrayList<Programme> alProgrammes;
    
            public Channel(){}
    
            public String getName() {
                return name;
            }
    
            public void setName(String name) {
                this.name = name;
            }
    
            public String getOid() {
                return oid;
            }
    
            public void setOid(String oid) {
                this.oid = oid;
            }
    
            public ArrayList<Programme> getAlProgrammes() {
                return alProgrammes;
            }
    
            public void setAlProgrammes(ArrayList<Programme> alProgrammes) {
                this.alProgrammes = alProgrammes;
            }
    
    
        }
    

    Programme

     public class Programme {
    
        private String description;
        private String end_time;
        private String episode;
        private String genres;
        private String oid;
        private String season;
        private String start_time;
        private String title;
    
    
    
        public Programme() {
        }
    
        //Getters / Setters
        public String getDescription() {
            return description;
        }
        public void setDescription(String description) {
            this.description = description;
        }
        public String getEnd_time() {
            return end_time;
        }
        public void setEnd_time(String end_time) {
            this.end_time = end_time;
        }
        public String getEpisode() {
            return episode;
        }
        public void setEpisode(String episode) {
            this.episode = episode;
        }
        public String getGenres() {
            return genres;
        }
        public void setGenres(String genres) {
            this.genres = genres;
        }
        public String getOid() {
            return oid;
        }
        public void setOid(String oid) {
            this.oid = oid;
        }
        public String getSeason() {
            return season;
        }
        public void setSeason(String season) {
            this.season = season;
        }
        public String getStart_time() {
            return start_time;
        }
        public void setStart_time(String start_time) {
            this.start_time = start_time;
        }
        public String getTitle() {
            return title;
        }
        public void setTitle(String title) {
            this.title = title;
        }
    
    }
    

    XMLManager

    public final class XMLManager {
    
        public static ArrayList<Channel> getAlChannels(){
    
              DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
              DocumentBuilder db = null;
              Document doc = null;
              ArrayList<Channel> alChannels = new ArrayList<>();
    
              try {
    
                db = dbf.newDocumentBuilder();
                doc = db.parse(new File("D:\\Loic_Workspace\\Test2\\res\\test.xml"));
                NodeList ndListChannels = doc.getElementsByTagName("channel");
    
                Integer channelsCount = ndListChannels.getLength();
                NodeList ndListChannel = null;
                Integer ndListChannelLength = null;
                Channel channel = null;
                NodeList ndListProgrammes = null;
                for(int i=0;i<channelsCount;i++){
    
                    ndListChannel = ndListChannels.item(i).getChildNodes();
                    ndListChannelLength = ndListChannel.getLength();
                    channel = new Channel();
                    for(int j=0;j<ndListChannelLength;j++){
    
                        Node currentNode = ndListChannel.item(j);
                        String currentNodeName = currentNode.getNodeName();
                        String value = currentNode.getTextContent();
    
                        if(currentNodeName.equals("name")){
                            channel.setName(value);
                        }
                        if(currentNodeName.equals("oid")){
                            channel.setOid(value);
                        }
                        if(currentNodeName.equals("programmes")){
                            ndListProgrammes = currentNode.getChildNodes();
                            ArrayList<Programme> alProgrammes = new ArrayList<>();
                            for(int k=0;k<ndListProgrammes.getLength();k++){
    
                                Node ndProgrammes = ndListProgrammes.item(k);
                                if(ndProgrammes.hasChildNodes()){
    
                                    NodeList ndListProgramme = ndProgrammes.getChildNodes();
                                    Integer ndListProgrammeLength = ndListProgramme.getLength();
                                    Programme programme = new Programme();
                                    for(int l=0;l<ndListProgrammeLength;l++){
    
                                        Node  ndProgramme = ndListProgramme.item(l);
                                        String nodeProgrameName = ndProgramme.getNodeName();
                                        String nodeProgrameValue = ndProgramme.getTextContent();
                                        if(nodeProgrameName.equals("description")){
                                            programme.setDescription(nodeProgrameValue);
                                        }
                                        if(nodeProgrameName.equals("end_time")){
    
                                            programme.setEnd_time(nodeProgrameValue);
                                        }
                                        if(nodeProgrameName.equals("episode")){
                                            programme.setEpisode(nodeProgrameValue);
                                        }
                                        if(nodeProgrameName.equals("genres")){
                                            programme.setGenres(nodeProgrameValue);
                                        }
                                        if(nodeProgrameName.equals("oid")){
                                            programme.setOid(nodeProgrameValue);
                                        }
                                        if(nodeProgrameName.equals("season")){
                                            programme.setSeason(nodeProgrameValue);
                                        }
                                        if(nodeProgrameName.equals("start_time")){
                                            programme.setStart_time(nodeProgrameValue);
                                        }
                                        if(nodeProgrameName.equals("title")){
                                            programme.setTitle(nodeProgrameValue);
                                        }
    
                                    }
    
                                    alProgrammes.add(programme);
    
                                }
    
                            }
    
                            channel.setAlProgrammes(alProgrammes);
    
                        }
    
                    }
    
                    alChannels.add(channel);
    
                }
    
    
    
              } catch (ParserConfigurationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SAXException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
              return alChannels;
    
        }
    
    
    
    }
    

    Main

    public class MyMain {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
    
    
            ArrayList<Channel> alChannels = XMLManager.getAlChannels();
            for(Channel c:alChannels){
                System.out.println(c.getName());
                System.out.println(c.getOid());
                System.out.println("------------------------");
                for(Programme p:c.getAlProgrammes()){
                    System.out.println(p.getTitle());
                    System.out.println(p.getStart_time()+" - "+p.getEnd_time());
                    System.out.println(p.getGenres());
                    System.out.println("Season : "+p.getSeason()+" / Episode : "+p.getEpisode());
                    System.out.println("Description:\n"+p.getDescription());
                    System.out.println(p.getOid());
                    System.out.println("**********************");
                }
    
                System.out.println("//////////////////////////");
    
            }
    
        }
    
    }
    

    UPDATE

    Here is an example of how I did it using SAX.

    Important : I’ve kept my classes Programme and Channel

    ChannelsHandler

    public class ChannelsHandler extends DefaultHandler{
    
        private ArrayList<Channel> tvGuide;
        private Channel channel;
        private ArrayList<Programme> alProgrammes;
        private Programme programme;
        private String reading;
    
        public ChannelsHandler(){
            super();
        }
    
        @Override
        public void startElement(String uri, String localName, String qName,
                Attributes attributes) throws SAXException {
    
            if(qName.equals("channels")){
                tvGuide = new ArrayList<>();
            }else if(qName.equals("channel")){
                channel = new Channel();
            }
            else if(qName.equals("channel")){
                channel = new Channel();
            }
            else if(qName.equals("programmes")){
                alProgrammes = new ArrayList<>();
            }
            else if(qName.equals("programme")){
                programme = new Programme();
            }
    
        }
    
        @Override
        public void characters(char[] ch, int start, int length)
                throws SAXException {
            reading = new String(ch, start, length);
        }
    
        @Override
        public void endElement(String uri, String localName, String qName)
                throws SAXException {
    
            if(qName.equals("channel")){
                tvGuide.add(channel);
                channel = null;
            }
            if(qName.equals("name")){
                channel.setName(reading);
            }
            else if(qName.equals("programmes")){
                channel.setAlProgrammes(alProgrammes);
                alProgrammes = new ArrayList<>();
            }
            else if(qName.equals("programme")){
                alProgrammes.add(programme);
                programme = null;
            }
            else if(qName.equals("description")){
                programme.setDescription(reading);
            }
            else if(qName.equals("end_time")){
                programme.setEnd_time(reading);
            }
            else if(qName.equals("episode")){
                programme.setEpisode(reading);
            }
            else if(qName.equals("genres")){
                programme.setGenres(reading);
            }
            else if(qName.equals("season")){
                programme.setSeason(reading);
            }
            else if(qName.equals("start_time")){
                programme.setStart_time(reading);
            }
            else if(qName.equals("title")){
                programme.setTitle(reading);
            }
    
        }
    
        public ArrayList<Channel> getTVGuide(){
            return tvGuide;
        }
    
    
    
    }
    

    My new Main

    public static void main(String[] args) {
    
            SAXParserFactory factory = SAXParserFactory.newInstance();
            try {
                SAXParser parser = factory.newSAXParser();
                File file = new File("D:\\Loic_Workspace\\TestSAX\\res\\test.xml");
                ChannelsHandler handler = new ChannelsHandler();
                parser.parse(file,handler);
                List<Channel> tvGuide = handler.getTVGuide();
                for(Channel c:tvGuide){
                    System.out.println(c.getName());
                    System.out.println("------------------------");
                    for(Programme p:c.getAlProgrammes()){
                        System.out.println(p.getTitle());
                        System.out.println(p.getStart_time()+" - "+p.getEnd_time());
                        System.out.println(p.getGenres());
                        System.out.println("Season : "+p.getSeason()+" / Episode : "+p.getEpisode());
                        System.out.println("Description:\n"+p.getDescription());
                        System.out.println("**********************");
                    }
    
                    System.out.println("//////////////////////////");
    
                }
            } catch (ParserConfigurationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SAXException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
        }
    

    Output in my console :

    BBC ONE
    ------------------------
    The Celebrity Apprentice USA
    2013-02-04 00:15:00 - 2013-02-04 01:40:00
    Entertainment
    Season : 8 / Episode : 9
    Description:
    Blah blah blah
    **********************
    The Celebrity Apprentice USA
    2013-02-04 01:45:00 - 2013-02-04 02:25:00
    Entertainment
    Season : 8 / Episode : 10
    Description:
    Blah blah blah
    **********************
    //////////////////////////
    BBC TWO
    ------------------------
    American Dad
    2013-02-04 00:30:00 - 2013-02-04 01:25:00
    Cartoon
    Season : 14 / Episode : 1
    Description:
    Blah blah blah
    **********************
    American Dad
    2013-02-04 01:30:00 - 2013-02-04 02:15:00
    Cartoon
    Season : 14 / Episode : 2
    Description:
    Blah blah blah
    **********************
    //////////////////////////
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I am trying to understand how to use SyndicationItem to display feed which is
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
In my XML file chapters tag has more chapter tag.i need to display chapters
I have an autohotkey script which looks up a word in a bilingual dictionary
I'm trying to select an H1 element which is the second-child in its group
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm parsing an XML file, the creators of it stuck in a bunch social

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.