php - How to set a node of an XML file? -
php - How to set a node of an XML file? -
i've created xml document.
so, now, want find node , set values of node, after research topic, don't know how it.
this document :
<?xml version="1.0" encoding="utf-8"?> <scripts> <script nom="mytools.class.php"> <titre>useful php classes</titre> <date>18/07/2011</date> <options> <option name="topic">tutorials</option> <option name="desc">tutorial you</option> </options> </script> <script nom="index.php"> <titre>blabla</titre> <date>15/07/2011</date> <options> <option name="topic">the homepage</option> </options> </script> </scripts >
so, build html form theses values, @ moment, can't , set want :(
i want first "script" node :
<script nom="mytools.class.php"> //how set "nom" attribute ? <titre>useful php classes</titre> //how value , set ? <date>18/07/2011</date> <options> <option name="topic">tutorials</option> <option name="desc">tutorial you</option> </options> </script> i have no problem loop document, not "own choices"
have thought ?
use xpath first dom document
$dom=new domdocument(); $dom->loadxml('file'); // file name of xml file if u have string of xml called $string utilize $dom->loadxml($string) $xpath=new domxpath($dom); $path='//scripts/script[1]'; // first node $elem=$xpath->query($path); now $elem[0] first script node
if u want elements attribute utilize $path='//scripts/script[@nom='attribute value']'; using path homecoming nodeset script elements having nom attribute of ur given value can see more here
in response bahamut100's comment xpath fot alternative element //options/option
now if u meant getting alternative node attribute value this
$path='//options/option[@attrib_name=attrib_value]'; $elem=$xpath->query($path); but if u meant getting attributes of node first u have reach node. in ur case u have reach alternative node first
$path='//options/option'; $option=$xpath->query($path); now $option node list getting first element's attibutes use
$attribute=$option[0]->attributes; now $attribute namednodemap getting value of first attribute use
$value=$attribute->item(0); php xml parsing dom
Comments
Post a Comment