从xml文件中获取属性和值

时间:2015-06-30 06:41:45

标签: php xml

我有以下xml文件:

<ROW>
    <rating>10</rating>
    <answers>Very Satisfied</answers>
    <transaction_date>01/21/2015</transaction_date>
    <date>05/30/2015</date>
    <username>got2bsunny4me</username>
    <link>http://widget.resellerratings.com/store/Amberen/review/#comment8877470</link>
    <comment><![CDATA[my extreme hot flashes became less frequent within a couple weeks, and not as severe.  after 2 months of taking Amberen, I didn't need to take as often.  I've used Amberen before and my hot flashes...]]></comment>
</ROW>
<ROW>
     <rating>10</rating>
     <answers>Very Satisfied</answers>
     <transaction_date>03/23/2015</transaction_date>
     <date>05/18/2015</date>
     <username>musiclvr24</username>
     <link>http://widget.resellerratings.com/store/Amberen/review/#comment8785090</link>
     <comment><![CDATA[Ordering was very easy!  The whole website was very informative and very useful as a health resource.  Thank you very much!]]></comment>
</ROW> 

这是我解析xml文件的php文件。

<pre>
<?php

$seller_id = 113;
$xml = simplexml_load_string(file_get_contents("http://widget.atings.com/widget/snippet/?seller_id=$seller_id"));
foreach($xml->ROW as $row){  
//echo $row,'="',$s,"\"\n"; // to get all rows
 var_dump($row);
   echo '<div class="spans"></div>';
}



//var_dump($xml->ROW) // to get single row
?>
</pre> 

我需要在每个行的新创建的div上显示每一行的属性和值。我怎样才能做到这一点?

3 个答案:

答案 0 :(得分:1)

如果可能的话,在XML的开头和结尾添加一些主要标签,其余的代码将正常工作......

<document>
<ROW>
<rating>10</rating>
<answers>Very Satisfied</answers>
<transaction_date>01/21/2015</transaction_date>
<date>05/30/2015</date>
<username>got2bsunny4me</username>
<link>http://widget.resellerratings.com/store/Amberen/review/#comment8877470</link>
<comment><![CDATA[my extreme hot flashes became less frequent within a couple weeks, and not as severe.  after 2 months of taking Amberen, I didn't need to take as often.  I've used Amberen before and my hot flashes...]]></comment>
</ROW>

<ROW>
<rating>10</rating>
<answers>Very Satisfied</answers>
<transaction_date>03/23/2015</transaction_date>
<date>05/18/2015</date>
<username>musiclvr24</username>
<link>http://widget.resellerratings.com/store/Amberen/review/#comment8785090</link>
<comment><![CDATA[Ordering was very easy!  The whole website was very informative and very useful as a health resource.  Thank you very much!]]></comment>
</ROW>
</document>

答案 1 :(得分:0)

您需要附上一些主要标签,例如“ROWS”,其中包含其他标签。请检查下面的XML和代码以访问元素值。

$xml = <<<XML
<ROWS>
    <ROW>
        <rating>10</rating>
        <answers>Very Satisfied</answers>
        <transaction_date>01/21/2015</transaction_date>
        <date>05/30/2015</date>
        <username>got2bsunny4me</username>
        <link>http://widget.resellerratings.com/store/Amberen/review/#comment8877470</link>
        <comment><![CDATA[my extreme hot flashes became less frequent within a couple weeks, and not as severe.  after 2 months of taking Amberen, I didn't need to take as often.  I've used Amberen before and my hot flashes...]]></comment>
    </ROW>
    <ROW>
        <rating>10</rating>
        <answers>Very Satisfied</answers>
        <transaction_date>01/21/2015</transaction_date>
        <date>05/30/2015</date>
        <username>got2bsunny4me</username>
        <link>http://widget.resellerratings.com/store/Amberen/review/#comment8877470</link>
        <comment><![CDATA[my extreme hot flashes became less frequent within a couple weeks, and not as severe.  after 2 months of taking Amberen, I didn't need to take as often.  I've used Amberen before and my hot flashes...]]></comment>
    </ROW>
</ROWS>
XML;

$xml = simplexml_load_string($xml);
foreach($xml as $row){  
    echo $row->rating;
    echo $row->answers;
}

答案 2 :(得分:0)

$xml=simplexml_load_file("http://widget.atings.com/widget/snippet/?seller_id=$seller_id") or die("Error: Cannot create object");                                
foreach($xml->children() as $child) :

endforeach;

// Load xml file content

$xml=simplexml_load_file("http://example.com/xml.xml") or die("Error: Cannot create object");


//Get children with foreach.


foreach($xml->children() as $child)
{ $inc = 1;
    foreach($child->children() as $childs){                                 
    foreach($childs->children() as $childss){
    if($inc == 7) :                                             
    if($childss->getName() === 'description') :
    echo '<div class="descriptionOfNisab">'.  $childss . '</div>';
    endif;
    endif;                                              
    }                                   
    $inc ++;
    }
}