In previous post, I demonstrated how to marshall POJOs to SDO. In this post, I will show how to convert SDOs to DOM Elements without parsing them to or from String.
The key is the following methods from the commonj.sdo.helper.XSDHelper:
- void save(XMLDocument xmlDocument, javax.xml.transform.Result outputResult, java.lang.Object options); -- wherein, we can instantiate a DOM node and wrap it with a javax.xml.transform.dom.DOMResult;
- XMLDocument load(javax.xml.transform.Source inputSource, java.lang.String locationURI, java.lang.Object options); -- wherein we can wrap a given DOM element with javax.xml.transform.dom.DOMSource;
Below is a client I add on the sample application from previous post:
package soadev.sdojaxbsample.clients; import commonj.sdo.DataObject; import commonj.sdo.Type; import commonj.sdo.helper.XMLDocument; import javax.xml.bind.JAXBContext; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.dom.DOMResult; import javax.xml.transform.dom.DOMSource; import oracle.bpel.services.common.util.XMLUtil; import org.eclipse.persistence.sdo.helper.jaxb.JAXBHelperContext; import org.w3c.dom.Document; import org.w3c.dom.Element; import soadev.hrservice.model.Job; public class SDOToElementClient { public static void main(String[] args) { SDOToElementClient client = new SDOToElementClient(); try { client.testConvertSdoToDomElementAndViceVersa(); } catch (Exception e) { e.printStackTrace(); } } public void testConvertSdoToDomElementAndViceVersa()throws Exception{ System.out.println("----Start testConvertSdoToDomElementAndViceVersa----"); JAXBContext jaxbContext = JAXBContext.newInstance(Job.class); JAXBHelperContext jaxbHelperContext = new JAXBHelperContext(jaxbContext); ClassLoader loader = Thread.currentThread().getContextClassLoader(); jaxbHelperContext.getXSDHelper().define(loader.getResourceAsStream("soadev/hrservice/model/JobSDO.xsd"), "soadev/hrservice/model"); DataObject sdo = createSampleSDO(jaxbHelperContext); System.out.println("Created sample SDO: " + sdo); XMLDocument xmlDocument = jaxbHelperContext.getXMLHelper().createDocument(sdo, sdo.getType().getURI(), "jobSDO"); DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document document = docBuilder.newDocument(); jaxbHelperContext.getXMLHelper().save(xmlDocument, new DOMResult(document), null); Element element = document.getDocumentElement(); System.out.println("SDO converted to DOM element: " + element); System.out.println(XMLUtil.toString(element)); System.out.println(">>> Converting element to SDO..."); XMLDocument xmlDocFromElement = jaxbHelperContext.getXMLHelper().load(new DOMSource(element), null, null); DataObject sdoFromElement = xmlDocFromElement.getRootObject(); System.out.println("DOM element converted to SDO: " + sdoFromElement); System.out.println(sdoFromElement.get("jobId")); System.out.println(sdoFromElement.get("jobTitle")); System.out.println(sdoFromElement.get("maxSalary")); System.out.println(sdoFromElement.get("minSalary")); System.out.println("----End testConvertSdoToDomElementAndViceVersa----"); } private DataObject createSampleSDO(JAXBHelperContext jaxbHelperContext) { Type jobType = jaxbHelperContext.getType(Job.class); DataObject jobSDO = jaxbHelperContext.getDataFactory().create(jobType); jobSDO.set("jobId", "SOADEV"); jobSDO.set("jobTitle", "SOA Developer"); jobSDO.set("maxSalary", 20000L); jobSDO.set("minSalary", 18000L); return jobSDO; } }
Run Output...
C:\Oracle\11g\jdk160_24\bin\javaw.exe -client -classpath ... ----Start testConvertSdoToDomElementAndViceVersa---- Created sample SDO: soadev.hrservice.model.JobSDOImpl@1372656 SDO converted to DOM element: oracle.xml.parser.v2.XMLElement@1fa6d18 <ns0:jobSDO xmlns:ns0="/soadev.hrservice.model/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <jobId>SOADEV</jobId> <jobTitle>SOA Developer</jobTitle> <maxSalary>20000</maxSalary> <minSalary>18000</minSalary> </ns0:jobSDO> >>> Converting element to SDO... DOM element converted to SDO: soadev.hrservice.model.JobSDOImpl@1ee148b SOADEV SOA Developer 20000 18000 ----End testConvertSdoToDomElementAndViceVersa---- Process exited with exit code 0.
No comments:
Post a Comment