tags:

views:

2311

answers:

2

Hi friends,

I want to modify my xml file in PHP based on the following criteria.

my xml structure look like this:

<?xml version="1.0" encoding="ISO-8859-1" ?> 

<markers>
   <marker>
      <type></type>
      <title></title>
      <address></address>
      <latitude></latitude>
      <longitude></longitude>
   <marker>
   <marker>
      <type></type>
      <title></title>
      <address></address>
      <latitude></latitude>
      <longitude></longitude>
   <marker>
</markers>

Now every time when xml going to modify, with the type attribute.

means, on first time search string == "hotels" then data related to hotels will be stored in xml file as above format.

Now when again search for hotels is done then it will remove the elements which have child element with value hotels.

and search for different query for e.g. schools is done at that time data related to the schools are appended to the xml file. with the data of the hotels.

now again search for schools is done then it will remove the element related to schools.

Thanks in advance.

+1  A: 

Document Object Model (DOM) is the answer.

<?
 $dom=new DomDocument();
 $dom->Load("file.xml");
 $root=$dom->documentElement;
 ...
 $dom->Save("file.xml");
?>
adatapost
but how could i loop through the xml file elments..?
Avinash
By reading the documentation: http://de.php.net/manual/en/class.domelement.php
Bombe
The purpose of StackOverflow, in my opinion, is to help users by giving them examples that may help them in resolving their issue. Not to imply that they haven't read or searched in the documentation.
Christian Toma
+1  A: 

You can use the DOMDocument from PHP.

You load your file and than loop trough the childNodes of the document.

<?php
$dom=new DOMDocument();
$dom->load("file.xml");

$root=$dom->documentElement; // This can differ (I am not sure, it can be only documentElement or documentElement->firstChild or only firstChild)

$nodesToDelete=array();

$markers=$root->getElementsByTagName('marker');

// Loop trough childNodes
foreach ($markers as $marker) {
 $type=$marker->getElementsByTagName('type')->item(0)->textContent;
 $title=$marker->getElementsByTagName('title')->item(0)->textContent;
 $address=$marker->getElementsByTagName('address')->item(0)->textContent;
 $latitude=$marker->getElementsByTagName('latitude')->item(0)->textContent;
 $longitude=$marker->getElementsByTagName('longitude')->item(0)->textContent;

 // Your filters here

 // To remove the marker you just add it to a list of nodes to delete
 $nodesToDelete[]=$marker;
}

// You delete the nodes
foreach ($nodesToDelete as $node) $node->parentNode->removeChild($node);

echo $dom->saveXML();
?>

You can save your output XML like this

$dom->saveXML(); // This will return the XML as a string
$dom->save('file.xml'); // This saves the XML to a file

To do this parsing in JavaScript you should use jQuery (a small, but powerful library).

You can include the library directly from Google Code Repository.

<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"&gt;&lt;/script&gt;

The library is cross-browser and very small. It should be cached in many cases, because some sites use it from Google Code

$(yourXMLStringOrDocument).find("marker").each(function () {
     var marker=$(this);

     var type=marker.find('type').text();
     var title=marker.find('title').text();
     var address=marker.find('address').text();
     var latitude=marker.find('latitude').text();
     var longitude=marker.find('longitude').text();
});
Christian Toma
yes seems good, let me chk in my code
Avinash
how do i remove all marker node from my xml file?
Avinash
You add all $marker's from within the foreach to the $nodesToDelete array.
Christian Toma
Yes i have done but not working $root=$dom->documentElement->firstChildFor this line i have used all option given by you but no one is working. pl send me proper format for all three option.
Avinash
I changed the code, this should work now. $root wasn't the problem but the firstChild in it was of DOMText type and we wanted only the DOMElements with the tag 'marker'. If you have any other problems I am open to discussion.
Christian Toma
Thanks Christian, $root=$dom->documentElement;is works for me, thanks a lot
Avinash
hi Christiani have bit of problem with reading the xml file in javascript.My xml file format is as above.Please suggest me the right way.i need that its loop through all marker element and give the value of each child element... thanks
Avinash
Sorry for my late answer. I will edit my answer with the right way to do this in JavaScript. The best way to do this in JavaScript is by using a library (like jQuery).
Christian Toma
Thanks a lot i have done my job ,Thank you so much.
Avinash
You're welcome Avinash.
Christian Toma
hi Christian,I have bit of confusion for creating xml file,i have to create separate xml file for each user which are using my web page.how can i do this.is there any option to create xml file on the user's machine?It will be easy to map the file in all javascript function..
Avinash
When you create it in JavaScript it creates it on the users machine.If you create it in PHP you can use sessions to check if this is a known user or a new one.
Christian Toma