将Xml文件发送到Soap请求中获取java中的问题

时间:2014-02-27 11:28:49

标签: java xml string soap

我正在使用SoapRequst For我的桌面应用程序。但有一个Soap请求,其中我发送xml文档,但当我尝试添加该xml文件意味着文档然后转换为字符串格式我意味着所有“<>”将子句转换为字符串格式“<”像这样。

那么我应该做些什么来使它们保持XMl格式。

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <Update xmlns="http://tempuri.org/">
      <doc>xml</doc>
    </Update>
  </soap:Body>
</soap:Envelope>

当我发送文件时,转换为此文件。

**&gt;&lt;Folder&gt;&lt;**

必须是这个

<Folder>

我可以事先得到你的宝贵建议。

这里我的方法是。 public static SOAPMessage UpdateAllGroupWithSwitchUserConfiguration(){

        try {
            File f = new File("/com/package/package/TreeModel.xml");
            BufferedReader br = new BufferedReader(new FileReader(f));
            StringBuffer builder = new StringBuffer();
            String xml = "";
            while ((xml = br.readLine()) != null) {
                builder.append(xml.trim());
            }

            MessageFactory messageFactory = MessageFactory.newInstance();
            SOAPMessage soapMessage = messageFactory.createMessage();
            SOAPPart soapPart = soapMessage.getSOAPPart();

            String serverURI = "http://tempuri.org/";

            // SOAP Envelope
            SOAPEnvelope envelope = soapPart.getEnvelope();
            envelope.addNamespaceDeclaration("tem", serverURI);

            SOAPElement sOAPElement = envelope.addChildElement("UpdateAllGroupWithSwitchUserConfiguration", "tem");

            String XML_String = builder.toString();

//            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//            DocumentBuilder doc_builder = factory.newDocumentBuilder();
//            
//            Document document = doc_builder.parse(new InputSource(new StringReader(XML_String)));
//            
//            TransformerFactory factory1 = TransformerFactory.newInstance();
//            Transformer transformer = factory1.newTransformer();
//            Source source = new DOMSource(document);
//            Result result = new StreamResult(xml);
//            transformer.transform(source, result);

            SOAPElement xml_APElement = sOAPElement.addChildElement("doc", "tem");
            xml_APElement.addTextNode(builder.toString());




            MimeHeaders headers = soapMessage.getMimeHeaders();
            headers.addHeader("SOAPAction", serverURI + "UpdateAllGroupWithSwitchUserConfiguration");

            soapMessage.saveChanges();

            /*
             * Print the request message
             */
            System.out.print("Request SOAP Message = ");

            soapMessage.writeTo(System.out);

            return soapMessage;

        } catch (Exception e) {
        }
        return null;
    }

我已经评论了我的尝试。

1 个答案:

答案 0 :(得分:0)

嗯,这取决于您如何定义主XML文档的结构。你可以发布XSD吗? 例如。如果在主XSD中,您将doc元素定义为来自具有给定命名空间的另一个特定XSD,例如。 “myExternalDocumentNamespace”,然后你可以按照你尝试的方式包含它,只需添加元素的命名空间,例如。

<Folder xmlns="myExternalDocumentNamespace">
...
</Folder>

根据您的情况,您可能更喜欢在主XSD中使用xs:any元素,但最后也可以归结为相同。