[英]PHP XML - Extract repeating element and child elements to array
我使用內置的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轉換為數組,但是我仍然只需要提取我需要的內容。
任何幫助,將不勝感激。
我看到了SoapClient
的優點,但想要回答xpath-part:
$results = $xml->xpath("//Bid");
將選擇所有<Bid>
節點及其子節點。 要訪問這些:
foreach ($results as $result) {
echo $result->rows . "<br />";
}
但是,XML中的此節點似乎已被破壞:
<rows>1</rodun>
您應該使用SoapClient
類進行SOAP通信。 盡管可以僅使用低級 HTTP和XML處理來實現soap客戶端,但在大多數情況下我不建議您這樣做,因為如果數據結構變得復雜,這可能會很奇怪。
使用我鏈接的頁面及其上的注釋作為一個很好的起點。 這是另一個教程鏈接: http : //devzone.zend.com/25/php-soap-extension/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.