在Java中解析SOAP响应的最佳方法是什么?

时间:2017-03-26 10:37:18

标签: java xml web-services soap jaxb

我使用saaj调用soap api并将响应存储在一个文件中。以下是回复的一部分:

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
  <AdjPriceResponse xmlns="http://tsetmc.com/">
     <AdjPriceResult>
        <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" id="TseAdjPrice">
           <xs:element name="TseAdjPrice" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
              <xs:complexType>
                 <xs:choice minOccurs="0" maxOccurs="unbounded">
                    <xs:element name="TseAdjPrice">
                       <xs:complexType>
                          <xs:sequence>
                             <xs:element name="InsCode" type="xs:long" minOccurs="0" />
                             <xs:element name="DEven" type="xs:int" minOccurs="0" />
                             <xs:element name="PClosing" type="xs:decimal" minOccurs="0" />
                             <xs:element name="PClosingNoAdj" type="xs:decimal" minOccurs="0" />
                          </xs:sequence>
                       </xs:complexType>
                    </xs:element>
                 </xs:choice>
              </xs:complexType>
           </xs:element>
        </xs:schema>
        <diffgr:diffgram xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
           <TseAdjPrice xmlns="">
              <TseAdjPrice diffgr:id="TseAdjPrice1" msdata:rowOrder="0">
                 <InsCode>180774784936665</InsCode>
                 <DEven>20100707</DEven>
                 <PClosing>1786</PClosing>
                 <PClosingNoAdj>2136</PClosingNoAdj>
              </TseAdjPrice>
              <TseAdjPrice diffgr:id="TseAdjPrice2" msdata:rowOrder="1">
                 <InsCode>180774784936665</InsCode>
                 <DEven>20110724</DEven>
                 <PClosing>1865</PClosing>
                 <PClosingNoAdj>2215</PClosingNoAdj>
              </TseAdjPrice>
           </TseAdjPrice>
        </diffgr:diffgram>
     </AdjPriceResult>
  </AdjPriceResponse>

我使用&#34; JAXB&#34;创建bean类,但发生此错误:

Complex type and its child element share the same name "TseAdjPrice". Use a class customization to resolve this conflict.

我尝试绑定外部文件来解决它,但我不知道将它添加到项目的位置和方式,所以我在&#34; TseAdjPrice&#34中更改xsd和xml文件中的根元素的名称;到&#34; MainTseAdjPrice&#34;并成功地可以使用Jaxb生成MainTseAdjPrice.java类。但我又遇到了另一个错误:

The prefix "diffgr" for attribute "diffgr:id" associated with an element type "TseAdjPrice" is not bound.

我继续调试我的代码,最后我成功通过省略多行输出代码来解析输出。这是修改后的输出:

<?xml version="1.0" encoding="UTF-8"?>
<MainTseAdjPrice>
<TseAdjPrice diffgrid="TseAdjPrice1866" msdatarowOrder="1865">
  <InsCode>71744682148776880</InsCode>
  <DEven>20120521</DEven>
  <PClosing>915</PClosing>
  <PClosingNoAdj>1265</PClosingNoAdj>
</TseAdjPrice>
<TseAdjPrice diffgrid="TseAdjPrice1867" msdatarowOrder="1866">
  <InsCode>71744682148776880</InsCode>
  <DEven>20140312</DEven>
  <PClosing>1630</PClosing>
  <PClosingNoAdj>1780</PClosingNoAdj>
</TseAdjPrice>
</MainTseAdjPrice>

我是否正确省略输出?有没有更好的方法来解析xml而不修改输出?

0 个答案:

没有答案