如何使用php将检索到的URL保存在xml文件中?

如何使用php将检索到的URL保存在xml文件中?,php,xml,Php,Xml,我可以存储普通字符串。但如果我试图存储GET方法url,它就无法存储 function updateX_xml($id,$val,$addre){ $xml = new DOMDocument(); $xml->load('autoGen/autoGen.xml'); $node = $xml->getElementsByTagName('root')->item(0) ; $xml_id = $xml->createElement("i

我可以存储普通字符串。但如果我试图存储GET方法url,它就无法存储

function updateX_xml($id,$val,$addre){

    $xml = new DOMDocument();
    $xml->load('autoGen/autoGen.xml');
    $node = $xml->getElementsByTagName('root')->item(0) ;
    $xml_id = $xml->createElement("id");
    $xml_addres = $xml->createElement("Address");
    $domAttribute = $xml->createAttribute('type');
    $domAttribute->value = 'xs:string';
    $xml_addres->appendChild($domAttribute);
    $xml_url = $xml->createElement("url");
    $xml_id->nodeValue=$id;
    $xml_url->nodeValue=$val;
    $xml_addres->nodeValue=$addre;
    $node->appendChild( $xml_id );
    $node->appendChild( $xml_url );
    $xml->formatOutput = true;
    $xml->save("autoGen/autoGen.xml");
}
如果我像updateX_xml1那样调用这个函数,'getdata?event_id=1&lan=en',addares;它不起作用


这将生成此警告。警告:第25行C:\xampp\htdocs\test\u file\u read\gen\u url.php中的updateX\u xml:unterminated entity reference lan=en

updateX_xml(1,'getdata?event_id=1&lan=en',"addaress");
另外,正如其他人提到的,您需要转义&,因为它是xml中的一个特殊字符,使用:


您必须转义HTML实体字符:

$val= htmlentities($str, ENT_XML1);
    $xml_url->nodeValue=$val;

如果要在XML/HTML中插入内容,则应始终使用该函数。这将把字符串转义为正确的XML语法

因此:


您还需要转义特殊字符。
$val= htmlentities($str, ENT_XML1);
    $xml_url->nodeValue=$val;
function updateX_xml($id,$val,$addre)
{
    $xml = new DOMDocument();
    $xml->load('autoGen/autoGen.xml');
    $node = $xml->getElementsByTagName('root')->item(0) ;
    $xml_id = $xml->createElement("id");
    $xml_addres = $xml->createElement("Address");
    $domAttribute = $xml->createAttribute('type');
    $domAttribute->value = 'xs:string';
    $xml_addres->appendChild($domAttribute);
    $xml_url = $xml->createElement("url");
    $xml_id->nodeValue=$id;
    $xml_url->nodeValue = htmlspecialchars($val);
    $xml_addres->nodeValue=$addre;
    $node->appendChild( $xml_id );
    $node->appendChild( $xml_url );
    $xml->formatOutput = true;
    $xml->save("autoGen/autoGen.xml");
}