简体   繁体   中英

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.

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

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() :

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

You have to escape HTML entity character:

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

If you are inserting something into XML/HTML you should always use the htmlspecialchars function. this will escape your strings into correct XML syntax.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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