Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to modify xml file using PHP

Tags:

php

xml

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.

like image 908
Avinash Avatar asked Jul 28 '09 11:07

Avinash


People also ask

How to update XML file in PHP?

php $xml = new DOMDocument('1.0', 'utf-8'); $xml->formatOutput = true; $xml->preserveWhiteSpace = false; $xml->load('examples.

Can I use PHP in XML?

If you run the XML file as a PHP script, then yes.


3 Answers

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"></script>

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();
});
like image 100
Cristian Toma Avatar answered Sep 23 '22 02:09

Cristian Toma


Document Object Model (DOM) is the answer.

<?
 $dom=new DomDocument();
 $dom->Load("file.xml");
 $root=$dom->documentElement;
 ...
 $dom->Save("file.xml");
?>
like image 45
KV Prajapati Avatar answered Sep 20 '22 02:09

KV Prajapati


<?php
$xml = new DOMDocument('1.0','UTF-8');
$xml->preserveWhiteSpace = FALSE;
            
$xml->load("markers.xml");
        
$mar = $xml->getElementsByTagName("marker");
foreach($mar as $row){


            $type = $row->getElementsByTagName('type')->item(0);
            $title = $row->getElementsByTagName('title')->item(0);
            $address = $row->getElementsByTagName('address')->item(0);
            $latitude = $row->getElementsByTagName('latitude')->item(0);
            $longitude = $row->getElementsByTagName('longitude')->item(0);
            
            $row->replaceChild($type, $type);
            $row->replaceChild($title, $title);
            $row->replaceChild($address, $address);
            $row->replaceChild($latitude, $latitude);
            $row->replaceChild($longitude, $longitude);
          
            ?>
            
<form id="q_form" method="post" action="">
    <div class="form-group form-group-lg">
        <div class="table-responsive">
            <table id="choices_table"  class="table table-bordered"  >
                <tr>
                    <td>
                        <div class="form-group">
                            <input type="type"
                                    name ="title" 
                                    value="<?php echo $type->nodeValue ?>"
                                    />
                        </div>
                    </td>
                    <td>
                    <div class="form-group">
                        <input type="text"
                                name ="title" 
                                value="<?php echo $title->nodeValue ?>"
                                />
                     </div>
                    </td>
                    <td>
                    <div class="form-group">
                        <input type="text"
                                name ="address" 
                                value="<?php echo $address->nodeValue ?>"
                                />
                     </div>
                    </td>
                    <td>
                    <div class="form-group">
                        <input type="latitude"
                                name ="latitude" 
                                value="<?php echo $latitude->nodeValue ?>"
                                />
                     </div>
                    </td>
                    <td>
                    <div class="form-group">
                        <input type="latitude"
                                name ="longitude" 
                                value="<?php echo $longitude->nodeValue ?>"
                                />
                     </div>
                    </td>
        </table>
        </div>  
    
</div>
    <input type="submit"  name="submit" value=" EDIT "  class="btn btn-primary" />
</form>  
    <?php



    if (isset($_POST['submit']))
    {
    $type->nodeValue = $_POST['type'];    
    $text->nodeValue = $_POST['text'];
    $address->nodeValue = $_POST['address'];
    $latitude->nodeValue = $_POST['latitude'];
    $longitude->nodeValue = $_POST['longitude'];
    $xml->save("markers.xml");
        }
    
}

?>
like image 41
Nour Hoda Avatar answered Sep 20 '22 02:09

Nour Hoda