Board index   FAQ   Search  
Register  Login
Board index PHP PHP Scripts

Check for existing child in XML file

Links for php scripts

Moderators: macek, egami, gesf

Check for existing child in XML file

Postby DavidFaiz » Mon Feb 10, 2014 12:27 pm

Hello,

I am very new to PHP and searched online and couldn't find a solution.
I have a XML file that looks like this:

Code: Select all
   <Marker>
      <title>Richard-Wolf</title>
      <site_location>Knittlingen, Germany</site_location>
      <status>Normal</status>
          <longitude>686868686</longitude>
               <latitude>686868686</latitude>
              </Marker>


I would like to check if the longitude element doesn't exist.
Tried using "getElementsByTagName" but was unsuccessful.

Here is my code:

Code: Select all
<?php
$xml=simplexml_load_file("C:\Users\dfaiz\Desktop\Google project\Customers222.xml");
$sxe = new SimpleXMLElement($xml->asXML());

foreach($sxe->children() as $child)
  {
   if($child->?) // do not what condition to use here
   {
      $child->addChild("longitude", "686868686");
      $child->addChild("latitude", "686868686");
   }
  }
   $sxe->saveXML("C:\Users\dfaiz\Desktop\Google project\Customers222.xml");
?>


What condition should I use ?

Thanks in advance.
DavidFaiz
New php-forum User
New php-forum User
 
Posts: 1
Joined: Mon Feb 10, 2014 12:21 pm

Re: Check for existing child in XML file

Postby fanlongtao » Mon Feb 17, 2014 4:27 am

<?php
$xml=simplexml_load_file("Customers222.xml");
$sxe = new SimpleXMLElement($xml->asXML());

if(!$sxe->longitude){
$sxe->addChild("longitude", "686868686");
}
$sxe->saveXML("Customers222.xml");
fanlongtao
New php-forum User
New php-forum User
 
Posts: 2
Joined: Fri Feb 14, 2014 1:00 am


Return to PHP Scripts

Who is online

Users browsing this forum: No registered users and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.