简体   繁体   English

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

[英]How to save retrived URLs in a xml file using php?

I can store normal string. 我可以存储普通字符串。 But if I tried to store GET method url it can not store. 但是,如果我尝试存储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");
}

if i call this function like this updateX_xml(1,'getdata?event_id=1 &lan=en',"addaress"); 如果我这样调用此函数updateX_xml(1,'getdata?event_id=1 &lan=en',"addaress"); it is not working. 它不起作用。

This will generate this warning. 这将生成此警告。 Warning: updateX_xml(): unterminated entity reference lan=en in C:\\xampp\\htdocs\\test_file_read\\gen_url.php on line 25 警告:updateX_xml():第25行的C:\\ xampp \\ htdocs \\ test_file_read \\ gen_url.php中未终止的实体引用lan = en

Try without space between parameters: 尝试在参数之间没有空格:

updateX_xml(1,'getdata?event_id=1&lan=en',"addaress");

Also, as others mentioned, you need to escape the "&", as it's a special character in xml using htmlspecialchars() : 另外,正如其他人提到的那样,您需要转义“&”,因为它是使用htmlspecialchars()在xml中的特殊字符:

 $xml_url->nodeValue = htmlspecialchars($val);

You have to escape HTML entity character: 您必须转义HTML实体字符:

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

If you are inserting something into XML/HTML you should always use the htmlspecialchars function. 如果要在XML / HTML中插入内容,则应始终使用htmlspecialchars函数。 this will escape your strings into correct XML syntax. 这样会将您的字符串转义为正确的XML语法。

So: 所以:

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");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM