繁体   English   中英

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

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

我可以存储普通字符串。 但是,如果我尝试存储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_xml(1,'getdata?event_id=1 &lan=en',"addaress"); 它不起作用。

这将生成此警告。 警告:updateX_xml():第25行的C:\\ xampp \\ htdocs \\ test_file_read \\ gen_url.php中未终止的实体引用lan = en

尝试在参数之间没有空格:

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

另外,正如其他人提到的那样,您需要转义“&”,因为它是使用htmlspecialchars()在xml中的特殊字符:

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

您必须转义HTML实体字符:

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

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

所以:

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