PDA

Ver la Versión Completa : SOAP Web Service


foetus
23-01-2009, 21:35:25
Saludos,

Estoy tratando de consumir un web service que esta hecho en SOAP.

Necesito usar solo J2SE, estube probando con el paquete javax.xml.soap de la versión 1.6. Estube buscando ejemplos y eso, hasta ahora eh logrado que se conecte al web service, ejecute un metodo y me devuelva los datos de retorno en xml como era de esperarce.

El problema es que necesito manipular estos datos lo mas que se pueda, necesito saber si ahi alguna clase que me permita manipular el xml que me retorna el web service.

Aqui posteo el codigo de ejemplo que estoy usando:

import javax.xml.soap.*;
import java.util.*;

import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.Source;

import javax.xml.transform.stream.StreamResult;

import javax.imageio.metadata.*;



public class Connection
{
// instance variables - replace the example below with your own




private int x;

/**
* Constructor for objects of class Connection
*/
public Connection()
{

}


public static void main(String[] args)
{
String url = "http://spirittechnologies.net/carlos/web_service/3rd/cbf_camps.php";

try{
// Create Conextion
SOAPConnectionFactory factory = SOAPConnectionFactory.newInstance();
SOAPConnection conn = factory.createConnection();

// Create message
MessageFactory mf = MessageFactory.newInstance();
SOAPMessage msg = mf.createMessage();



// Object for message parts
SOAPPart sp = msg.getSOAPPart();

SOAPEnvelope env = sp.getEnvelope();
env.addNamespaceDeclaration("xsd","http://www.w3.org/2001/XMLSchema");
env.addNamespaceDeclaration("xsi","http://www.w3.org/2001/XMLSchema-instance");
env.addNamespaceDeclaration("enc","http://schemas.xmlsoap.org/soap/encoding/");
env.addNamespaceDeclaration("env","http://schemas.xmlsoap.org/soap/envelop/");
env.setEncodingStyle("http://schemas.xmlsoap.org/soap/encoding/");

SOAPBody bd = env.getBody();

// Populate body
// Main element and namespace
SOAPElement be = bd.addChildElement(env.createName("campList",
"urn",
"http://spirittechnologies.net/carlos/web_service/3rd/cbf_camps.php"));
// namespace to use for my rpc/encoded wsdl version is:
// http://phonedirlux.homeip.net/wsdl
// note, in this case the endpoint address is /rcx-ws-rpc/rcx

// Add content
be.addChildElement("partnerId").addTextNode("85").setAttribute("xsi:type","xsd:string");
be.addChildElement("partnerKey").addTextNode("tjbestty").setAttribute("xsi:type","xsd:string");
be.addChildElement("action").addTextNode("showCamps").setAttribute("xsi:type","xsd:string");


// Save message
msg.saveChanges();

// View input
System.out.println("\n Soap request:\n");
msg.writeTo(System.out);
System.out.println();

// Send
String urlval = url;//"http://www.pascalbotte.be/rcx-ws/rcx";
// or /rcx-ws-rpc/rcx for my rpc/encoded web service

SOAPMessage rp = conn.call(msg, urlval);

// View the output
System.out.println("\nXML response\n");

// Create transformer
TransformerFactory tff = TransformerFactory.newInstance();
Transformer tf = tff.newTransformer();

// Get reply content
SOAPPart soapPart = rp.getSOAPPart();

Source sc = soapPart.getContent();


// Set output transformation
StreamResult result = new StreamResult(System.out);
tf.transform(sc, result);
System.out.println();

// Close connection
conn.close();





}catch(Exception e){
e.printStackTrace();
System.out.println(e.getMessage());
}



}
}



:confused:

meriano
23-01-2009, 22:41:09
Puedes probar con las librerías JDom (http://www.jdom.org/). A mi me han resultado bastante cómodas y livianas, además de open source.

Suerte!

__hector
02-02-2009, 16:03:48
Ufff, no deberias hacerlo asi. Necesitas jaxb (java for xml binding) para pasar el resultado de un mensaje soap a la representacion de un objeto en java (pojo), y no trabajar directamente con el stream xml que te retorna esa llamada.

Lo mejor que puedes hacer es utilizar un framework para webservices, y uno de los mejorcitos que hay es Axis de apache:

http://ws.apache.org/axis2/

Suerte!

Arcioneo
19-02-2009, 16:50:44
Ufff, no deberias hacerlo asi. Necesitas jaxb (java for xml binding) para pasar el resultado de un mensaje soap a la representacion de un objeto en java (pojo), y no trabajar directamente con el stream xml que te retorna esa llamada.

Lo mejor que puedes hacer es utilizar un framework para webservices, y uno de los mejorcitos que hay es Axis de apache:

http://ws.apache.org/axis2/

Suerte!


Con Axis solucionas todo sin problemas