I'm new to XML and im trying to read in an XML page and store its contents in an arraylist.
So far i seem to have been able to get the arraylist filled with the first content, as when i tried an isEmpty, it returned false. so there is definitely containing something. However, when i try to call the overided tostring method, or even try to call any individual category for that matter, it just returns empty?
can anyone help?
heres the code im working with:
package test;
import java.io.File;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
public class xmlmx {
public static void main(String[] args) {
ArrayList<Anime> list = new ArrayList<Anime>();
try {
File inputFile = new File("c:\\code\\ad\\XMLDB.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(inputFile);
doc.getDocumentElement().normalize();
System.out.println("Root element: " + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("Anime");
System.out.println("----------------------------");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
System.out.println("\nCurrent Element :" + nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
list.add(new Anime(eElement.getAttribute("ID"),
eElement.getAttribute("name"),
eElement.getAttribute("altname"),
eElement.getAttribute("seasons"),
eElement.getAttribute("episodes"),
eElement.getAttribute("status"),
eElement.getAttribute("DS"),
eElement.getAttribute("have"),
eElement.getAttribute("left"),
eElement.getAttribute("plot"),
eElement.getAttribute("connect"),
eElement.getAttribute("image")));
System.out.println(list.get(0).toString());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
the arraylist is of type anime, a class here:
package test;
class Anime{
private String ID;
private String name;
private String altname;
private String seasons;
private String episodes;
private String status;
private String DS;
private String have;
private String left;
private String plot;
private String connect;
private String image;
public Anime(String ID,
String name,
String altname,
String seasons,
String episodes,
String status,
String DS,
String have,
String left,
String plot,
String connect,
String image) {
this.ID = ID;
this.name = name;
this.altname = altname;
this.seasons = seasons;
this.episodes = episodes;
this.status = status;
this.DS = DS;
this.have = have;
this.left = left;
this.plot = plot;
this.connect = connect;
this.image = image;
}
/*
getters and setters here...
*/
@Override
public String toString() {
return "Anime [ID=" + ID +
", name=" + name +
", altname=" + altname +
", seasons=" + seasons +
", episodes=" + episodes +
", status=" + status +
", DS=" + DS +
", have=" + have +
", left=" + left +
", plot=" + plot +
", connect=" + connect +
", image=" + image + "]";
}
}
finally, heres the xml:
<?xml version="1.0" encoding="UTF-8"?>
<Anime>
<record ID="BL1">
<name>Bleach</name>
<altname>Burichi</altname>
<seasons>16</seasons>
<episodes>366</episodes>
<status>Finished</status>
<sound>Dubbed</sound>
<have>All</have>
<left>-/-</left>
<plot>Ichigo gets grim reaper powers, fights reapers, hollows and everything in between</plot>
<connect>Bleach movies</connect>
<image>images/bleach.jpg</image>
</record>
</Anime>
any help would be greatly appreciated, thank you