I have a xsd say request.xsd
and corresponding jaxb generated classes. Now I got a xml file request.xml
which I am able to unmarshal and create "request" object.
I have many element tags in xml some of which are available multiple times. I need to create an java.util.List which should have all leaf node values.
For example :
Below is my request.xml :
<Request>
<Operation>manual</Operation>
<Work>
<WorkModule>
<Name>AXN</Name>
</WorkModule>
</Work>
<Identifier>
<WorkStatus>
<WorkName>CCH</WorkName>
</WorkStatus>
<WorkStatus>
<WorkName>TMH</WorkName>
</WorkStatus>
</Identifier>
</Request>
Below is my JAXB generated Request class. Simillarly there are other classes corresponding to each xml element:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"Operation",
"Work",
"Identifier"
})
@XmlRootElement(name = "Request", namespace = "http://www.sprts.com/clm/nso/mahsgd")
public class Request{
@XmlElement(name = "Operation", required = true)
protected Operation operation;
@XmlElement(name = "Work", required = true)
protected Work work;
@XmlElement(name = "Identifier", required = true)
protected Identifier identifier;
\\ getters and setters
}
So using JAXB I can get unmarshalled Request object having all values in xml file.
Now how do I get all leaf node values (operation, name, workName) in a generic manner without using getters from request object and each of which then I can put in a some collection let say List. I have heard of DOM being used to do similar stuff but I need to use JAXB for the same.
(Without using getters from request object like
String opertaion = request.getOperation();
or String name = request.getWork().getWorkModule().getName();
)
--EDIT--
Can someone help me out in finding an optimal solution to this. Let me know if problem statement is not clear.
--EDIT-- With Doughan & Alexandros's help and some around could able to achieve the same. Not sure if the work around (converting JAXB object to DOM object to InputSource) is the best solution. Below is the working code.
JAXBContext jc = JAXBContext.newInstance(JAXBObject.class);
// Create the Document
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.newDocument();
// Marshal the Object to a Document
Marshaller marshaller = jc.createMarshaller();
marshaller.marshal(jaxbObject, document);
XPathFactory xpf = XPathFactory.newInstance();
XPath xp = xpf.newXPath();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Source xmlSource = new DOMSource(document);
Result outputTarget = new StreamResult(outputStream);
TransformerFactory.newInstance().newTransformer().transform(xmlSource,outputTarget);
InputStream is = new ByteArrayInputStream(outputStream.toByteArray());
InputSource source = new InputSource(is);
NodeList leafNodeObjects = (NodeList) xp.evaluate("//*[not(*)]", source, XPathConstants.NODESET);
for(int x=0; x<leafNodeObjects.getLength(); x++) {
System.out.print("nodeElement = ");
System.out.print(leafNodeObjects.item(x).getNodeName());
System.out.print(" and node value = ");
System.out.println(leafNodeObjects.item(x).getTextContent());
inputDtos.add(new InputDto(leafNodeObjects.item(x).getNodeName(),
leafNodeObjects.item(x).getTextContent()));
}