duandu5846 2017-01-25 08: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 08: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部