Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Web Service from WSDL

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Web Service from WSDL

    I'm a total newbie to Java and was wondering if you can save the incoming parameter object of a web service to am XML file.

    I'm using netbeans and have created the following:

    1) an xml schema to describe the parameters being passed by the soap message.
    2) WSDL to describe my web service using the complex type defined in my schema.
    3) Created my web sevice from the WSDL.

    Here is a snippet of my web service....

    public class OrderService implements WebOrderPortType {

    public String getWebOrder(webservices.weborderschema.OrderType inOrder)


    All I want the web service to do is save the incoming object (inOrder) as an xml file in a directory.

    Do I need to parse the object to a DOM and then save ?? Or is there an easier way to do it?

    Any help would be appreciated.


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default 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.

    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

  3. #3
    Junior Member
    Join Date
    Jan 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Web Service from WSDL

    Thank you - this worked great.

  4. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Web Service from WSDL

    My pleasure!

    // Json

Similar Threads

  1. Java EE warnings while parsing wsdl
    By Idy in forum Web Frameworks
    Replies: 0
    Last Post: January 14th, 2010, 03:29 PM
  2. simple login web service
    By mr_aliagha in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: January 5th, 2010, 02:49 PM
  3. Unable to open web service tester page
    By oneofthelions in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: December 13th, 2009, 05:20 PM
  4. Replies: 2
    Last Post: November 19th, 2009, 10:55 PM
  5. Free java hosting service providers
    By servlet in forum Java Servlet
    Replies: 1
    Last Post: May 14th, 2009, 07:28 AM