rami
rami

Reputation:

Inserting URL Values into XML Page

I trying to write a php page that will take url values and insert them into an xml file. Any sample files would be of great help.

Upvotes: 0

Views: 519

Answers (1)

Johan van Leur
Johan van Leur

Reputation: 21

You could do something like the following:

<?php

// get parameters
$parameters = explode ( "&", $_SERVER ['QUERY_STRING'] );

// create new dom document
$doc = new DOMDocument();

// create new root node
$root = $doc->appendChild ( $doc->createELement ( "querystring", "" ));

// iterate all parameters
foreach ( $parameters as $parameter ) {
    // get keypair from parameter
    $keypair = explode ( "=", $parameter );
    // check if we have a key and a value
    if ( count ( $keypair ) == 2 ) {    
        // add new node to xml data
        $root->appendChild ( $doc->createElement( $keypair[0],$keypair[1] ) );
    }   
}

// save XML to variable
$xml = $doc->saveXML();

// echo or output to file...
echo $xml;  

?>

It will take the parameters (key/value pairs) of an URL and adds it to a new XML document. After the saveXML you can do whatever you want with the XML data.

Hope that helps.

Johan.

Upvotes: 1

Related Questions