使用 php simplexml 显示 XML 数据
我有一段 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的问题是,当你这样做时,
PHP 将首先评估
$dateFirst['month']
作为一个整体,然后再尝试将其用作属性,因为 字符串可以通过数组表示法的偏移量来访问,但非整数偏移量会转换为整数因为将 'month' 转换为整数是0,您正在尝试执行
$currentRecord->d
:您可以使用大括号访问连字符的属性:
旁注,当问题中显示的 XML 实际上是您使用 SimpleXml 加载的 XML 时,例如,当
是根节点时,则执行操作不起作用,因为
$reportDataXmlrecords
已经是根节点,您必须省略->records
如果您想迭代其中的记录元素。You problem is when you do
PHP will first evaluate
$dateFirst['month']
as a whole before trying to use it as a propertybecause 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
:You can access hyphenated properties with curly braces:
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 doingcannot 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.