使用php simplexml从XML显示数据

时间:2011-04-29 15:06:03

标签: php xml simplexml

我有一段XML如下

<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>

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

$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是来自

父节点的simpleXML对象的一部分

但是前两个echo没有打印任何东西而且所有其他都正确打印,具体来说,我无法访问

中的数据
<date-first month="10" year="1999"/>
<date-last month="04" year="2011"/>

如果我这样做也是为了调试

print_r($currentRecord->$dateFirst);

打印

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

非常感谢任何帮助。谢谢。

1 个答案:

答案 0 :(得分:1)

问题在于你做什么

$currentRecord->$dateFirst['month']

在尝试将$dateFirst['month']用作属性

之前,PHP将首先评估$dateFirst = 'date-first'; var_dump( $dateFirst['month'] ); // gives "d"
$currentRecord->d

因为strings can be accessed by offset with array notation,但是非整数偏移被转换为整数,并且因为将'month'转换为整数是0,所以你试图做$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

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

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

<records>

在旁注中,当您的问题中显示的XML实际上是您使用SimpleXml加载的XML时,例如当$reportDataXmlrecords->records->record 是根节点时,然后执行

$reportDataXmlrecords

无法工作,因为->records已经是根节点,如果要迭代其中的记录元素,则必须省略{{1}}。