Re: php5
| From: | Vivian Steller | Date: | Tue, 02 Mar 2004 17:06:29 +0000 |
| Subject: | Re: php5 | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-179274@lists.php.net to get a copy of this message | ||
Carlos wrote:
> hello ... I'm working with simplexml (php5) to manipulate the data in
> xml file, but I'm having one problem.
i'm not familiar with simplexml but with the dom functions and i think (as
simplexml is based on dom) the simplexml objects will work with dom objects
as well?!
otherwise you'll perhaps get an idea how to work with extended DOM
functions :)
See below:
put your xml into a variable $xml..
> <?xml version="1.0"?>
leaving the first line out...
so $xml="
> <config>
> <network defaultroute="" dnsserver="">
> <interface name="lan" type="ethernet" device="eth0"
> ip="192.168.1.1" subnet="24"/>
> <interface name="wan" type="ethernet" device="eth1"
> ip="192.168.1.1" subnet="24"/>
> <alias number="1" desc="teste" device="eth0"/>
> <alias number="2" desc="teste1" device="eth1"/>
> </network>
> </config>
";
<?php
$doc = new DomDocument();
$doc->loadXML($xml); // this will parse your code into dom
// xpath will help to get the right nodes
$xpath = new DomXPath($doc);
$networkNode = $xpath->query("/config/network")->item(0);
?>
> I need manipulate the nodes (add and remove) .
> example
> I need add more one node
> <interface name="dmz" type="ethernet" device="eth3"
> ip="xxxxxxx"
> subnet="24"/>
<?php
// create a new node
$newInterfaceNode = $doc->createElement("interface");
// append this node under network
$newInterfaceNode = $networkNode->appendChild($newInterfaceNode);
?>
> and remove the node
>
> <alias number="2" desc="teste1" device="eth1"/>
<?php
// xpath again
$aliasNode = $xpath->query("//alias[@number = '2']")->item(0);
// remove the node (don't no exactly if this works...)
$aliasNode->parentNode->removeChild($aliasNode);
?>
> but I don't have idea ...
> somebody can help me ?
> Any example ...
>
> greats
>
> Carlos
have fun :)
regards,
vivi