【问题标题】:function to retrieve data from xml从 xml 中检索数据的函数
【发布时间】:2012-08-02 07:32:00
【问题描述】:

(这和I can't get all the data from an xml file with php有些不同)

目前我使用静态列表来检索一系列数据,例如

    $q1_result =  $tab->q1['listEntry'];
$q2_result =  $tab->q2['listEntry'];
$q3_result =  $tab->q3['listEntry'];
$q4_result =  $tab->q4['listEntry'];
$q5_result =  $tab->q5['listEntry'];
$q6_result =  $tab->q6['listEntry'];
$q7_result =  $tab->q7['listEntry'];
$q8_result =  $tab->q8['listEntry'];
$q9_result =  $tab->q9['listEntry'];
$q10_result =  $tab->q10['listEntry'];

如何检索不同数量的(连续的)实例,这样我就不必一直添加到此列表或从中删除? (我需要遍历所有 $tab->q(integer)['listEntry'] 条目)


这是我从中获取数据的 xml 文件的示例,我可以获取数据,但不知道如何循环遍历我想要的那些(上面列表中的那些):

magic5Out version="2.1.0">
<Report customerPK="Survey_30" locationPK="4439" userPK="1343895457" template="13642" formDate="2012-08-02T00:00:00" dateTimeStarted="2012-08-02T10:18:34" dateTimeMobileReleased="2012-08-02T10:20:03" currentStatus="5" reportGuid="b9f7681f-3468-402a-8dcc-60f0915b7d42">
<Results>
<Item itemPK="SurveyTab_200">
<q1 listEntry="No" listEntryId="239128"/>
<q1Comments text="latest test"/>
<q2 listEntry="No" listEntryId="239128"/>
<q2Comments text=""/>
<q3 listEntry="No" listEntryId="239128"/>
<q3Comments text=""/>
<q4 listEntry="No" listEntryId="239128"/>
<q4Comments text=""/>
<q5 listEntry="No" listEntryId="239128"/>
<q5Comments text=""/>
<q6 listEntry="No" listEntryId="239128"/>
<q6Comments text=""/>
<q7 listEntry="No" listEntryId="239128"/>
<q7Comments text=""/>
<q8 listEntry="No" listEntryId="239128"/>
<q8Comments text=""/>
<q9 listEntry="No" listEntryId="239128"/>
<q9Comments text=""/>
<q10 listEntry="No" listEntryId="239128"/>
<q10Comments text=""/>
<q11 listEntry="No" listEntryId="239128"/>
<q11Comments text=""/>
<q12 listEntry="No" listEntryId="239128"/>
<q12Comments text=""/>
<q13 listEntry="No" listEntryId="239128"/>
<q13Comments text=""/>
<q14 listEntry="No" listEntryId="239128"/>
<q14Comments text=""/>
<q15 listEntry="No" listEntryId="239128"/>
<q15Comments text=""/>
<q16 listEntry="No" listEntryId="239128"/>
<q16Comments text=""/>
<q17 listEntry="No" listEntryId="239128"/>
<q17Comments text=""/>
<photo/>
<sketch/>
<item_comments text=""/>
</Item>
<Item itemPK="SurveyTab_201">
<q1 listEntry="Yes" listEntryId="239127"/>
<q1Comments text="latest test 10:19"/>
<q2 listEntry="Yes" listEntryId="239127"/>
<q2Comments text=""/>
<q3 listEntry="Yes" listEntryId="239127"/>
<q3Comments text=""/>
<q4 listEntry="Yes" listEntryId="239127"/>
<q4Comments text=""/>
<q5 listEntry="Yes" listEntryId="239127"/>
<q5Comments text=""/>
<q6 listEntry="Yes" listEntryId="239127"/>
<q6Comments text=""/>
<q7 listEntry="Yes" listEntryId="239127"/>
<q7Comments text=""/>
<q8 listEntry="Yes" listEntryId="239127"/>
<q8Comments text=""/>
<q9 listEntry="Yes" listEntryId="239127"/>
<q9Comments text=""/>
<q10 listEntry="Yes" listEntryId="239127"/>
<q10Comments text=""/>
<q11 listEntry="Yes" listEntryId="239127"/>
<q11Comments text=""/>
<q12 listEntry="Yes" listEntryId="239127"/>
<q12Comments text=""/>
<q13 listEntry="Yes" listEntryId="239127"/>
<q13Comments text=""/>
<q14 listEntry="Yes" listEntryId="239127"/>
<q14Comments text=""/>
<q15 listEntry="Yes" listEntryId="239127"/>
<q15Comments text=""/>
<q16 listEntry="Yes" listEntryId="239127"/>
<q16Comments text=""/>
<q17 listEntry="Yes" listEntryId="239127"/>
<q17Comments text=""/>
<photo/>
<sketch/>
<item_comments text=""/>
</Item>
<surveyorComments0 text="10:19 test"/>
<surveyorName text="N"/>
<surveyorSig opFile="D:\Sites\WebApp_eden\Output\2100\XMLSurvey\Attachments\5b2fd203-77ed-4165-a72c-ba82736df2ab.png"/>
</Results>
</Report>
</magic5Out>

【问题讨论】:

    标签: php xml xml-parsing


    【解决方案1】:

    使用 SimpleXML

    下面是一个 XML 文件:

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <note>
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Don't forget me this weekend!</body>
    </note>
    

    例子

    <?php
    $xml = simplexml_load_file("test.xml");
    
    echo $xml->getName() . "<br />";
    
    foreach($xml->children() as $child)
      {
      echo $child->getName() . ": " . $child . "<br />";
      }
    ?>
    

    上述代码的输出将是: 笔记 至:托夫 来自:贾尼 标题:提醒 body:这个周末别忘了我!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-01
      • 2020-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多