Unmarshalling XML using JAXB
java, jaxb, xml
Solution
Below is one way that you could map your use case with a JAXB (JSR-222) implementation:
Items
We will use the following class for the root object and annotate it with `@XmlRootElement`. The `@XmlRootElement` annotation tells JAXB that this class should be instantiated if the root element in the document being unmarshalled is `items`, you can also specify a different name `@XmlRootElement(name="foo")`.
package forum11152046;
import java.util.List;
import javax.xml.bind.annotation.*;
@XmlRootElement
public class Items {
private List<Item> items;
@XmlElement(name="item")
public List<Item> getItems() {
return items;
}
public void setItems(List<Item> items) {
this.items = items;
}
}
Item
In this example I created a class where all the property names correspond directly to the names in the XML document. This means there aren't any annotations that need to be added. If you need to override the default name you can use an annotation such as `@XmlElement` to do so. I used the `@XmlElement` annotation to do this in the `Items` class for the `items` property.
package forum11152046;
public class Item {
private int code;
private String name;
private int price;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
Demo
package forum11152046;
import java.io.File;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Items.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum11152046/input.xml");
Items items = (Items) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(items, System.out);
}
}
input.xml/Output
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<items>
<item>
<code>12000</code>
<name>Samsung 620</name>
<price>9999</price>
</item>
<item>
<code>15000</code>
<name>NOKIA</name>
<price>19999</price>
</item>
<item>
<code>18000</code>
<name>HTC 620</name>
<price>29999</price>
</item>
</items>
Problem
I went through almost all questions related to this topic here. But was not able to get a proper solution. My issue is as follows: I created a simple program to unmarshall an xml file for which i had a xsd. I was able to do that successfully. But if i am getting an xml without xsd, how can I get my attributes from that, if the xml looks something like this : ``` <items> <item> <code>12000</code> <name>Samsung 620</name> <price>9999</price> </item> <item> <code>15000</code> <name>NOKIA</name> <price>19999</price> </item> <item> <code>18000</code> <name>HTC 620</name> <price>29999</price> </item> </items> ``` Here I don't have an xsd to generate my classes. How can i proceed? Kindly help me. Thank You