duandu5846 2017-01-25 16:09
浏览 32
已采纳

如何在使用php解析时从XML文件中获取链接和粗体表示法?

<?xml version="1.0" encoding="utf-8"?>
<activities>
<activity category="Team">
<date><h4>Date: January 15, 2017</h4></date>
<work> 
    <li><b>MR X</b> working on X</li>
    <li><b>MR Y</b> working on Y</li>
    <li><b>MR Z</b> working on <a href="#">Z</a></li>
</work>
</activity>
</activities>

The above one is the XML file. Suppose the file name is activities.xml.

Now I want to parse the data in php. But I want to get the <b> and <a> tag intact. Means I want to get the line <b>MR Z</b> working on <a href="#">Z</a> directly when I parse using li tag. Is that possible? How to get the text with the links and show it?

My php code is something like below:

<?php
 $xmls=simplexml_load_file("activities.xml") or die("Error: Cannot create object");
foreach ($xmls as $xml) {
 echo $xml->date;
 echo $xml->work;
}
?>
  • 写回答

2条回答 默认 最新

  • doushenxu7294 2017-01-25 16:36
    关注

    You can use asXML function to ouput the way you want:

    foreach ($xmls->activity as $xml) {
        echo $xml->date->asXml();
        echo $xml->work->asXml();
    }
    

    But the use of <![CDATA[ ... ]> is a better choice here, is the correct way to send this kind of data.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分