views:

51

answers:

3

Can we generate an xml file using webservices in Java, and if so how?

+1  A: 

Create a web service (e.g. using Java 6 annotations) and let the annotated method return a DOM tree transformed to a string.

Thorbjørn Ravn Andersen
+1  A: 

Generating XML files has nothing to do with webservices. The common SOAP based webservices communicate with messages written in XML. So to call a webservice, you'll have to create a XML document that implements some xml schema and send the xml document to the servers address. And you won't need files, usually the XML documents are created in memory and not written to files.

Apache Axis2 is a quite powerful library that takes care of most of the marshalling/unmarshalling and communication stuff.

Andreas_D
+2  A: 

There are two Java Web Service Standards:

  • Java API for XML Web Services (JAX-WS)
  • Java API for RESTful Web Services (JAX-RS)

Each spec has multiple implementations. GlassFish is the reference implementation for both these standards.

You can either interact directly with XML, or with POJOs that are converted to XML via an XML binding layer. The standard binding layer for JAX-WS and JAX-RS is Java Architecture for XML binding (JAXB).

For an example of a JAX-RS Webservice check out:

Blaise Doughan
+1 for JAXB, which is how I'd do it.
Donal Fellows