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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?