OK, here is where I'm at so far.
XML File:
<?xml version="1.0" encoding="UTF-8"?>
<cycling>
<bicycle>
<make>Dawes</make>
<model>Giro 300</model>
<price>20</price>
</bicycle>
<bicycle>
<make>Kona</make>
<model>Kona Road</model>
<price>25</price>
</bicycle>
<bicycle>
<make>Felt</make>
<model>F95</model>
<price>25</price>
</bicycle>
<bicycle>
<make>Lapierre</make>
<model>R LITE 300 09</model>
<price>30</price>
</bicycle>
</cycling>
DataReader Class:
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import java.io.*;
/*
* The DataReader class's job is to " read " data from the XML file.
*/
/**
*
* @author Andy Green
*/
public class DataReader
{
public static void main(String[] args)
{
File file = new File("/home/andy/NetBeansProjects/cycling.xml");
try {
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(file);
NodeList nodes = doc.getElementsByTagName("bicycle");
for (int i = 0; i < nodes.getLength(); i++) {
Element element = (Element) nodes.item(i);
NodeList name = element.getElementsByTagName("make");
Element line = (Element) name.item(0);
System.out.println("Bicycle Make: " + line.getFirstChild().getTextContent());
NodeList age = element.getElementsByTagName("model");
line = (Element) age.item(0);
System.out.println("Model: " + line.getFirstChild().getTextContent());
NodeList hobby = element.getElementsByTagName("price");
for(int j=0;j<hobby.getLength();j++)
{
line = (Element) hobby.item(j);
System.out.println("Price: £" + line.getFirstChild().getTextContent());
}
System.out.println();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Result of running the DataReader Class:
Bicycle Make: Dawes
Model: Giro 300
Price: £20
Bicycle Make: Kona
Model: Kona Road
Price: £25
Bicycle Make: Felt
Model: F95
Price: £25
Bicycle Make: Lapierre
Model: R LITE 300 09
Price: £30
However, looking at my DataReader class, I feel there are bits of code that aren't really needed for my XML file, but are there from the previous file you used in your Tutorial example. I say this because all I have done is Copy and Paste from your class into my calss in NetBeans.
I'd appreciate it if you could tell me which bits I can remove, I think I have a rough idea myself, but I don't want to destroy the whole thing !
Thanks