[英]I'm trying to read a XML with namespace using PHP
I'm trying to read this URL:我正在尝试阅读此网址:
http://www.anterior.banxico.org.mx/rsscb/rss?BMXC_canal=fix&BMXC_idioma=es http://www.anterior.banxico.org.mx/rsscb/rss?BMXC_canal=fix&BMXC_idioma=es
With this XML structure使用这种 XML 结构
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://purl.org/rss/1.0/" xmlns:cb="http://staging.bis.org/rss-cb/1.0/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:xsi="http://www.w3c.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3c.org/1999/02/22-rdf-syntax-ns#rdf.xsd">
<channel rdf:about="http://www.banxico.org.mx/rsscb/rss?canal=tipCam&idioma=es">...</channel>
<item rdf:about="http://www.banxico.org.mx/portal-mercado-cambiario/index.html/20190212">
<title>
<![CDATA[ MX: 19.2592 MXN = 1 USD 2019-02-12 BM FIX ]]>
</title>
<link>
http://www.banxico.org.mx/portal-mercado-cambiario/index.html#FIX
</link>
<description>
<![CDATA[
Este tipo de cambio es determinado por el Banco de México los días hábiles bancarios con base en un promedio de las cotizaciones del mercado de cambios al mayoreo para operaciones liquidables el segundo día hábil bancario siguiente.
]]>
</description>
<dc:date>2019-02-12T12:01:34-06:00</dc:date>
<dc:language>es</dc:language>
<dc:format>text/html</dc:format>
<dc:creator>Banco de México</dc:creator>
<cb:simpletitle>FIX</cb:simpletitle>
<cb:statistics>
<cb:country>MX</cb:country>
<cb:institutionAbbrev>BM</cb:institutionAbbrev>
<cb:exchangeRate>
<cb:value frequency="daily business" decimals="4">19.2592</cb:value>
<cb:baseCurrency>USD</cb:baseCurrency>
<cb:targetCurrency>MXN</cb:targetCurrency>
<cb:rateName>FIX</cb:rateName>
</cb:exchangeRate>
</cb:statistics>
</item>
</rdf:RDF>
I'm using the following code:我正在使用以下代码:
$url = "http://www.anterior.banxico.org.mx/rsscb/rss?BMXC_canal=fix&BMXC_idioma=es";
$xml = simplexml_load_file($url);
$item = $xml->item;
print_r($item);
I'm getting this result:我得到这个结果:
SimpleXMLElement Object
(
[title] => SimpleXMLElement Object
(
)
[link] => http://www.banxico.org.mx/portal-mercado-cambiario/index.html#FIX
[description] => SimpleXMLElement Object
(
)
)
It's not getting the past <dc:date>
:它没有过去<dc:date>
:
I need to get to <cb:value frequency="daily business" decimals="4">19.2592</cb:value>
.我需要到达<cb:value frequency="daily business" decimals="4">19.2592</cb:value>
。
So I can get the 19.2592 value.所以我可以得到19.2592 的值。
What I'm doing wrong?我做错了什么?
You can use XPath to find the element your after without having to search through each node...您可以使用 XPath 来查找您想要的元素,而无需搜索每个节点...
$values = $item->xpath("//cb:value[@frequency=\"daily business\"]");
echo (string)$values[0];
The XPath looks for a <value>
element in the cb
namespace (so <cd:value>
) it also looks for the frequency
attribute with a value of "daily business"
. XPath 在cb
命名空间(所以<cd:value>
)中查找<value>
元素,它还查找值为"daily business"
的frequency
属性。
The xpath()
call will return a list of matching nodes, so use [0]
to take the first element and use (string)
to force it to be a string (or you could use (float)
if you want to use it for calculations).该xpath()
调用将返回匹配节点的列表,以便使用[0]
采取的第一个元素,并使用(string)
,迫使它是一个字符串(或者你可以使用(float)
如果你想用它来计算)。
You can also register a prefix and namespace URI if you can use DOMXPath ...如果您可以使用 DOMXPath,您还可以注册前缀和命名空间 URI ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.