1

How i get specific data only Kod attribute is USD and GBP on xml file?Is there a method for fetching attribute value?

TODAY.XML:

<?xml version="1.0" encoding="iso-8859-9"?>
    <Tarih_Date Tarih="27.07.2012" Date="07/27/2012">
        <Currency Kod="USD" CurrencyCode="USD">
            <CurrencyName>US DOLLAR</CurrencyName>
            <ForexBuying>1.81</ForexBuying>
            <ForexSelling>1.8187</ForexSelling>
        </Currency>
        <Currency Kod="EUR" CurrencyCode="EUR">
            <CurrencyName>EURO</CurrencyName>
            <ForexBuying>2.2227</ForexBuying>
            <ForexSelling>2.2334</ForexSelling>
        </Currency>
        <Currency Kod="GBP" CurrencyCode="GBP">
            <CurrencyName>POUND STERLING</CurrencyName>
            <ForexBuying>2.841</ForexBuying>
            <ForexSelling>2.8559</ForexSelling>
        </Currency>
    </Tarih_Date>

PHP:

<?php
    $get_url = "today.xml";
    $xml = simplexml_load_file($get_url) or die("Error : Cannot create object!");

    foreach($xml-> Currency as $currency){
         //$attr = $currency -> attributes();
         echo $attr."<br/>";
         //echo $currency -> CurrencyName;
        //echo $data -> CurrencyName ."-". $data -> ForexBuying ."-". $data -> ForexSelling ."<br/>";
    }
?>
mantissa
  • 147
  • 3
  • 14
  • 1
    You might want to look into DOMDocument and DOMXpath instead of SimpleXML. – GordonM Jul 31 '12 at 07:16
  • 1
    It's possible to do with SimpleXML, but read these first: [http://php.net/manual/en/simplexmlelement.attributes.php](http://php.net/manual/en/simplexmlelement.attributes.php) and [http://stackoverflow.com/questions/6576773/how-to-get-attribute-of-node-with-namespace-using-simplexml](http://stackoverflow.com/questions/6576773/how-to-get-attribute-of-node-with-namespace-using-simplexml) – Stegrex Jul 31 '12 at 07:25

2 Answers2

2

Use an XPath expression like this::

/*/*[@Kod = 'USD' or @Kod = 'GBP']/*

This selects any element that is a grand-child of the top element of the XML document, and its parent's Kod attribute has string value either "USD" or "GBP".

Or use:

/*/*[@Kod = 'USD' or @Kod = 'GBP']/*/text()

This selects the text-node children of the elements selected in the previous XPath expression.

XSLT-based verification:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/">
     <xsl:copy-of select="/*/*[@Kod = 'USD' or @Kod = 'GBP']/*"/>
===========
     <xsl:copy-of select="/*/*[@Kod = 'USD' or @Kod = 'GBP']/*/text()"/>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied on the provided XML document:

<Tarih_Date Tarih="27.07.2012" Date="07/27/2012">
    <Currency Kod="USD" CurrencyCode="USD">
        <CurrencyName>US DOLLAR</CurrencyName>
        <ForexBuying>1.81</ForexBuying>
        <ForexSelling>1.8187</ForexSelling>
    </Currency>
    <Currency Kod="EUR" CurrencyCode="EUR">
        <CurrencyName>EURO</CurrencyName>
        <ForexBuying>2.2227</ForexBuying>
        <ForexSelling>2.2334</ForexSelling>
    </Currency>
    <Currency Kod="GBP" CurrencyCode="GBP">
        <CurrencyName>POUND STERLING</CurrencyName>
        <ForexBuying>2.841</ForexBuying>
        <ForexSelling>2.8559</ForexSelling>
    </Currency>
</Tarih_Date>

the two XPath expressions are evaluated and the results of this evaluation are copied to the output:

<CurrencyName>US DOLLAR</CurrencyName>
<ForexBuying>1.81</ForexBuying>
<ForexSelling>1.8187</ForexSelling>
<CurrencyName>POUND STERLING</CurrencyName>
<ForexBuying>2.841</ForexBuying>
<ForexSelling>2.8559</ForexSelling>
===========
     US DOLLAR1.811.8187POUND STERLING2.8412.8559
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
0

i tried get values with index number.Thanks @GordonM.

   <?php
        $get_url = "http://www.tcmb.gov.tr/kurlar/today.xml";
        $xml = simplexml_load_file($get_url) or die("Error : Cannot create object!");                       
        echo $xml -> Currency[0] -> Isim;
        echo "<br/>";   
        echo $xml -> Currency[2] -> Isim;                   
    ?> 
mantissa
  • 147
  • 3
  • 14