无法从PHP解析XML

时间:2012-01-14 19:00:13

标签: php xml parsing soap

我无法找到同样的问题,因此我决定开新。 我坚持下面的问题。 目标是使用soap消息在UI上显示xml属性值。 PHP代码如下所示:

$client = new SoapClient("https://domain.com/xml/listener.asmx?WSDL");
$results = $client->ProductDescription(array('Username' => "anyuser", 'Password' => "anypassword", 'code' => "1108324"));
print_r($results);

结果我得到以下消息

stdClass Object ( [ProductDescriptionResult] => stdClass Object ( [any] => Imagehttp://catalog2.elkogroup.com/pictures/prDesc/large/1108324_425_0_425NULL.jpgDescription<h1 class="tagline"><span style="font-size: small">Quality surround audio for music, movies and games</span></h1> <p>&nbsp;Sound Blaster 5.1 VX is the absolute choice for those looking for better quality audio solutions basic motherboard audio can not deliver.</p>Vendor Homepagehttp://en.europe.creative.com/products/productarchive.asp?category=1&subcategory=873&product=17510&nav=Description2CREATIVE 5.1 VX (SB1071) OEMAudio-InInput/Output connectors1Audio-OutInput/Output connectors1MicrophoneInput/Output connectors1Included AccessoriesQuick start Guide; User Guide (on CD); Installation CDUnit Brutto Volumecubm0.001555Unit Net Weightkg0.13Unit Gross Weightkg0.157 ) )

我使用相同的请求从soap ui获得的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>
      <ProductDescriptionResponse xmlns="https://ecom.elko.lv/xml">
         <ProductDescriptionResult>
            <NewDataSet xmlns="">
               <Product>
                  <Criteria>Image</Criteria>
                  <Value>http://test/</Value>
               </Product>
               ...
               ...
            </NewDataSet>
        </ProductDescriptionResult>
      </ProductDescriptionResponse>
    </soap:Body>
</soap:Envelope>

我试图使用不同的方法读取Criteria和Value参数值,例如

a)

foreach($Envelope->Body->ProductDescriptionResponse->ProductDescriptionResult->NewDataSet->Product as $p)
        {
            echo $p->Criteria."<br>";
            echo $p->Value."<br>";
        }

错误消息是“未定义的变量:信封...”

b)中

foreach ($results->xpath('//soap:Envelope[1]/soap:Body[1]/ProductDescriptionResponse[1]/ProductDescriptionResult[1]/NewDataSet[1]/Product/*') as $item) 
        { print_r($item);} 

错误消息是调用未定义的方法stdClass :: xpath()

c)中 还试图注册名称空间

$xml = simplexml_load_string($results);
$xml->registerXPathNamespace('envoy', 'https://ecom.elko.lv/xml');

...

但它告诉我simplexml_load_string()要求参数1为字符串

等...

你能帮忙解决一下吗?

1 个答案:

答案 0 :(得分:2)

从响应元素(ProductDescriptionResult)获取,这是SOAPClient给你的内容:

echo $results->ProductDescriptionResult->NewDataSet->Product[1]->Criteria;

请记住,此处不再使用XML,SOAPClient已将它转换为一个漂亮的对象树。可能在任何时候上面的踪迹都不存在(我只是根据XML猜测它),在这种情况下,只需从var_dump做一个get_object_vars来查看当前节点中的内容。哦,不要在浏览器中查看HTML输出,查看源代码,调试输出中的很多<>可以“隐藏”内容,如果你看的是HTML


编辑:此处可以使用

<?php
class S extends SoapClient {
        function __doRequest(){
                return  <<<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>
      <ProductDescriptionResponse xmlns="https://ecom.elko.lv/xml">
         <ProductDescriptionResult>
            <NewDataSet xmlns="">
               <Product>
                  <Criteria>Image1</Criteria>
                  <Value>http://test/1</Value>
               </Product>
               <Product>
                  <Criteria>Image2</Criteria>
                  <Value>http://test/2</Value>
               </Product>
            </NewDataSet>
        </ProductDescriptionResult>
      </ProductDescriptionResponse>
    </soap:Body>
</soap:Envelope>
XML;
        }
}
$d = new S(null,array('uri' => 'localhost','location' => 'localhost'));
$result = $d->somerequest();
echo $result->NewDataSet->Product[0]->Criteria;