无法从 PHP 解析 XML
我找不到同样的问题,因此我决定打开新的。 我一直坚持下面的一个问题。 目标是使用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> 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);}
错误消息为 Call to undefined method stdClass::xpath()
c) 还尝试注册名称空间
$xml = simplexml_load_string($results);
$xml->registerXPathNamespace('envoy', 'https://ecom.elko.lv/xml');
...
但它说我 simplexml_load_string() 期望参数 1 是字符串
等...
您能帮忙弄清楚吗?
I was not able to find same problem, for that reason I decided to open new.
I've stuck with an issue below.
The target is to show xml attributes values on UI using soap message.
PHP code looks like:
$client = new SoapClient("https://domain.com/xml/listener.asmx?WSDL");
$results = $client->ProductDescription(array('Username' => "anyuser", 'Password' => "anypassword", 'code' => "1108324"));
print_r($results);
As a result i get following message
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> 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 ) )
The xml I get from soap ui using same request has different structure
<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>
I was trying to read Criteria and Value argument values using different approaches, for example
a)
foreach($Envelope->Body->ProductDescriptionResponse->ProductDescriptionResult->NewDataSet->Product as $p)
{
echo $p->Criteria."<br>";
echo $p->Value."<br>";
}
Error message was "Undefined variable: Envelope..."
b)
foreach ($results->xpath('//soap:Envelope[1]/soap:Body[1]/ProductDescriptionResponse[1]/ProductDescriptionResult[1]/NewDataSet[1]/Product/*') as $item)
{ print_r($item);}
Error message was Call to undefined method stdClass::xpath()
c)
Also tried to register namespace
$xml = simplexml_load_string($results);
$xml->registerXPathNamespace('envoy', 'https://ecom.elko.lv/xml');
...
But it says me that simplexml_load_string() expects parameter 1 to be string
etc...
Could you please help to figure out?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从响应元素 (
ProductDescriptionResult
) 中获取它,这是SOAPClient
将为您提供的内容:请记住,这里不再有 XML,
SOAPClient
已经将它转换为一棵漂亮的对象树了。可能在任何时候上述线索都不存在(我只是根据 XML 猜测),在这种情况下,只需从get_object_vars
执行var_dump
即可查看当前节点中有什么。哦,不要在浏览器中查看 HTML 输出,请查看源代码,调试输出中的许多<
和>
可以“隐藏”如果你看到的内容是 HTML 格式的。编辑:这在这里有效
Take it from the response element (
ProductDescriptionResult
), which is what theSOAPClient
would give you:Keep in mind you don't have XML here anymore, the
SOAPClient
converted it to a nice tree of objects for you already. It could be that at any point the above trail doesn't exist (I just guessed it based on the XML), in that case, just do avar_dump
fromget_object_vars
to see what's in the current node. Oh, and don't look at the HTML output in a browser, view the source, a lot of<
and>
s in your debug output can 'hide' content if you look at is as HTML.edit: this works here