使用 php simplexml 显示来自 XML 的数据 [英] display data from XML using php simplexml

查看:23
本文介绍了使用 php simplexml 显示来自 XML 的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段 XML 如下

I have a piece of XML which is as follows

<records count="2">
  <record>
    <firstname>firstname</firstname>
    <middlename>middlename</middlename>
    <lastname>lastname</lastname>
    <namesuffix/>
    <address>
      <street-number>demo</street-number>
      <street-pre-direction/>
      <street-name>demo</street-name>
      <street-post-direction/>
      <street-suffix>demo</street-suffix>
      <city>demo</city>
      <state>NY</state>
      <zip>demo</zip>
      <zip4>demo</zip4>
      <county>demo</county>
    </address>
    <phonenumberdetails>
      <phonenumber>demo</phonenumber>
      <listed>demo</listed>
      <firstname>demo</firstname>
    </phonenumberdetails>
    <dob day="" month="" year=""/>
    <age/>
    <date-first month="10" year="1999"/>
    <date-last month="04" year="2011"/>
  </record>
  <record>
    <firstname>firstname</firstname>
    <middlename>middlename</middlename>
    <lastname>lastname</lastname>
    <namesuffix/>
    <address>
      <street-number>demo</street-number>
      <street-pre-direction/>
      <street-name>demo</street-name>
      <street-post-direction/>
      <street-suffix>demo</street-suffix>
      <city>demo</city>
      <state>NY</state>
      <zip>demo</zip>
      <zip4>demo</zip4>
      <county>demo</county>
    </address>
    <phonenumberdetails>
      <phonenumber>demo</phonenumber>
      <listed>demo</listed>
      <firstname>demo</firstname>
    </phonenumberdetails>
    <dob day="" month="" year=""/>
    <age/>
    <date-first month="10" year="1999"/>
    <date-last month="04" year="2011"/>
  </record>
</records>

现在,除了 date-first 和 date-last 元素之外,我已经能够使用 SimpleXML 获取 PHP 中的所有数据.我一直在使用下面列出的代码

Now, I have been able to get all the data in PHP using SimpleXML except for the date-first and date-last elements. I have been using code listed below

$dateFirst           = 'date-first';
$dateLast            = 'date-last';
$streetNumber        = 'street-number';
$streetPreDirection  = 'street-pre-direction';
$streetName          = 'street-name';
$streetPostDirection = 'street-post-direction';
$streetSuffix        = 'street-suffix';
$unitDesignation     = 'unit-designation';
$unitNumber          = 'unit-number';

foreach ($reportDataXmlrecords->records->record as $currentRecord) {
    echo $currentRecord->$dateFirst['month'].'/'.$currentRecord->$dateFirst['year'];
    echo $currentRecord->$dateLast['month'].'/'.$currentRecord->$dateLast['year'];
    echo $currentRecord->address->$streetNumber;
    $currentRecord->address->$streetName; // ......and so on 
}

其中 $reportDataXmlrecords 是来自

但前两个回声不打印任何内容,其他所有都正确打印,具体来说,我无法访问

But the first two echo's don't print anything and all the other are printing correctly, specifically, I cant access the data in

<date-first month="10" year="1999"/>
<date-last month="04" year="2011"/>

如果我这样做,也用于调试

Also for debugging if I do

print_r($currentRecord->$dateFirst);

打印

SimpleXMLElement Object ( 
    [@attributes] => Array ( [month] => 10 [year] => 1999 ) 
)

任何帮助将不胜感激.谢谢.

Any help would be greatly appreciated. Thank you.

推荐答案

问题在于你什么时候做

$currentRecord->$dateFirst['month']

PHP 会首先将 $dateFirst['month'] 作为一个整体进行评估,然后再尝试将其用作属性

PHP will first evaluate $dateFirst['month'] as a whole before trying to use it as a property

$dateFirst = 'date-first';
var_dump( $dateFirst['month'] ); // gives "d"

因为 字符串可以通过使用数组符号的偏移量,但非整数偏移量被转换为整数,并且因为将 'month' 转换为整数是 0,您正在尝试执行 $currentRecord->d:

because strings can be accessed by offset with array notation, but non-integer offsets are converted to integer and because casting 'month' to integer is 0, you are trying to do $currentRecord->d:

$xml = <<< XML
<record>
    <date-first month="jan"/>
    <d>foo</d>
</record>
XML;

$record = simplexml_load_string($xml);
$var    = 'date-first';
echo $record->$var['month']; // foo

您可以使用花括号访问带连字符的属性:

You can access hyphenated properties with curly braces:

$record->{'date-first'}['month'] // jan

<小时>

顺便说一句,当您的问题中显示的 XML 实际上是您使用 SimpleXml 加载的 XML 时,例如当 是根节点时,则做

$reportDataXmlrecords->records->record

不能工作,因为 $reportDataXmlrecords 已经是根节点,如果你想遍历记录元素,你必须省略 ->records

cannot work, because $reportDataXmlrecords is already the root node and you'd have to omit the ->records if you want to iterate over the record elements in it.

这篇关于使用 php simplexml 显示来自 XML 的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆