在Java中解析SOAP响应的最佳方法是什么
我使用saaj调用soap api并将响应存储在文件中。以下是部分回应:在Java中解析SOAP响应的最佳方法是什么,java,xml,web-services,soap,jaxb,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-instan
<?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>
我试图绑定外部文件来解析它,但我不知道在哪里以及如何将它添加到项目中,所以我将xsd和xml文件中根元素的名称从“TseAdjPrice”更改为“MainTseAdjPrice”,并成功地使用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>
71744682148776880
20120521
915
1265
71744682148776880
20140312
1630
1780
我省略输出对吗?有没有更好的方法在不修改输出的情况下解析xml?使用JDK中内置的DOM解析器。这就是解析XML的方法。您还有很多工作要做:通过遍历DOM树来提取值。@duffymo我想JAXB是一个将xml映射到对象的解析器。我的问题是如何更有效地使用它。使用JDK内置的DOM解析器。这就是解析XML的方法。您还有很多工作要做:通过遍历DOM树来提取值。@duffymo我想JAXB是一个将xml映射到对象的解析器。我的问题是如何更有效地使用它。
<?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>