Reputation: 313
I need to read the k element from xml using JAXB but I cannot find a solution. Has someone an idea? My xml file looks like this :
<supplier>
<material>
<materialId>0-MAT-1-LIC</materialId>
<name>Material 1</name>
<quantity>100</quantity>
</material>
<material>
<materialId>1-MAT-1-LIC</materialId>
<name>Material 2</name>
<quantity>123</quantity>
</material>
<material>
<materialId>3-MAT-1-LIC</materialId>
<name>Material 3</name>
<quantity>1343</quantity>
</material>
<material>
<materialId>4-MAT-1-LIC</materialId>
<name>Material 4</name>
<quantity>1323</quantity>
</material>
<material>
<materialId>5-MAT-1-LIC</materialId>
<name>Material 5</name>
<quantity>1234</quantity>
</material>
<material>
<materialId>6-MAT-1-LIC</materialId>
<name>Material 6</name>
<quantity>12</quantity>
</material>
</supplier>
I was able to map the xml to the java object but I need for example only the 4th element and not the hole list. Thank you in advance!
Upvotes: 0
Views: 37
Reputation: 3943
If you have already mapped the xml to java object, you should have a List
of material. You can invoke a .get(index)
on that list. E.g. if you want to get the 4th element and the object name for the list is materialList, the code would be like this: materialList.get(3);
Index 3 will give you the 4th element in the list.
However, if you only need to work with the 4th element then there is no need to convert the whole xml into a java object. You can use xpath for that. Xpath for the given sample xml to fetch 4th material will be (/supplier/material)[4]
Upvotes: 1