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


PHP XML DOM unwanted escaping characters, i cannot write &qnot;

我有一个问题

$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 
"
*/
为什么会这样??

就这么做:

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

你将有你的"在xml。

您必须直接编写", DomDocument负责转义。

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

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

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

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

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