-1

I have attempted to unmarshal a using jaxb, an xml file in a webservice method in netbeans.

the result was that the unmarshalling never gets done. I read in someplace that the soap envelop is formed before jaxb actions. I am a newbie to this.

If anyone could explain simply how to go about getting xml data from an xml file on a jax ws soap message.

this is what my webservice method code is:

 @WebMethod(operationName = "getData")
public String getData() {

try {
    //List<String> listing = new ArrayList();
    StringBuilder str =  new StringBuilder();
    FlightList flights = new FlightList();
    javax.xml.bind.JAXBContext jaxbCtx = javax.xml.bind.JAXBContext.newInstance(flights.getClass().getPackage().getName());
    javax.xml.bind.Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller();
    flights = (FlightList) unmarshaller.unmarshal(new java.io.File("flights.xml")); //NOI18N
    for (Flight f : flights.getAvailableFlight()) {
              str.append(f.getDestinationCity());


    }
    String myString =  str.toString();
    return myString;
} catch (javax.xml.bind.JAXBException ex) {
    // XXXTODO Handle exception
    System.out.println("Error occured" + ex);
}

return null;
}

and my schema file:

<xsd:complexType name="Flight">
<xsd:sequence>
    <xsd:element name="aircraftVendor" type="xsd:string"></xsd:element>
    <xsd:element name="aircraftCode" type="xsd:string"></xsd:element>   
    <xsd:element name="aircraftType" type="xsd:string"></xsd:element>
    <xsd:element name="fare" type="tns:Fare"/>
</xsd:sequence>
<xsd:element name="FlightList">
<xsd:complexType>
    <xsd:sequence >
        <xsd:element name="availableFlight" type="tns:Flight" minOccurs="0"    maxOccurs="unbounded"/>
    </xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:complexType name="Fare">
   <xsd:attribute name="amount" type="tns:Value" use="required"/>
</xsd:complexType>
4
  • What are you trying to do? Send file content in a web service response/as a web service request parameter?
    – dratewka
    May 27, 2013 at 14:05
  • Yes, pass content of the xml file as parameters in the websevice method.
    – Ttola
    May 27, 2013 at 19:44
  • Did you generate all the required classes (e.g from XSD Schema) needed for the unmarshalling?
    – Xargos
    May 28, 2013 at 6:59
  • Yes, i used jax binding to generate all the classes, and if i do test in a java class, i get the right results, yet if i test in a web service it returns null
    – Ttola
    May 28, 2013 at 10:59

1 Answer 1

0

I guess the simplest approach in this case would be to pass the file content as a string parameter to the web service method and then unmarshall the string in that method implementation.

So the web service would be something like this:

@WebService(endpointInterface = "org.foo.SomeEndpoint")
public class SomServiceImpl implements SomeService{

    @Override
    public void uploadFile(String fileName, String fileContent) {
        // unmarshall fileContent into a file called fileName and do whatever you need
    }

}

How to unmarshall an XML file from string..

You should not confuse the XML connected with the webservice messages and the XML from your file. From the point of view of the webservice layer, the file content is just some data to transfer, and it's simpler to treat these things as separate.

1
  • Thanks for your response, what I'm trying to do is to unmarshall an xml file using jaxb in a jax Ws web service method..
    – Ttola
    May 27, 2013 at 21:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.