[507] | 1 | /*
|
---|
| 2 | * To change this template, choose Tools | Templates
|
---|
| 3 | * and open the template in the editor.
|
---|
| 4 | */
|
---|
| 5 | package gov.hhs.fha.nhinc.example;
|
---|
| 6 |
|
---|
| 7 | import gov.hhs.fha.nhinc.common.propertyaccess.GetPropertyRequestType;
|
---|
| 8 | import gov.hhs.fha.nhinc.common.propertyaccess.ObjectFactory;
|
---|
| 9 | import javax.xml.bind.JAXBContext;
|
---|
| 10 | import javax.xml.bind.JAXBElement;
|
---|
| 11 | import javax.xml.bind.Marshaller;
|
---|
| 12 | import javax.xml.bind.Unmarshaller;
|
---|
| 13 |
|
---|
| 14 | import java.io.StringWriter;
|
---|
| 15 | import java.io.StringReader;
|
---|
| 16 |
|
---|
| 17 |
|
---|
| 18 | /**
|
---|
| 19 | *
|
---|
| 20 | * @author westbergl
|
---|
| 21 | */
|
---|
| 22 | public class JAXBExamplePropAccess
|
---|
| 23 | {
|
---|
| 24 | public static void main (String[] args)
|
---|
| 25 | {
|
---|
| 26 | try
|
---|
| 27 | {
|
---|
| 28 |
|
---|
| 29 | GetPropertyRequestType oRequest = new GetPropertyRequestType();
|
---|
| 30 |
|
---|
| 31 | oRequest.setPropertyName("Test");
|
---|
| 32 | oRequest.setPropertyFile("TestFile");
|
---|
| 33 |
|
---|
| 34 | // if the element is not a root element, then you have to wrap it with a JAXB element.
|
---|
| 35 | // This is how you do it...
|
---|
| 36 | //-------------------------------------------------------------------------------------
|
---|
| 37 | ObjectFactory oFactory = new ObjectFactory();
|
---|
| 38 | JAXBElement oElement = oFactory.createGetPropertyRequest(oRequest);
|
---|
| 39 |
|
---|
| 40 | // Marshall the JAXB object into XML
|
---|
| 41 | //-----------------------------------
|
---|
| 42 | JAXBContext oContext = JAXBContext.newInstance("gov.hhs.fha.nhinc.common.propertyaccess");
|
---|
| 43 | Marshaller oMarshaller = oContext.createMarshaller();
|
---|
| 44 | StringWriter swXML = new StringWriter();
|
---|
| 45 | oMarshaller.marshal(oElement, swXML);
|
---|
| 46 | String sXML = swXML.toString();
|
---|
| 47 |
|
---|
| 48 | System.out.println("Starting program...");
|
---|
| 49 | System.out.println("Marshalling...");
|
---|
| 50 | System.out.println("XML: ");
|
---|
| 51 | System.out.println(sXML);
|
---|
| 52 |
|
---|
| 53 | // Now take it from XML back into a set of JAXB objects
|
---|
| 54 | //------------------------------------------------------
|
---|
| 55 | Unmarshaller oUnmarshaller = oContext.createUnmarshaller();
|
---|
| 56 | StringReader srXML = new StringReader(sXML);
|
---|
| 57 | JAXBElement oElementOutput = (JAXBElement) oUnmarshaller.unmarshal(srXML);
|
---|
| 58 | GetPropertyRequestType oRequestOutput = (GetPropertyRequestType) oElementOutput.getValue();
|
---|
| 59 |
|
---|
| 60 | System.out.println("");
|
---|
| 61 | System.out.println("Unmarshalling...");
|
---|
| 62 | System.out.println("FileName: " + oRequestOutput.getPropertyFile());
|
---|
| 63 | System.out.println("Property Name:" + oRequestOutput.getPropertyName());
|
---|
| 64 | System.out.println("Program done...");
|
---|
| 65 | System.out.println("");
|
---|
| 66 | }
|
---|
| 67 | catch (Exception e)
|
---|
| 68 | {
|
---|
| 69 | System.out.println("An unexpected error occurred: " + e.getMessage());
|
---|
| 70 | e.printStackTrace();
|
---|
| 71 | System.exit(-1);
|
---|
| 72 | }
|
---|
| 73 | }
|
---|
| 74 | }
|
---|