如何解析已定义多个名称空间的soap xml响应。我需要解析返回的Articles数组

时间:2019-05-01 06:30:52

标签: php xml parsing curl soap

soap xml响应如下:

 <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
       <SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:NS1="urn:TPAPIPosIntfU-ITPAPIPOS">
          <NS1:GetArticlesInfoResponse xmlns:NS2="urn:TPAPIPosIntfU" xmlns:NS3="urn:TPAPIPosTypesU">
             <return xsi:type="NS2:TGetArticlesInfoResponse">
                <ReturnCode xsi:type="xsd:int">0</ReturnCode>
                <ReturnMessage xsi:type="xsd:string">ok</ReturnMessage>
                <Articles xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="NS3:TArticleInfo[x]">...</Articles>
                <Extra xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="NS3:TExtraInfo[0]"/>
             </return>
          </NS1:GetArticlesInfoResponse>
       </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>

这是我需要解析的Articles数组:

<item xsi:type="NS3:TArticleInfo">
    <ArticleId xsi:type="xsd:long">5000001716</ArticleId>
    <ArticleName xsi:type="xsd:string">Coca Cola</ArticleName>
    <ArticleNumber xsi:type="xsd:int">1</ArticleNumber>
    <Available xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="xsd:long[3]">
        <item>5000000210</item>
        <item>5000000208</item>
        <item>5000000209</item>
    </Available>
    <DepartmentId xsi:type="xsd:long">5000000170</DepartmentId>
    <Prices xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="NS3:TItemPrice[2]">
        <item xsi:type="NS3:TItemPrice">
        <ArticleId xsi:type="xsd:long">5000001716</ArticleId>
        <PriceId xsi:type="xsd:long">5000000206</PriceId>
        <Amount xsi:type="xsd:double">2</Amount>
        <Vat xsi:type="xsd:double">21</Vat>
        <Extra xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="NS3:TExtraInfo[0]"/>
    </item>
    <item xsi:type="NS3:TItemPrice">
        <ArticleId xsi:type="xsd:long">5000001716</ArticleId>
        <PriceId xsi:type="xsd:long">5000000207</PriceId>
        <Amount xsi:type="xsd:double">1.7</Amount>
        <Vat xsi:type="xsd:double">12</Vat>
        <Extra xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="NS3:TExtraInfo[0]"/>
    </item>
    </Prices>
    <FreeOption xsi:type="xsd:long">5000000145</FreeOption>
    <Options xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="xsd:long[1]">
        <item>5000000143</item>
    </Options>
    <IsMenu xsi:type="xsd:boolean">false</IsMenu>
    <IsManualPrice xsi:type="xsd:boolean">false</IsManualPrice>
    <IsActive xsi:type="xsd:boolean">true</IsActive>
    <Promo xsi:type="xsd:boolean">false</Promo>
    <HqId xsi:type="xsd:string">Coca Cola</HqId>
    <Extra xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="NS3:TExtraInfo[5]">
        <item xsi:type="NS3:TExtraInfo">
            <Key xsi:type="xsd:string">daily_stock_active</Key>
            <Value xsi:type="xsd:string">0</Value>
            <Extra xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="NS3:TExtraInfo[0]"/>
        </item>
        <item xsi:type="NS3:TExtraInfo">
            <Key xsi:type="xsd:string">daily_stock_qty</Key>
            <Value xsi:type="xsd:string">0</Value>
            <Extra xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="NS3:TExtraInfo[0]"/>
        </item>
        <item xsi:type="NS3:TExtraInfo">
            <Key xsi:type="xsd:string">purchase_price</Key>
            <Value xsi:type="xsd:string">0.0000</Value>
            <Extra xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="NS3:TExtraInfo[0]"/>
            </item>
        <item xsi:type="NS3:TExtraInfo">
            <Key xsi:type="xsd:string">course_id</Key>
            <Value xsi:type="xsd:string">5000001331</Value>
            <Extra xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="NS3:TExtraInfo[0]"/>
        </item>
        <item xsi:type="NS3:TExtraInfo">
            <Key xsi:type="xsd:string">info</Key>
            <Value xsi:type="xsd:string"/>
            <Extra xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="NS3:TExtraInfo[0]"/>
        </item>
    </Extra>
</item>
Parameter   Type    Description
ArticleId   Long    Internal Id of this Article
ArticleName String  Name of this Article
ArticleNumber   Integer Number of this Article
Available   [Long]  List of Sales Area Id's where this Article is available
Prices  [ItemPrice] List of Prices for this Article
DepartmentId    Long    Id of the Department where this Article belongs to
FreeOption  Long    Free Option ID
Options [Long]  List of must-have options
IsMenu  Boolean Returns true if this Article is a menu
IsManualPrice   Boolean Returns true if this Article requires a manual price input when ordered
IsActive    Boolean Returns if true is this Article is active
Promo   Boolean Return true if this Article is a Cobmo/Promo article
HqId    String  HQ Id
Extra   [ExtraInfo] List of extra fields
List of available extra fields:

Key Description
course_id   Only return articles of a specific course
daily_stock_active  1/0 (if DailyStock=1 was specified in request’s Extra fields)
daily_stock_qty Daily stock quantity (if DailyStock=1 was specified in request’s Extra fields)
info    Article info (tab 9 of the article settings in back-office)
plu When PLU is not zero
purchase_price  Purchase price
ArticleShort
ArticleShort Object

<item xsi:type="NS3:TArticleShort">
    <ArticleId xsi:type="xsd:long">5000001716</ArticleId>
    <ArticleName xsi:type="xsd:string">Coca Cola</ArticleName>
    <ArticleNumber xsi:type="xsd:int">1</ArticleNumber>
    <SalesAreaId xsi:type="xsd:long">5000000210</SalesAreaId>
    <DepartmentId xsi:type="xsd:long">5000000170</DepartmentId>
    <HqId xsi:type="xsd:string">Coca Cola</HqId>
    <Extra xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="NS3:TExtraInfo[0]"/>
</item>

如何在php curl中解析此soap XML响应以获取Articles数组数据?我已经尝试过SimpleXMLElement,但是无法解析。

我已经浏览了很多答案,但是无法获得正确的解析技术。如果可以,我应该使用Xpaths,然后如何创建一个XPath来解析articles数组?

1 个答案:

答案 0 :(得分:1)

使用XPath和SimpleXML的简单解决方案,从XML的角度来看,它不涉及XPath本身的名称空间。这将加载XML,然后查找具有直接后代<return>的元素<Articles>。当xpath()返回节点列表时,它仅选择第一个节点(使用[0])并迭代包含的<item>元素,并输出<ArticleId>元素值。 ..

$xml = simplexml_load_string($data);
$articles = $xml->xpath("//return/Articles");

foreach ( $articles[0]->item as $item )    {
    echo (string)$item->ArticleId.PHP_EOL;
}

通过测试数据可以得出...

5000001716
5000001716