-
-
Save atkawa7/a06957d6daf69f622c45f4a18c79362d to your computer and use it in GitHub Desktop.
This is the main file for marshalling and unmarshalling the XML file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.jaxb; | |
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import javax.xml.bind.JAXBContext; | |
import javax.xml.bind.Marshaller; | |
import javax.xml.bind.Unmarshaller; | |
public class ReadXml { | |
public static void main(String[] args) throws IOException { | |
File xmlFile = new File("ussd.xml"); | |
unmarshalXML(xmlFile); | |
marshalXML(); | |
} | |
//-------------------------- UNMARSHALLING XML ------------------------------- | |
public static void unmarshalXML(File xmlFile) { | |
try { | |
System.out.println("Unmarshalling XML to Java Object-------"); | |
//creating the JAXB context | |
JAXBContext jContext = JAXBContext.newInstance(USSDMenuRequest.class); | |
//create the unmarshal object | |
Unmarshaller unmarshallerObj = jContext.createUnmarshaller(); | |
//call the unmarshal method | |
USSDMenuRequest request= (USSDMenuRequest) unmarshallerObj.unmarshal(xmlFile); | |
request.setStarCode("124"); | |
request.setKeyWord("Start"); | |
System.out.println(request.getShortCode() + " " + request.getPrompt() ); | |
} | |
catch(Exception e){ | |
e.printStackTrace(); | |
} | |
} | |
-------------------------- MARSHALLING XML ---------------------------------- | |
public static void marshalXML() { | |
try { | |
System.out.println("Marshalling Java Object to XML-------"); | |
//creating the JAXB context | |
JAXBContext jContext = JAXBContext.newInstance(USSDMenuRequest.class); | |
//create the marshaller object | |
Marshaller marshallerObj = jContext.createMarshaller(); | |
//the XML should be properly indented and formatted | |
marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); | |
USSDDynMenuRequest Sendrequest= new USSDMenuRequest(); | |
Sendrequest.setShortCode("124"); | |
Sendrequest.setPrompt("Start"); | |
Sendrequest.setMessageid("0244708099"); | |
Sendrequest.setSessionId("789888886adh"); | |
//calling the marshall method | |
marshallerObj.marshal(Sendrequest, new FileOutputStream("response.xml")); | |
} | |
catch(Exception e){ | |
e.printStackTrace(); | |
} | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<USSDDynMenuRequest> | |
<sessionId name='sessionId'>$sessionId</sessionId> | |
<messaged name='messageid'>$messageid</messageid> | |
<shortCode name='shortCode'>$shortCode</shortCode> | |
<prompt name='prompt'>$prompt</prompt>" | |
<menu> | |
<value>param1</value> | |
<value>param2</value> | |
<value>param3</value> | |
</menu> | |
<userData name="name">$name</userData> | |
<timeStamp>$timestamp</timeStamp> | |
</USSDMenuRequest> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment