PHP 解析 XML 数据
我在 PHP 中成功解析 xml 文件,但在解析尝试输出表的特定条目时遇到困难。
我的 xml 如下
<OutputData Name="ExchangeRate" ContentTypeID="">
<DataTitle>Exchange</DataTitle>
<DataInfo>
<TABLE CLASS="DataTable">
<TR><TD>1.00 GBP = 1.68 ALL</TD></TR>
<TR><TD>1.00 USD = 1.58 ALL</TD></TR>
<TR><TD>1.00 EUR = 1.35 ALL</TD></TR>
<TR><TD>Currency conversion rates as of January 2012</TD></TR>
</TABLE>
</DataInfo>
</OutputData>
如果我尝试以这种方式输出, $info = $outputinfo->DataInfo;
我收到错误。
有什么帮助吗?
我的代码如下
$xmlfile = $currenttitle.".xml";
$info = simplexml_load_file($xmlfile);
$region = $country->Region[0];
$section = $region->Section;
if($info){
foreach ($region->Section as $sectioninfo){
$title = $sectioninfo->SectionTitle;
echo "<b>$title</b><br />\n";
$output = $sectioninfo->OutputData;
foreach ($sectioninfo->OutputData as $outputinfo){
$titleinfo = $outputinfo->DataTitle;
$info = $outputinfo->DataInfo;
}
}
,对于上面的部分,我得到标题显示,作为交换,但表没有呈现。
解决方案 感谢 h4b0 让我找到了正确的方向
$rowinfo = $outputinfo->DataInfo->TABLE->TR->TD;
foreach ($outputinfo->DataInfo->TABLE->TR as $rowexchange){
echo $rowexchange->TD."<br />";
}
I am parsing an xml file successfully in PHP, but having difficulty parsing a specific entry trying to output a table.
My xml is as follow
<OutputData Name="ExchangeRate" ContentTypeID="">
<DataTitle>Exchange</DataTitle>
<DataInfo>
<TABLE CLASS="DataTable">
<TR><TD>1.00 GBP = 1.68 ALL</TD></TR>
<TR><TD>1.00 USD = 1.58 ALL</TD></TR>
<TR><TD>1.00 EUR = 1.35 ALL</TD></TR>
<TR><TD>Currency conversion rates as of January 2012</TD></TR>
</TABLE>
</DataInfo>
</OutputData>
if I try to output it this way$info = $outputinfo->DataInfo;
I am getting an error.
any help?
My code is as follow
$xmlfile = $currenttitle.".xml";
$info = simplexml_load_file($xmlfile);
$region = $country->Region[0];
$section = $region->Section;
if($info){
foreach ($region->Section as $sectioninfo){
$title = $sectioninfo->SectionTitle;
echo "<b>$title</b><br />\n";
$output = $sectioninfo->OutputData;
foreach ($sectioninfo->OutputData as $outputinfo){
$titleinfo = $outputinfo->DataTitle;
$info = $outputinfo->DataInfo;
}
}
for the section above, I get the Title display, as Exchange, but the Table is not rendered.
SOLUTION
Thanks to h4b0 who put me in the right direction
$rowinfo = $outputinfo->DataInfo->TABLE->TR->TD;
foreach ($outputinfo->DataInfo->TABLE->TR as $rowexchange){
echo $rowexchange->TD."<br />";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想如果您尝试
$info = $outputinfo->DataInfo->TABLE->TR->TD
,您不会收到错误。如果我错了,请发布您的错误消息。I guess you won't get error if you try
$info = $outputinfo->DataInfo->TABLE->TR->TD
. If I'm wrong, post your error message.