使用 SimpleXML 循环多个条目

发布于 2024-09-14 17:05:34 字数 2278 浏览 5 评论 0原文

您好,我正在尝试使用 php 中的 simplexml 解析 xml feed。 xml feed 的布局如下:

<Member>
    <MemberType>Full</MemberType>
    <JoinDate>2010-06-12</JoinDate>
    <DataType>A</DataType>
    <Data>
        <FirstName>Ted</FirstName>
        <LasttName>Smith</LasttName>
        <Data1>56</Data1>
        <Data2>100</Data2>
        <Data3>120</Data3>
    </Data>
</Member>
<Member>
    <MemberType>Full</MemberType>
    <JoinDate>2010-06-12</JoinDate>
    <DataType>B</DataType>
    <Data>
        <FirstName>Ted</FirstName>
        <LasttName>Smith</LasttName>
        <Data1>57</Data1>
        <Data2>110</Data2>
        <Data3>130</Data3>
    </Data>
</Member>
<Member>
    <MemberType>Full</MemberType>
    <JoinDate>2010-06-12</JoinDate>
    <DataType>C</DataType>
    <Data>
        <FirstName>Ted</FirstName>
        <LasttName>Smith</LasttName>
        <Data4>58</Data4>
        <Data5>115</Data5>
        <Data6>230</Data6>
    </Data>
</Member>

member 元素在 xml 文档中一遍又一遍地循环,但其中的数据发生了变化。我想做的是将某些成员的所有数据输入到 SQL 数据库中。所以理想情况下,我想将所有这些输入到数据库中的一行中。 xml feed 包含不同的成员类型,例如“完整”或“关联”。

目前,我正在尝试循环所有完整成员,并获取该特定成员的所有数据。每个成员的数据分为三个部分,每个部分都有一个单独的成员标签,因此上面的 Ted Bloggs 在三个成员标签中都有数据,其中数据类型是 A、B 和 C

$PLF = simplexml_load_file('../XML/Members.xml');

foreach ($PLF->Root->xpath('//Member') as $member) {

    if ($member->MemberType == 'Full') {


        echo $member->MemberType.'<br/>';
        echo $member->JoinDate.'<br />';
        echo $member->DataType.'<br/>';
        echo $member->Data->FirstName.'<br/>';
        echo $member->Data->LastName.'<br/>';
        echo $member->Data->Data1.'<br/>';
        echo $member->Data->Data2.'<br/>';
        echo '<br />';


        }}

我目前的代码只能从每个循环中的第一个类型(类型 A),我真的想将所有类型 A、B 和 C 组合到同一个循环中。因此,我可以将 Ted Smith 等每个成员的所有数据放入数据库中的一行中。

Hi Im trying to parse an xml feed using simplexml in php.
The xml feed is laid out as follows:

<Member>
    <MemberType>Full</MemberType>
    <JoinDate>2010-06-12</JoinDate>
    <DataType>A</DataType>
    <Data>
        <FirstName>Ted</FirstName>
        <LasttName>Smith</LasttName>
        <Data1>56</Data1>
        <Data2>100</Data2>
        <Data3>120</Data3>
    </Data>
</Member>
<Member>
    <MemberType>Full</MemberType>
    <JoinDate>2010-06-12</JoinDate>
    <DataType>B</DataType>
    <Data>
        <FirstName>Ted</FirstName>
        <LasttName>Smith</LasttName>
        <Data1>57</Data1>
        <Data2>110</Data2>
        <Data3>130</Data3>
    </Data>
</Member>
<Member>
    <MemberType>Full</MemberType>
    <JoinDate>2010-06-12</JoinDate>
    <DataType>C</DataType>
    <Data>
        <FirstName>Ted</FirstName>
        <LasttName>Smith</LasttName>
        <Data4>58</Data4>
        <Data5>115</Data5>
        <Data6>230</Data6>
    </Data>
</Member>

where the member element loops over and over again in the xml doc, but the data inside it changes. What im trying to do is enter all the data for certain members into an sql database. So ideally i want to enter this all in one line in the db. The xml feed contains different member types, like 'full' or 'associate'.

At the moment i am trying to loop through all the full members, and get all the data for this particular member. The data for each member is broken up into three parts each with a separate member tag, so above Ted Bloggs has data in three member tags, where Datatype is A, B and C

$PLF = simplexml_load_file('../XML/Members.xml');

foreach ($PLF->Root->xpath('//Member') as $member) {

    if ($member->MemberType == 'Full') {


        echo $member->MemberType.'<br/>';
        echo $member->JoinDate.'<br />';
        echo $member->DataType.'<br/>';
        echo $member->Data->FirstName.'<br/>';
        echo $member->Data->LastName.'<br/>';
        echo $member->Data->Data1.'<br/>';
        echo $member->Data->Data2.'<br/>';
        echo '<br />';


        }}

the code i have at the moment can only pull data from the first type (type A) in each loop, and i really want to combine all types A, B, and C into the same loop. So i can get all the data for each member like Ted Smith into one line in the DB.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一绘本一梦想 2024-09-21 17:05:34

我使用简单的 xml 来读取一些远程文件并循环遍历它们。这是我的代码:

         $sUrl      = "some url";
         $sContent  = file_get_contents($sUrl);

        $oXml       = new SimpleXMLElement($sContent);

        $aReturn    = array();

        foreach ($oXml->children() as $oStation)
        {
            $iRackId    = (int)$oStation->rack_id;
            $dLong      = (double)str_replace(",", ".", $oStation->longitute);
            $dLati      = (double)str_replace(",", ".", $oStation->latitude);
            $sDescription = (string)$oStation->description;

            $aRes   = array();
            $aRes['rack_id']        = $iRackId;
            $aRes['longitute']      = $dLong;
            $aRes['latitude']       = $dLati;
            $aRes['description']    = utf8_decode($sDescription);

            if ($dLong > 0 && $dLati > 0)
                $aReturn[$iRackId]  = $aRes;
        }

我所做的是将 XML 文件的结果放入一个数组中。稍后在代码中我也将该数据保存到数据库中。

希望这对您有帮助。我不使用 xpath...除了问题之外什么都没有,而且没有时间解决它们。但这似乎对我有用。

兄弟,
保罗·皮伦

I use simple xml to read some remote files and loop thru them as well. This is my code:

         $sUrl      = "some url";
         $sContent  = file_get_contents($sUrl);

        $oXml       = new SimpleXMLElement($sContent);

        $aReturn    = array();

        foreach ($oXml->children() as $oStation)
        {
            $iRackId    = (int)$oStation->rack_id;
            $dLong      = (double)str_replace(",", ".", $oStation->longitute);
            $dLati      = (double)str_replace(",", ".", $oStation->latitude);
            $sDescription = (string)$oStation->description;

            $aRes   = array();
            $aRes['rack_id']        = $iRackId;
            $aRes['longitute']      = $dLong;
            $aRes['latitude']       = $dLati;
            $aRes['description']    = utf8_decode($sDescription);

            if ($dLong > 0 && $dLati > 0)
                $aReturn[$iRackId]  = $aRes;
        }

What I do is I put the result of the XML file into an array. Later in the code I save that data to the database as well.

Hope this helped you. I don't use xpath... had nothing but problems with it and didn't had the time to sort them out. This seems to work for me though.

Br,
Paul Peelen

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文