I'm facing pretty unusual (I guess ?) scenario where I'm having a hard time trying to parse an XML file w/ XStream, I'd like to ask your help for enlighment.
Here's the deal:
I have the following XML that I'm trying to parse w/ XStream. Everything works fine up to the point where I reach "<Keywords>...</Keywords>" element.
I'd like to get all of the <KEYWORD> elements (keyword_1, keyword_2, keyword_3, ..., keyword_n), without having to make a class to handle the <KEYWORDS>. (This is not an academic exercise, indeed I have a file just like that, but of course, filled with real world data instead of tests)...
================================
Sample.XML
================================
<CATALOG>
<PRODUCTS>
<PRODUCT>
<PRODUCTOID>123</PRODUTOID>
<PRODUCTNAME>TEST NAME</NOMEPRODUTO>
<DESCRIPTION>TEST DESCRIPTION</DESCRIPTION>
<KEYWORDS>
<KEYWORD>keyword_1</KEYWORD>
<KEYWORD>keyword_2</KEYWORD>
<KEYWORD>keyword_3</KEYWORD>
<KEYWORD>keyword_n</KEYWORD>
<KEYWORDS>
</PRODUCT>
</PRODUCTS>
</CATALOG>
================================
Catalog.java
================================
@XStreamAlias("CATALOG")
public class Catalog {
@XStreamAlias("PRODUCTS")
private List<Product> products;
// Getters and Setters
}
================================
Products.java
================================
@XStreamAlias("PRODUCT")
public class Product {
@XStreamAlias("PRODUCTID")
private String id;
@XStreamAlias("PRODUCTNAME")
private String name;
@XStreamAlias("DESCRIPTION")
private String description;
// @XStreamImplicit(itemFieldName = "KEYWORD")
// the above line works, *IF* I delete the parent node `<KEYWORDS>`,
// but that's not the case, I have to deal with it
// @StreamAlias("KEYWORDS/KEYWORD")
// this doesn't work
@StreamAlias("KEYWORDS")
// and this returns the error I'm posting below after this class
private List<String> keywords;
// Getters and Setters
}
================================
XStreamTest.java
================================
public class XStreamTest {
public static void main(String[] args) {
String filepath = "Sample.xml";
try {
FileReader reader = new FileReader(filepath);
XStream xstream = new XStream();
xstream.processAnnotations(Catalog.class);
xstream.processAnnotations(Product.class);
Catalog catalog = (Catalog) xstream.fromXML(reader);
System.out.println(catalog.getProducts().get(0).getKeywords().get(0));
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
}
With the current setup, this is what I'm getting (down below is the stack trace). I'm aware why I'm getting this, it's because I'm not mapping the element <KEYWORD> anywhere... but I don't know how to fix it.
And I really did NOT want to make another class, say "Keyword.java", just to have to deal with that <KEYWORDS> tag (like I did with the Product.java, to manage the elements within the <PRODUCT> element - generating a **List<Product> products** in Catalog.java).
Is there anyway I can solve this (without having to make another class) ?
(Maybe writing a custom unmarshaller, no idea of how, I'm just guessing)
Please enlighten me.
Thank you very much in advance.