Is it possible to unmarshal a XML file like this:
<company id="bh23" name="imob">
<store>
<store-info id="2392">
<address>NYC</address>
<name>Imob's NYC 5th</name>
</store>
<products>
<product>
<name>keyboard</keyboard>
<price>2000</price>
</product>
<product>
<name>mouse</keyboard>
<price>1000</price>
</product>
</products>
</store>
<store />
</stores>
into classes like these:
@XmlElementRoot(name = "company")
public class Company {
@XmlAttribute (name = "id")
String id;
@XmlAttribute (name = "name")
String name;
@XmlElement (name = "store")
List<Store>stores;
//all the getters and setters
}
@XmlElementRoot (name = "store")
public class Store {
@XmlAttribute (name = "id")
String id;
@XmlElement (name = "address")
String address;
@XmlElement (name = "name")
String name;
@XmlElementWrapper (name = "products")
@XmlElement (name = "product")
List<Product>products;
//all the getters and setters
}
public class main {
public static void main (String args[]) {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(Company.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Company portfolio = (Company) jaxbUnmarshaller.unmarshal(new File(xmlUrlPath));
System.out.println(portfolio.toString());
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
I'm trying to "skip" or "jump" the node named "store-info" because I don't want to create another class just to keep the store's address and name, since it would be more simple to "append" both address and name to "Store" class. Of course, when I run the code, the vars "address", "id" and "name" becomes null and only the list of products is correctly unmarshaled. Is there a way to skip a node, merging their fields into another class? I'm avoiding (for "legal" purposes) the use of MOXy lib and their XPath annotation.