使用 PHP SimpleXml 从 xmlfeed 选择特定 id

发布于 2024-11-06 03:19:13 字数 778 浏览 0 评论 0原文

我有一个使用 ID 或类型的 XML Feed。我需要选择特定节点并将其转换为变量。

XML Feed 示例:

<tour>
<tourName>Amazon Riverboat Adventure</tourName>
<dossierCode>PVIIA</dossierCode>
<tripDetails>
<tripDetail type="StartFinish">ex Lima</tripDetail>
<tripDetail type="What's Included">Lots of stuff </tripDetail>
</tripDetails>

我一直在使用以下方法提取此数据:

<?php
if(!$xml=simplexml_load_file('xmlfeed.xml')){
    trigger_error('Error reading XML file',E_USER_ERROR);
}
foreach ($xml->tourName as $tourname)
foreach ($xml->dossierCode as $agentcode)
?>

但是我不确定如何将 提取为 $startfinish。

有人可以帮忙吗?

非常感谢!

I have an XML Feed which is using ID's or Types. I need to select the specific node and convert it to a variable.

Example XML Feed:

<tour>
<tourName>Amazon Riverboat Adventure</tourName>
<dossierCode>PVIIA</dossierCode>
<tripDetails>
<tripDetail type="StartFinish">ex Lima</tripDetail>
<tripDetail type="What's Included">Lots of stuff </tripDetail>
</tripDetails>

I have been extracting this data by using:

<?php
if(!$xml=simplexml_load_file('xmlfeed.xml')){
    trigger_error('Error reading XML file',E_USER_ERROR);
}
foreach ($xml->tourName as $tourname)
foreach ($xml->dossierCode as $agentcode)
?>

However I am unsure how I can extract the <tripDetail type="StartFinish"> as $startfinish.

Can anyone help?

Many thanks!

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

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

发布评论

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

评论(1

凉栀 2024-11-13 03:19:13

http://www.php.net/manual/en/simplexmlelement.attributes.php

<?php
echo $xmlAsString = <<<XML
<tour>
<tourName>Amazon Riverboat Adventure</tourName>
<dossierCode>PVIIA</dossierCode>
<tripDetails>
<tripDetail type="StartFinish">ex Lima</tripDetail>
<tripDetail type="What's Included">Lots of stuff</tripDetail>
</tripDetails>
</tour>


XML;
$xml = simplexml_load_string($xmlAsString);
foreach ($xml->tourName as $tourname) {
    var_dump($tourname);
}
foreach ($xml->dossierCode as $agentcode) {
    var_dump($agentcode);
}

foreach ($xml->tripDetails as $tripDetails) {
    foreach ($tripDetails as $tripDetail) {
        $attributes = $tripDetail->attributes();
        echo 'Content of the type: ' . $attributes['type'] . PHP_EOL .
             'Content of the whole tag: ' . $tripDetail  . PHP_EOL;
    }
}

http://www.php.net/manual/en/simplexmlelement.attributes.php

<?php
echo $xmlAsString = <<<XML
<tour>
<tourName>Amazon Riverboat Adventure</tourName>
<dossierCode>PVIIA</dossierCode>
<tripDetails>
<tripDetail type="StartFinish">ex Lima</tripDetail>
<tripDetail type="What's Included">Lots of stuff</tripDetail>
</tripDetails>
</tour>


XML;
$xml = simplexml_load_string($xmlAsString);
foreach ($xml->tourName as $tourname) {
    var_dump($tourname);
}
foreach ($xml->dossierCode as $agentcode) {
    var_dump($agentcode);
}

foreach ($xml->tripDetails as $tripDetails) {
    foreach ($tripDetails as $tripDetail) {
        $attributes = $tripDetail->attributes();
        echo 'Content of the type: ' . $attributes['type'] . PHP_EOL .
             'Content of the whole tag: ' . $tripDetail  . PHP_EOL;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文