首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Webservice-Client:使用纯文本xml而不是对象层次结构。

Webservice-Client:使用纯文本xml而不是对象层次结构。
EN

Stack Overflow用户
提问于 2010-02-02 08:39:33
回答 1查看 8.2K关注 0票数 3

我正在用java编写一个简单的代理程序:

  1. 读取XML文件
  2. 向Web服务发送请求
  3. 阅读Web-服务响应
  4. 写入文件响应

我的第一次尝试是使用JAXB读取xml文件并生成Java对象。然后我用JAX (IBM WebSphere)发送对象。我以"ResponseObject“的形式接收响应,然后将其生成为xml代码。我把XML代码写到文件中.

这个装置很好用。但是..。

当将java对象发送到WebService时,将生成xml,并再次响应创建java对象。我真的不需要那些请求和响应对象。是否有一种方法可以直接使用明文xml调用WebService?并将响应读取为明文xml,而不是那些响应对象?

(让我们假设xml文件总是有效的。)

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-02-05 07:32:30

人们可以使用萨伊 (),它运行在比JAX更低的级别上。我希望它比JAX使用更少的系统资源。

请参阅以下示例(从users.skynet.be/pascalbotte/rcx-ws-doc/saajpost.htm复制)

这次不再使用SOAP消息的动态构造,让我们使用一个简单的文本编辑器并键入我们想要发送的soap消息。 例1-13。文本文件中格式化的SOAP消息: prepared.msg

代码语言:javascript
复制
<SOAP-ENV:Envelope 
 SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" 
 xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 
 xmlns:enc="http://schemas.xmlsoap.org/soap/encoding/" 
 xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <SOAP-ENV:Header/><SOAP-ENV:Body>
  <ans1:readLS xmlns:ans1="http://phonedirlux.homeip.net/types">
   <String_1 xsi:type="xsd:string">your message or e-mail</String_1>
  </ans1:readLS>
 </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

现在,代码将更短,您可以很容易地将其用于测试目的。 例1-14。在文本文件中发布SOAP消息,使用SAAJ将其发送到web服务

代码语言:javascript
复制
  import javax.xml.soap.SOAPConnectionFactory;
  import javax.xml.soap.SOAPConnection;
  import javax.xml.soap.MessageFactory;
  import javax.xml.soap.SOAPMessage;
  import javax.xml.soap.SOAPPart;

  import java.io.FileInputStream;
  import javax.xml.transform.stream.StreamSource;

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

  import javax.xml.transform.stream.StreamResult;

  public class Client {

    public static void main(String[] args) {

      try {
 // Create the connection
 SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance();
 SOAPConnection conn = scf.createConnection();

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

 // Object for message parts
 SOAPPart sp = msg.getSOAPPart();
 StreamSource prepMsg = new StreamSource(
   new FileInputStream("path/prepared.msg"));
 sp.setContent(prepMsg);

 // Save message
 msg.saveChanges();

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

 // Send
 String urlval = "http://www.pascalbotte.be/rcx-ws/rcx";
 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
 Source sc = rp.getSOAPPart().getContent();

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

 // Close connection
 conn.close();

      }
      catch (Exception e) {
 System.out.println(e.getMessage());
      }
    }
  }
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2182747

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档