我使用内置PHP SOAP
函数CURL
发送请求并从.Net ASMX web service
.
目前我正在接收作为 XML 的响应,但需要一些帮助来从 XML 中提取信息,因为我需要的数据嵌套在 XML 的深处。例如,仅提取我需要的内容并将其转换为数组,以便将其直接发送到数据库。
XML 开头如下:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetViewBidResponse xmlns="http://domain.com/SolutionWebService">
<GetViewBidResult>
<message>OK</message>
<table>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:msprop="urn:schemas-microsoft-com:xml-msprop">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="Bidrod" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Bid">
<xs:complexType>
<xs:sequence>
<xs:element name="rows" msprop:ColumnId="1" type="xs:string" minOccurs="0"/>
<xs:element name="Queue" msprop:ColumnId="2" type="xs:string" minOccurs="0"/>
<xs:element name="Running" msprop:ColumnId="3" type="xs:string" minOccurs="0"/>
<xs:element name="New" msprop:ColumnId="4" type="xs:string" minOccurs="0"/>
<xs:element name="Working" msprop:ColumnId="5" type="xs:string" minOccurs="0"/>
<xs:element name="Done" msprop:ColumnId="6" type="xs:string" minOccurs="0"/>
<xs:element name="Agree" msprop:ColumnId="7" type="xs:string" minOccurs="0"/>
<xs:element name="Canceled" msprop:ColumnId="8" type="xs:string" minOccurs="0"/>
<xs:element name="Hold" msprop:ColumnId="9" type="xs:string" minOccurs="0"/>
<xs:element name="Test" msprop:ColumnId="10" type="xs:string" minOccurs="0"/>
<xs:element name="All" msprop:ColumnId="11" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<DocumentElement xmlns="">
<Bid diffgr:id="Bid1" msdata:rowOrder="0" diffgr:hasChanges="inserted">
<rows>1</rodun>
<Queue>Testqueue1</Queue>
<Running>1</Running>
<New>2</New>
<Working>7</Working>
<Done>4802</Lokið>
<Agree>1</Agree>
<Canceled>87</Canceled>
<Hold>3</Hold>
<Test>0</Test>
<All>4902</All>
</Bid>
<Bid diffgr:id="Bid2" msdata:rowOrder="1" diffgr:hasChanges="inserted">
<rows>1</rodun>
<Queue>Testqueue2</Queue>
<Running>23</Running>
<New>q2</New>
<Working>27</Working>
<Done>48</Lokið>
<Agree>11</Agree>
<Canceled>827</Canceled>
<Hold>31</Hold>
<Test>10</Test>
<All>402</All>
</Bid>
<Bid diffgr:id="Bid3" msdata:rowOrder="2" diffgr:hasChanges="inserted">
...shortened, this continues from "diffgr:id="Bid1" to "diffgr:id="Bid97"...
我想做的是提取每个元素下的所有子元素<Bid diffgr:id="Bid1"
(从DocumentElement
元素之后开始)。最好以仅包含以下信息的数组结尾:
[DocumentElement] => Array ( // Optional
[Bid] => Array ( // Optional
[0] => Array (
[rows] => 1
[Queue] => Testqueue1
[Running] => 1
[New] => 2
[Working] => 7
[Done] => 4802
[Agree] => 1
[Canceled] => 87
[Hold] => 3
[Test] => 0
[All] => 4902 )
[1] => Array (
[rows] => 1
[Queue] => Testqueue2
[Running] => 1
[New] => 4
[Working] => 3
... and continues with all "Bid diffgr:id="BidXX" (number can reach 99) ...
如果我能得到帮助,只提取我需要的数组,那么我可以管理其余部分(将它们添加到数据库中)。
我一直在查看 xpath,但没有成功将它绑定到正确的 xpath 元素。这是我尝试过但没有运气的方法:
$xml = simplexml_load_string($response);
$id = $xml->xpath("Bid[@id=Bid*]"); // Not sure how to use the wildcard
var_dump($id);
我还能够使用XML2ARRAY将整个 XML 转换为数组,但是我仍然需要只提取上面提到的内容。
任何帮助,将不胜感激。