Skip to content

Instantly share code, notes, and snippets.

@atkawa7
Forked from Lormenyo/ReadXml.java
Created December 10, 2020 16:58
Show Gist options
  • Save atkawa7/a06957d6daf69f622c45f4a18c79362d to your computer and use it in GitHub Desktop.
Save atkawa7/a06957d6daf69f622c45f4a18c79362d to your computer and use it in GitHub Desktop.
This is the main file for marshalling and unmarshalling the XML file.
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();
}
}
}
<?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>
package com.jaxb;
//import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="USSDMenuRequest")
public class USSDMenuRequest{
private String sessionId;
private String messageid;
private String shortCode;
private String prompt;
private String timeStamp;
@XmlElement(name="sessionId")
public void setSessionId(String sessionId){
this.sessionId = sessionId;
}
public String getSessionId(){
return sessionId;
}
public String getMessageid(){
return messageid;
}
@XmlElement(name="messageid")
public void setMessageid(String messageid){
this.messageid = messageid;
}
@XmlElement(name="shortCode")
public void setShortCode(String shortCode){
this.shortCode = shortCode;
}
public String getShortCode(){
return shortCode;
}
@XmlElement(name="prompt")
public void setPrompt(String prompt){
this.prompt = prompt;
}
public String getKeyWord(){
return keyWord;
}
public String getTimeStamp(){
return timeStamp;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment