我如何获得xml文件的具体数据?

时间:2012-07-31 07:13:51

标签: php xml xpath xml-parsing

我如何获得特定数据Kod属性是xml文件中的USD和GBP?是否有获取属性值的方法?

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/>";
    }
?>

2 个答案:

答案 0 :(得分:2)

使用这样的XPath表达式:

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

这将选择任何元素作为XML文档顶部元素的子元素,其父元素的Kod属性的字符串值为"USD""GBP"

或使用

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

这将选择在前一个XPath表达式中选择的元素的文本节点子节点。

基于XSLT的验证

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

在提供的XML文档上应用此转换时

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

评估两个XPath表达式,并将此评估的结果复制到输出中:

<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

答案 1 :(得分:0)

我尝试使用索引号获取值。谢谢@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;                   
    ?>