-1

CDATA を含む SOAP 応答がありますが、Java で応答を処理します。すべての開始引用符 (<) は (<) に置き換えられます。

応答は次のようになります

 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
  <ns:Response xmlns:ns="http://pipeline_ws">
     <ns:return>
     <![CDATA[<fr><Result>
        <ListPartner>
            <operator>
                <country_code></country_code>
                <currency_code></currency_code>
                <operator_code></operator_code>

        </operator>
     </ListPartner>
 </Result></fr>]]></ns:return>
  </ns:Response>
</soapenv:Body>
</soapenv:Envelope>

しかし代わりに、これは私が得ている応答です

 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
    <ns:Response xmlns:ns="http://pipeline_ws">
        <ns:return>
&lt;fr>
&lt;Result>
&lt;ListPartner>
&lt;operator>
&lt;country_code>&lt;/country_code>
&lt;currency_code>&lt;/currency_code>
&lt;operator_code>&lt;/operator_code>   
&lt;/operator>
&lt;/ListPartner>
&lt;/Result>
&lt;/fr>
    </ns:return>
</ns:Response>
</soapenv:Body>
</soapenv:Envelope>

応答を解析できるように、不要な文字 (<) を削除/置換するソリューションを提供してください。SAAJ を使用して応答を処理しています

        // Process the SOAP Response
SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);
        soapResponse.writeTo(System.out);
4

1 に答える 1

0

Parse the XML and retrieve the text node value of <ns:return/> as a string. Then this string can be treated itself as XML.

final XPath xpath = XPathFactory.newInstance().newXPath();
xpath.setNamespaceContext(new MyNamespaceContext());
final String xmlFile = "1.xml";
final InputSource source = new InputSource(new FileInputStream(xmlFile));

final DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
fac.setNamespaceAware(true);
final Document doc = fac.newDocumentBuilder().parse(source);

final String result = (String) xpath.evaluate(
    "/soapenv:Envelope/soapenv:Body/pipeline:Response/pipeline:return/text()", 
    doc, XPathConstants.STRING);

System.out.println(result);

Beware that you should set namespace awareness in the DocumentBuilderFactory instance.

And here a simple NamespaceContext implementation defining the needed namespace prefixes:

static class MyNamespaceContext implements NamespaceContext {

  private Map<String, String> ns;

  public MyNamespaceContext() {
    ns = new HashMap<String, String>();
    ns.put("xs", "http://www.w3.org/2001/XMLSchema");
    ns.put("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
    ns.put("pipeline", "http://pipeline_ws");
  }

  public String getNamespaceURI(String prefix) {
    return ns.get(prefix);
  }

  public Iterator<String> getPrefixes() {
    return ns.keySet().iterator();
  }

  public Iterator<String> getPrefixes(String namespaceURI) {
    final List<String> prefixes = new LinkedList<String>();
    if(namespaceURI != null) {
      for(Entry<String, String> entry: ns.entrySet()) {
        if(namespaceURI.equals(entry.getValue())) {
          prefixes.add(entry.getKey());
        }
      }
    }
    return prefixes.iterator();
  }

  public Map<String, String> getPrefixMapping() {
    return ns;
  }

  @Override
  public String getPrefix(String namespaceURI) {
    if(namespaceURI != null) {
      for(Entry<String, String> entry: ns.entrySet()) {
        if(namespaceURI.equals(entry.getValue())) {
          return entry.getKey();
        }
      }
    }
    return null;
  }    
}

The output for the first XML version is:

<fr><Result>
   <ListPartner>
        <operator>
            <country_code></country_code>
            <currency_code></currency_code>
            <operator_code></operator_code>

    </operator>
 </ListPartner>
</Result></fr>

And for the second:

<fr>
<Result>
<ListPartner>
<operator>
<country_code></country_code>
<currency_code></currency_code>
<operator_code></operator_code>   
</operator>
</ListPartner>
</Result>
</fr>
于 2015-11-02T14:44:57.110 に答える