将MySQL数据库中的XML读入PHP

时间:2016-06-05 20:19:54

标签: php xml simplexml

我的数据库中存储了以下XML结构:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Payouts>
<Range end="1" start="1">900</Range>
<Range end="2" start="2">650</Range>     
<Range end="3" start="3">450</Range>     
<Range end="4" start="4">330</Range>     
<Range end="5" start="5">230</Range>     
<Range end="6" start="6">175</Range>     
<Range end="7" start="7">120</Range>     
<Range end="8" start="8">95</Range>     
<Range end="9" start="9">80</Range>     
<Range end="10" start="10">70</Range>     
<Range end="15" start="11">50</Range>     
<Range end="20" start="16">35</Range>     
<Range end="25" start="21">25</Range>     
<Range end="30" start="26">18</Range>     
<Range end="40" start="31">15</Range>     
<Range end="50" start="41">14</Range>     
<Range end="75" start="51">13</Range>     
<Range end="100" start="76">12</Range>     
<Range end="125" start="100">11</Range>     
<Range end="150" start="126">10</Range>     
<Range end="200" start="151">9</Range>     
<Range end="350" start="201">8</Range>     
<Range end="360" start="351">7</Range>     
<Range end="380" start="361">6</Range>   
<Range end="400" start="381">5</Range>   
<Range end="420" start="401">4</Range>   
<Range end="440" start="421">3</Range>   
<Range end="460" start="441">2</Range>   
<Range end="480" start="461">1</Range>   
<Range end="500" start="481">1</Range>   
<Range end="520" start="501">1</Range>   
</Payouts>

这基本上是一系列选择。我想过滤这些结果并在最后获取值并开始,然后显示该行的值。

输出类似于以下内容:

1st - 900 2nd - 650 3rd - 450 4th - 330 5th - 230 6th - 175 7th - 120 8th - 95 9th - 80 10th - 70 11th to 15th - 50 16th to 20th - 35 21th to 25th - 25

我尝试使用以下代码读取XML文件:

$payout_xml = $event_details[0]['PAYOUT_DETAILS'];
$xml = new SimpleXMLElement($payout_xml);
echo "The Simple Element is: ".print_r($xml);
echo 'single value: <br />';
echo $xml->Payouts->Range->end; // get single value
echo "end of data";

当我print_r $payout_xml时,我得到以下结果:

SimpleXMLElement Object
(
[Range] => Array
    (
        [0] => 900
        [1] => 650
        [2] => 450
        [3] => 330
        [4] => 230
        [5] => 175
        [6] => 120
        [7] => 95
        [8] => 80
        [9] => 70
        [10] => 50
        [11] => 35
        [12] => 25
        [13] => 18
        [14] => 15
        [15] => 14
        [16] => 13
        [17] => 12
        [18] => 11
        [19] => 10
        [20] => 9
        [21] => 8
        [22] => 7
        [23] => 6
        [24] => 5
        [25] => 4
        [26] => 3
        [27] => 2
        [28] => 1
        [29] => 1
        [30] => 1
    )

)
The Simple Element is: 1 

虽然我没有访问数据。我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

end是属性,而不是元素。尝试类似:

echo $sxml->Range . '|' . $sxml->Range['end'] . '|' . $sxml->Range['start'];

将输出为

  

900 | 1 | 1

功能示例

$xml = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Payouts>
<Range end="1" start="1">900</Range>
<Range end="2" start="2">650</Range>     
<Range end="3" start="3">450</Range>     
<Range end="4" start="4">330</Range>     
<Range end="5" start="5">230</Range>     
<Range end="6" start="6">175</Range>     
<Range end="7" start="7">120</Range>     
<Range end="8" start="8">95</Range>     
<Range end="9" start="9">80</Range>     
<Range end="10" start="10">70</Range>     
<Range end="15" start="11">50</Range>     
<Range end="20" start="16">35</Range>     
<Range end="25" start="21">25</Range>     
<Range end="30" start="26">18</Range>     
<Range end="40" start="31">15</Range>     
<Range end="50" start="41">14</Range>     
<Range end="75" start="51">13</Range>     
<Range end="100" start="76">12</Range>     
<Range end="125" start="100">11</Range>     
<Range end="150" start="126">10</Range>     
<Range end="200" start="151">9</Range>     
<Range end="350" start="201">8</Range>     
<Range end="360" start="351">7</Range>     
<Range end="380" start="361">6</Range>   
<Range end="400" start="381">5</Range>   
<Range end="420" start="401">4</Range>   
<Range end="440" start="421">3</Range>   
<Range end="460" start="441">2</Range>   
<Range end="480" start="461">1</Range>   
<Range end="500" start="481">1</Range>   
<Range end="520" start="501">1</Range>   
</Payouts>';
$sxml = new SimpleXMLElement($xml);
echo $sxml->Range . '|' . $sxml->Range['end'] . '|' . $sxml->Range['start']; // get single value

演示:https://eval.in/583665

或者如果您想要遍历所有记录https://eval.in/583666

foreach($sxml->Range as $range){
     echo $range . '|' . $range['end'] . '|' . $range['start'] . "\n";
}