使用 php simplexml 显示 XML 数据

发布于 2024-11-04 09:53:32 字数 3455 浏览 0 评论 0原文

我有一段 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 对象的一部分,

但是前两个回显不打印任何内容,而所有其他回显都正确打印,具体来说,我无法访问中的数据,

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

如果我这样做的话也可以进行调试

print_r($currentRecord->$dateFirst);

打印

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

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

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>

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 
}

where $reportDataXmlrecords is the part of the simpleXML object from the parent node of

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

it prints

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

Any help would be greatly appreciated. Thank you.

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

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

发布评论

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

评论(1

森罗 2024-11-11 09:53:32

你的问题是,当你这样做时,

$currentRecord->$dateFirst['month']

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

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

因为 字符串可以通过数组表示法的偏移量来访问,但非整数偏移量会转换为整数因为将 'month' 转换为整数是0,您正在尝试执行 $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

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

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

旁注,当问题中显示的 XML 实际上是您使用 SimpleXml 加载的 XML 时,例如,当 是根节点时,则执行

$reportDataXmlrecords->records->record

操作不起作用,因为 $reportDataXmlrecords 已经是根节点,您必须省略->records 如果您想迭代其中的记录元素。

You problem is when you do

$currentRecord->$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"

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

On a sidenote, when the XML shown in your question is really the XML you are loading with SimpleXml, e.g. when <records> is the root node, then doing

$reportDataXmlrecords->records->record

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.

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