source: ccr/trunk/nhin-vista/projects/NHINC/Current/Product/Production/Examples/JAXBLibExample/src/gov/hhs/fha/nhinc/example/JAXBExamplePropAccess.java@ 507

Last change on this file since 507 was 507, checked in by George Lilly, 15 years ago

NHIN gateway and adaptor for use on linux with VistA EHR and RPMS

File size: 2.8 KB
Line 
1/*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5package gov.hhs.fha.nhinc.example;
6
7import gov.hhs.fha.nhinc.common.propertyaccess.GetPropertyRequestType;
8import gov.hhs.fha.nhinc.common.propertyaccess.ObjectFactory;
9import javax.xml.bind.JAXBContext;
10import javax.xml.bind.JAXBElement;
11import javax.xml.bind.Marshaller;
12import javax.xml.bind.Unmarshaller;
13
14import java.io.StringWriter;
15import java.io.StringReader;
16
17
18/**
19 *
20 * @author westbergl
21 */
22public 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}
Note: See TracBrowser for help on using the repository browser.