I'm developing an Android app. My apps has a button and when I press this button I will parse an XML file, put the information of this file in some object and present this object in a expandable list. Also my XML file has this structure:
<?xml version="1.0" encoding="UTF-8"?>
<Programs>
<Program programNumber="1" imgURL="http://www.photovideolife.com/userfiles/Placeholder%2001.jpg" description="Lorem ipsum dolor sit er elit">
<Episode pN="1" episodeNumber="1" transmissionName="Titolo" date="29 Giu 2013" time1="14:30" time2="" channel="Real Time" channelLogo="https://lh6.googleusercontent.com/-XSh-9ngYJu4/ThiY-xl2EeI/AAAAAAAABmc/iz04VpL5hOs/s800/realtime.png">
</Episode>
<Episode pN="1" episodeNumber="1" transmissionName="Titolo" date="29 Giu 2013" time1="" time2="16:30" channel="DMAX" channelLogo="http://tv.zam.it/canali/dmax.png">
</Episode>
<Episode pN="1" episodeNumber="2" transmissionName="Titolo" date="01 Lug 2013" time1="14:30" time2="" channel="Real Time" channelLogo="https://lh6.googleusercontent.com/-XSh-9ngYJu4/ThiY-xl2EeI/AAAAAAAABmc/iz04VpL5hOs/s800/realtime.png">
</Episode>
<Episode pN="1" episodeNumber="2" transmissionName="Titolo" date="01 Lug 2013" time1="" time2="16:30" channel="DMAX" channelLogo="http://tv.zam.it/canali/dmax.png">
</Episode>
</Program>
</Programs>
I made 3 object: Episode, Program and Programs. I post here the implementation:
Episode.java
public class Episode {
String pN, episodeNumber, transmissionName, date, time1, time2, channel, channelLogo;
public String getpN() {
return pN;
}
public void setpN(String pN) {
this.pN = pN;
}
public String getEpisodeNumber() {
return episodeNumber;
}
public void setEpisodeNumber(String episodeNumber) {
this.episodeNumber = episodeNumber;
}
public String getTransmissionName() {
return transmissionName;
}
public void setTransmissionName(String transmissionName) {
this.transmissionName = transmissionName;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getTime1() {
return time1;
}
public void setTime1(String time1) {
this.time1 = time1;
}
public String getTime2() {
return time2;
}
public void setTime2(String time2) {
this.time2 = time2;
}
public String getChannel() {
return channel;
}
public void setChannel(String channel) {
this.channel = channel;
}
public String getChannelLogo() {
return channelLogo;
}
public void setChannelLogo(String channelLogo) {
this.channelLogo = channelLogo;
}
}
Program.java
public class Program {
public Episode[] episodes;
String programNumber, imgUrl, description;
public Episode[] getEpisodes() {
return episodes;
}
public void setEpisodes(Episode[] episodes) {
this.episodes = episodes;
}
public String getProgramNumber() {
return programNumber;
}
public void setProgramNumber(String programNumber) {
this.programNumber = programNumber;
}
public String getImgUrl() {
return imgUrl;
}
public void setImgUrl(String imgUrl) {
this.imgUrl = imgUrl;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
Programs.java
public class Programs {
public Program[] programs;
public Program[] getPrograms() {
return programs;
}
public void setPrograms(Program[] programs) {
this.programs = programs;
}
}
To parse the XML file I made this class:
XmlParser.java
import it.lucgian84.models.Episode;
import it.lucgian84.models.Program;
import it.lucgian84.models.Programs;
import java.io.ByteArrayInputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import android.renderscript.Element;
import android.util.Log;
public class XmlParser {
private String xml;
private Programs programs;
private Program program = new Program();
private Episode episode = new Episode();
public XmlParser(String xml) {
this.xml = xml;
}
public void parseXml() {
try {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory
.newDocumentBuilder();
Document document = documentBuilder.parse(new InputSource(
new ByteArrayInputStream(xml.getBytes("utf-8"))));
document.getDocumentElement().normalize();
NodeList nodeList = document.getElementsByTagName("Program");
for (int i = 0; i < nodeList.getLength(); i++) {
Node nodeItem = nodeList.item(i);
if (nodeItem.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) nodeItem;
program.setProgramNumber(((org.w3c.dom.Element) element)
.getAttribute("programNumber"));
program.setImgUrl(((org.w3c.dom.Element) element)
.getAttribute("imgUrl"));
program.setDescription(((org.w3c.dom.Element) element)
.getAttribute("description"));
}
}
nodeList = document.getElementsByTagName("Episode");
for (int i = 0; i < nodeList.getLength(); i++) {
Node nodeItem = nodeList.item(i);
if (nodeItem.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) nodeItem;
episode.setpN(((org.w3c.dom.Element) element)
.getAttribute("pN"));
episode.setEpisodeNumber(((org.w3c.dom.Element) element)
.getAttribute("episodeNumber"));
episode.setTransmissionName(((org.w3c.dom.Element) element)
.getAttribute("transmissionName"));
episode.setDate(((org.w3c.dom.Element) element)
.getAttribute("date"));
episode.setTime1(((org.w3c.dom.Element) element)
.getAttribute("time1"));
episode.setTime2(((org.w3c.dom.Element) element)
.getAttribute("time2"));
episode.setChannel(((org.w3c.dom.Element) element)
.getAttribute("channel"));
episode.setChannelLogo(((org.w3c.dom.Element) element)
.getAttribute("channelLogo"));
}
}
} catch (Exception e) {
Log.d("XML", "Exception: " + e);
}
}
}
I'm not sure how to insert the object Episode to the array Program and the object Program to the array Programs. I hope you can help me to find a solution of this issue. Thank you