vendredi 11 janvier 2019

Xml to Java Class using JAXBContext

I have created several REST Web Services that will return list of records, from my database.

I'm returning all query results in XML format and in my Web service methods I convert it to Java to send it to my client app in JSON (I know, not the best case).

I'm trying to reduce the code in my methods an part of the code that I started with was the XML to Java conversion.

Right now I'm using the following

String xml = xml.replaceFirst("ROWSET xmlns:xsi = " + "\"http://www.w3.org/2001/XMLSchema-instance\"", "invTrxXmlList");  
String string = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";  
xml = string.concat(xml);  
xml = xml.replaceAll("ROWSET", "invTrxXmlList");  
xml = xml.replaceAll("ROW", "invTrxXmlList");  
xml = xml.replaceAll(" xsi:nil = \"true\"", "");  
InputStream instr = new ByteArrayInputStream(xml.getBytes());  
JAXBContext jaxbContext = JAXBContext.newInstance(InvTrxXmlList.class);  
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();  
InvTrxXmlList invTrxXml = (InvTrxXmlList) jaxbUnmarshaller.unmarshal(instr);  

inventoryTrx = new InventoryTrx(invTrxXml);  

What I want is to create a separate method that could be called by all methods to this job, if possible to work with several distinct Custom Objects, like InvTrxXmlList.

I have, in another task worked a little with Reflection and i was thinking of using it to solve this issue.

Can you help me?

Is there another approach?

Thanks





Aucun commentaire:

Enregistrer un commentaire