Edit: here's how I'm loading the XML document, as I used it in Blaise's answer. I'm loading it like this because I want to work with a node, not the whole doc. Even using the whole document I'm still having trouble when loading in this manner.
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(false);
factory.setNamespaceAware(false);
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse("[path to doc]/input.xml");
TestClass testClass = (TestClass) unmarshaller.unmarshal(doc);
I've got XML that looks like this:
<test>
<items>
<item type="cookie">cookie</item>
<item type="crackers">crackers</item>
</items>
</test>
And a class:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "test")
public class TestClass
{
@XmlPath("items/item/text()")
@XmlElement
private ArrayList<String> itemList = new ArrayList<String>();
// getters, setters omitted
}
The above code will work whether or not I have @XmlElement
, and I get an ArrayList containing [cookie, crackers].
If I change the declaration above to
@XmlPath("items/item/@type")
@XmlElement
private ArrayList<String> itemList = new ArrayList<String>();
my ArrayList is empty.
My ultimate goal is to just have attributes so my XML would look like this:
<test>
<items>
<item type="cookie"/>
<item type="crackers"/>
</items>
</test>
Is what I'm trying to do, pull out a list of attributes using XPath, possible, and if so, how?
Thank you.