Re: Web Service from WSDL
I think your question makes this sound harder than it actually is. If I understand correctly you wish to store or serialize a Java object to an XML document?
You can of course go through the object members and store this XML your self somehow or you could look into something like JAXB which can transform Java objects to XML and XML to Java objects.
For more information on JAXB and how to get the appropriate libraries head over to https://jaxb.dev.java.net/
Here is a code snippet to transform an object into XML and back.
Code :
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlRootElement;
public class JaxbTest1 {
public static void main(String[] args) throws Throwable {
// =============================================================================================================
// Setup JAXB
// =============================================================================================================
// Create a JAXB context passing in the class of the object we want to marshal/unmarshal
final JAXBContext context = JAXBContext.newInstance(JavaObject.class);
// =============================================================================================================
// Marshalling OBJECT to XML
// =============================================================================================================
// Create the marshaller, this is the nifty little thing that will actually transform the object into XML
final Marshaller marshaller = context.createMarshaller();
// Create a stringWriter to hold the XML
final StringWriter stringWriter = new StringWriter();
// Create the sample object we wish to transform into XML
final JavaObject javaObject = new JavaObject();
javaObject.setName("Json");
javaObject.setRole("Moderator");
javaObject.setAge(28);
// Marshal the javaObject and write the XML to the stringWriter
marshaller.marshal(javaObject, stringWriter);
// Print out the contents of the stringWriter
System.out.println(stringWriter.toString());
// =============================================================================================================
// Unmarshalling XML to OBJECT
// =============================================================================================================
// Create the unmarshaller, this is the nifty little thing that will actually transform the XML back into an object
final Unmarshaller unmarshaller = context.createUnmarshaller();
// Unmarshal the XML in the stringWriter back into an object
final JavaObject javaObject2 = (JavaObject) unmarshaller.unmarshal(new StringReader(stringWriter.toString()));
// Print out the contents of the JavaObject we just unmarshalled from the XML
System.out.println(javaObject2.toString());
}
/**
* JavaObject is the sample object we've created to use for marshalling to and from XML.
* Make sure you have the @XmlRootElement annotation at the top there as well or JAXB
* might moan.
*/
@XmlRootElement
private static class JavaObject {
private String name;
private String role;
private int age;
public JavaObject() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Name [" + this.name + "], Role [" + this.role + "], Age [" + this.age + "]";
}
}
}
Enjoy!
// Json
Re: Web Service from WSDL
Thank you - this worked great.
Re: Web Service from WSDL