How to create sub nodes in xml with the help of php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kushaljutta
Forum Commoner
Posts: 50
Joined: Fri Dec 26, 2008 11:05 am

How to create sub nodes in xml with the help of php

Post by kushaljutta »

<?php
$dom = new DomDocument();
$dom->load("listings.xml");
$agents = $dom->documentElement;
$id=$_REQUEST['listid'];
$listheading=$_REQUEST['listheading'];
$postcode=$_REQUEST['postcode'];
$suburb=$_REQUEST['suburb'];
$city=$_REQUEST['city'];
$state=$_REQUEST['state'];
$propertytype=$_REQUEST['propertytype'];


$agents = $dom->documentElement;
$newagent = $agents->appendChild(new domElement('list'));
$newagent->appendChild(new domElement('listid', $id));
$newagent->appendChild(new domElement('listheading', $listheading));
$newagent->appendChild(new domElement('postcode', $postcode));
$newagent->appendChild(new domElement('suburb', $suburb));
$newagent->appendChild(new domElement('city', $city));
$newagent->appendChild(new domElement('state', $state));
$newagent->appendChild(new domElement('propertytype', $propertytype));

$res= $dom->saveXML();

$default_dir="";
$default_dir .= "listings.xml";
$fp = fopen($default_dir,'w');
$write = fwrite($fp,$res);
$result="ok";

if($result=="ok")
{
?>
<script>
location.href="view.php";
</script>
<?php
}
?>

with the help of this file i can create
xml like following xml file

listings.xml
========
<?xml version="1.0"?>
<listings>
<list>
<listid>list001</listid>
<listheading>rajesh</listheading>
<postcode>500505</postcode>
<suburb>muncipal </suburb>
<city>tanuku</city>
<state>lasgdf</state>
<propertytype>asgdf</propertytype>
</list>

but i cant create sub nodes for <suburd> nod...

i want sub nodes in <suburb>...like following
<suburb>
<suburb1>xyz</suburb1>
<suburb2>xyz</suburb2>
</suburb>

totally i want following format


<?xml version="1.0"?>
<listings>
<list>
<listid>list001</listid>
<listheading>rajesh</listheading>
<postcode>500505</postcode>
<suburb>
<suburb1>xyz</suburb1>
<suburb2>xyz</suburb2>
</suburb>

<city>tanuku</city>
<state>lasgdf</state>
<propertytype>asgdf</propertytype>
</list>

anybody help me...

im in very bad situation for this problem.....

please...
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: How to create sub nodes in xml with the help of php

Post by Chris Corbyn »

Using SimpleXML it can be done like this:

Code: Select all

$doc = simplexml_load_file('listings.xml');
$doc->suburb->suburb1 = 'Something here';
$doc->suburb->suburb2 = 'other here';
 
echo $doc->asXML();
kushaljutta
Forum Commoner
Posts: 50
Joined: Fri Dec 26, 2008 11:05 am

Re: How to create sub nodes in xml with the help of php

Post by kushaljutta »

Thanks for ur reply...

can you give some example for this bcoz..

im new to xml...
please
Post Reply