2013-08-03 90 views
0

我有一個PHP文件,它讀取XML文件並使用for循環將數據顯示爲HTML5 <article>對象。所有這一切都像一個冠軍。我遇到的問題似乎與XML文件本身中的數據有關。我的數據與此類似:XML數據中的超鏈接不顯示在PHP文件中

<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實體名稱和一些其他的東西無濟於事。感謝幫助。謝謝。

+0

你可能會嘗試在'href'而不是''''周圍放置'''',以確保不會造成任何問題。 – dfockler

回答

2

CDATA - 不應由XML解析器解析的文本數據。

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

編輯

我可以建議PHP的Dom,這是非常強大,因爲它可以讓你輕鬆解析HTML內容。這是一個流行的利弊,利弊問題,你可能有興趣在What's the difference between PHP's DOM and SimpleXML extensions?

$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; 
0

您需要使用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