XML数据中的超链接;t在PHP文件中显示


Hyperlink in XML Data Doesn't Display in PHP File

我有一个PHP文件,它读取一个XML文件,并使用for循环将数据显示为HTML5 <article>对象。所有这些都像冠军一样发挥作用。我遇到的问题似乎与XML文件本身中的数据有关。我的数据与此类似:

<articles>
  <article>
    <num>1</num><text>This is one.</text>
  </article>
  <article>
    <num>2</num><text>This is <a href='./two.htm'>two</a>.</text>
  </article>
  <article>
    <num>3</num><text>This is three.</text>
  </article>
</articles>

我像这样加载XML:

$xml = simplexml_load_file("./articles_test.xml");

输出如下:

This is one.
This is .
This is three.

我尝试过转义、引用、使用HTML实体名称和其他一些方法,但都无济于事。感谢您的帮助。谢谢

CDATA-XML解析器不应解析的文本数据。

<article>
    <num>2</num>
    <text>
        <![CDATA[
            This is <a href='./two.htm'>two</a>.
        ]]>
    </text>
</article>

编辑

我可以推荐PHP的Dom,它非常强大,因为它可以让你轻松地解析HTML内容。这里有一个你可能感兴趣的热门正反问题;s PHP之间的区别;DOM和SimpleXML扩展?

$doc = new DomDocument();
$file = "../articles_test.xml";
$doc->load($file);
$xPath = new DomXPath($doc);
$article_text = $xPath->query("//article/text")->item(1);
echo $article_text->nodeValue;

您需要使用asXML()来输出:

<?php
$string = <<<XML
<?xml version='1.0'?> 
<articles>
  <article>
    <num>1</num><text>This is one.</text>
  </article>
  <article>
    <num>2</num><text>This is <a href='./two.htm'>two</a>.</text>
  </article>
  <article>
    <num>3</num><text>This is three.</text>
  </article>
</articles>
XML;
$xml = simplexml_load_string($string);
print_r($xml->asXML());

请在此处查看其实际操作:http://codepad.org/EW9yQcdM