gpt4 book ai didi

php - 访问此 xml 的元素

转载 作者:行者123 更新时间:2023-12-04 06:56:10 24 4
gpt4 key购买 nike

<wsdl:definitions targetNamespace="http://www.webserviceX.NET/">
<wsdl:types>
<s:schema elementFormDefault="qualified" targetNamespace="http://www.webserviceX.NET/">
<s:element name="ConversionRate">
<s:complexType>
<s:sequence>
<s:element minOccurs="1" maxOccurs="1" name="FromCurrency" type="tns:Currency"/>
<s:element minOccurs="1" maxOccurs="1" name="ToCurrency" type="tns:Currency"/>
</s:sequence>
</s:complexType>
</s:element>
<s:simpleType name="Currency">
<s:restriction base="s:string">
<s:enumeration value="AFA"/>
<s:enumeration value="ALL"/>
<s:enumeration value="DZD"/>
<s:enumeration value="ARS"/>

我试图获取枚举中的所有元素,但似乎无法正确处理。这是作业,所以请不要提供完整的解决方案,如果可能,请提供指导。
$feed = simplexml_load_file('http://www.webservicex.net/CurrencyConvertor.asmx?WSDL');

foreach($feed->simpleType as $val){
$ns s = $val->children('http://www.webserviceX.NET/');
echo $ns_s -> enumeration;
}

我究竟做错了什么?

谢谢

最佳答案

以下适用于 PHP 5.2:

$feedUrl = 'http://www.webservicex.net/CurrencyConvertor.asmx?WSDL';
$feed = simplexml_load_file($feedUrl);
$feed->registerXPathNamespace('s', 'http://www.w3.org/2001/XMLSchema');
foreach( $feed->xpath('//s:enumeration/@value') as $enumNode) {
echo $enumNode, PHP_EOL;
}

print_r( $feed->getDocNamespaces() );

另一种方法是使用 DOM 扩展:
$feed = new DomDocument;
$feed->load('http://www.webservicex.net/CurrencyConvertor.asmx?WSDL');
$nodes = $feed->getElementsByTagNameNS(
'http://www.w3.org/2001/XMLSchema',
'enumeration');

foreach( $nodes as $node ) {
echo $node->getAttribute('value'), PHP_EOL;
}

关于php - 访问此 xml 的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2533394/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com