Page 1 of 1

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

Posted: Sat Feb 07, 2009 8:07 am
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...

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

Posted: Sun Feb 08, 2009 6:21 am
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();

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

Posted: Sun Feb 08, 2009 10:32 am
by kushaljutta
Thanks for ur reply...

can you give some example for this bcoz..

im new to xml...
please