PHP XML DOM不需要的转义字符,我不能写& qnot;

时间:2011-09-03 15:52:31

标签: php xml dom escaping special-characters

我遇到了问题

$xml2 = new DOMDocument;
$xml2->load($FilenamePost."/".$FilenameOfThePost.'.xml');

/* filename.xml */
$xpath2 = new DOMXPath($xml2);
$hrefs2 = $xpath2->evaluate("/page");

$Title = str_replace("-==single-quote==--"," " ",$Title);
$Title = str_replace('-==double-quote==--',' " ',$Title);

$Title = str_replace('"',' " ',$Title);

$Title = "Sssa "";

$href2 = $hrefs2->item(0);
$href2->setAttribute("PostTitle",$Title);

$xml2->save($FilenamePost."/".$FilenameOfThePost.'.xml');

/*
And when i try to write 
" 
in my xml, it keeps showing 
"
*/

为什么?我找不到合乎逻辑的解释......

3 个答案:

答案 0 :(得分:2)

就这样做:

$Title = str_replace("-==single-quote==--", ' " ',$Title);
$Title = str_replace('-==double-quote==--', ' " ',$Title);

你将在xml中拥有"

答案 1 :(得分:1)

你必须直接写",DomDocument负责逃避它。

$Title = "Sssa &";
...
...->setAttribute("PostTitle", $Title);

保存时,PostTitle属性将被正确转义:

<... PostTitle="Sssa &quot;" ...>

答案 2 :(得分:1)

setAttribute()默认情况下会进行转义,因此如果字符串已经转义,您可能需要使用html_entity_decode():

$href2->setAttribute("PostTitle", html_entity_decode($Title));