Unable to read from XML file using JAXB



I'm trying to read from the XML file below:



<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<parts>
<item>CPU</item>
<item>GPU</item>
<item>PSU</item>
</parts>


My goal is to add the items to a DefaultDataModel so I can update a JList. However, when I get to my for each loop nothing is added to the list nor is anything printed to the console. Can someone please tell me if I'm doing something wrong?



public void load()
{
// First clear the JLit before loading anything
partList.clear();
chosenList.clear();

File file = new File("partsList.xml");

try
{
// Setup JAXB to create a list of strings from the XML file
JAXBContext context = JAXBContext.newInstance(Parts.class);
Unmarshaller unmarshaller = context.createUnmarshaller();

// Read the XML file
List<String> parts = (List<String>) unmarshaller.unmarshal(file);
System.out.println(parts);

for (String item : parts)
{
chosenList.addElement(item);
System.out.println(item);
}

list_1.setModel(chosenList);
list_1.repaint();
}

catch (JAXBException e)
{
e.printStackTrace();
}
}

No comments:

Post a Comment