簡體   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