Iteruj przez elementy w JAXB
Stworzyłem metodę unmarshall mojego pliku xml (item.xml). Ale jeśli jest więcej niż jeden element, jak mogę iterować przez wszystkie elementy i uzyskać je wyświetlane?
Mój kod wygląda następująco:
final JAXBContext jc = JAXBContext.newInstance("com.generated");
final Unmarshaller u = jc.createUnmarshaller();
final File f = new File("D:\\item.xml");
final JAXBElement element = (JAXBElement) u.unmarshal(f);
final Item item = (Item) element.getValue();
// This will be helpful only if the xml contains one element
System.out.println(item.getCode());
System.out.println(item.getName());
System.out.println(item.getPrice());
Jeśli mój xml jest
<item>
<item1>
<code>12000</code>
<name>Samsung Galaxy Tab 620</name>
<price>9999</price>
</item1>
<item2>
<code>15000</code>
<name>NOKIA</name>
<price>19999</price>
</item2>
<item3>
<code>18000</code>
<name>HTC 620</name>
<price>29999</price>
</item3>
</item>
Jak mogę wyświetlić wszystkie wartości? Czy ktoś może mi pomóc?