1

I'm trying to read this URL:

http://www.anterior.banxico.org.mx/rsscb/rss?BMXC_canal=fix&BMXC_idioma=es

With this XML structure

<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>:

I need to get to <cb:value frequency="daily business" decimals="4">19.2592</cb:value>.

So I can get the 19.2592 value.

What I'm doing wrong?

halfer
  • 19,824
  • 17
  • 99
  • 186
  • You need to inform the XML parser of the namespaces you're working with. I think I know of a duplicate question, hang on... – halfer Feb 13 '19 at 19:19
  • Possible duplicate of [How can I get data from this Amazon XML response file using PHP?](https://stackoverflow.com/questions/51989129/how-can-i-get-data-from-this-amazon-xml-response-file-using-php) – halfer Feb 13 '19 at 19:21

2 Answers2

1

You can use XPath to find the element your after without having to search through each node...

$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".

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).

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
0

You can also register a prefix and namespace URI if you can use DOMXPath ...

DOMXPath::registerNamespace

davro
  • 1
  • 1