Jump to content

Query - PHP with XML - probably simple to solve


jug

Recommended Posts

Hi. I have got a xml file (xmlphoto.xml). I need to query the file to find out which row contains title="green", and then remove that row.

Im pretty sure i need the row index to remove the row. Im new to xml so any basic help would be much apprecaiated.

 

<gallery>

  <image title="blue">

  <image title="green">

  <image title="pink">

</gallery>

 

thanks in advance.

<php

$xmlobj = simplexml_load_file("filename.xml");

  if($xmlobj == false){

          $flag = false;

      echo $message = "Error: XML file cannot be loaded";

      }else{

 

  foreach($xmlobj as $obj){

  if($obj['title']=="green"){

  $obj['title']="";

  }

  }

  echo $xmlobj->asXML();

  }

?>

 

Use this code snippet

unfortunately

 

<?php
$str = '<gallery>
  <image title="blue">
  <image title="green">
  <image title="pink">
</gallery>';

$xmlobj = simplexml_load_string($str);
if($xmlobj == false){
       $flag = false;
        echo $message = "Error: XML file cannot be loaded";
  }else{

     foreach($xmlobj as $obj){
           if($obj['title']=="green"){
              $obj['title']="";
           }
     }
     echo $xmlobj->asXML();
}
?>

 

gives -->

Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 5: parser error : Opening and ending tag mismatch: image line 4 and gallery in C:\Inetpub\wwwroot\test\noname7.php on line 11

 

Warning: simplexml_load_string() [function.simplexml-load-string]: </gallery> in C:\Inetpub\wwwroot\test\noname7.php on line 11

 

Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in C:\Inetpub\wwwroot\test\noname7.php on line 11

 

Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 5: parser error : Premature end of data in tag image line 3 in C:\Inetpub\wwwroot\test\noname7.php on line 11

 

Warning: simplexml_load_string() [function.simplexml-load-string]: </gallery> in C:\Inetpub\wwwroot\test\noname7.php on line 11

 

Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in C:\Inetpub\wwwroot\test\noname7.php on line 11

 

Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 5: parser error : Premature end of data in tag image line 2 in C:\Inetpub\wwwroot\test\noname7.php on line 11

 

Warning: simplexml_load_string() [function.simplexml-load-string]: </gallery> in C:\Inetpub\wwwroot\test\noname7.php on line 11

 

Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in C:\Inetpub\wwwroot\test\noname7.php on line 11

 

Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 5: parser error : Premature end of data in tag gallery line 1 in C:\Inetpub\wwwroot\test\noname7.php on line 11

 

Warning: simplexml_load_string() [function.simplexml-load-string]: </gallery> in C:\Inetpub\wwwroot\test\noname7.php on line 11

 

Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in C:\Inetpub\wwwroot\test\noname7.php on line 11

Error: XML file cannot be loaded

unfortunately

 

<?php
$str = '<gallery>
  <image title="blue">
  <image title="green">
  <image title="pink">
</gallery>';

$xmlobj = simplexml_load_string($str);
if($xmlobj == false){
       $flag = false;
        echo $message = "Error: XML file cannot be loaded";
  }else{

     foreach($xmlobj as $obj){
           if($obj['title']=="green"){
              $obj['title']="";
           }
     }
     echo $xmlobj->asXML();
}
?>

 

gives -->

Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 5: parser error : Opening and ending tag mismatch: image line 4 and gallery in C:\Inetpub\wwwroot\test\noname7.php on line 11

 

Warning: simplexml_load_string() [function.simplexml-load-string]: </gallery> in C:\Inetpub\wwwroot\test\noname7.php on line 11

 

Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in C:\Inetpub\wwwroot\test\noname7.php on line 11

 

Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 5: parser error : Premature end of data in tag image line 3 in C:\Inetpub\wwwroot\test\noname7.php on line 11

 

Warning: simplexml_load_string() [function.simplexml-load-string]: </gallery> in C:\Inetpub\wwwroot\test\noname7.php on line 11

 

Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in C:\Inetpub\wwwroot\test\noname7.php on line 11

 

Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 5: parser error : Premature end of data in tag image line 2 in C:\Inetpub\wwwroot\test\noname7.php on line 11

 

Warning: simplexml_load_string() [function.simplexml-load-string]: </gallery> in C:\Inetpub\wwwroot\test\noname7.php on line 11

 

Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in C:\Inetpub\wwwroot\test\noname7.php on line 11

 

Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 5: parser error : Premature end of data in tag gallery line 1 in C:\Inetpub\wwwroot\test\noname7.php on line 11

 

Warning: simplexml_load_string() [function.simplexml-load-string]: </gallery> in C:\Inetpub\wwwroot\test\noname7.php on line 11

 

Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in C:\Inetpub\wwwroot\test\noname7.php on line 11

Error: XML file cannot be loaded

 

Use this as the XML is not well formed in above

 

<?php
$str = '<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<gallery>
  <image title="blue"/>
  <image title="green"/>
  <image title="pink"/>
</gallery>';

$xmlobj = simplexml_load_string($str);
if($xmlobj == false){
       $flag = false;
        echo $message = "Error: XML file cannot be loaded";
  }else{

     foreach($xmlobj as $obj){
           if($obj['title']=="green"){
              $obj['title']="";
           }
     }
     echo $xmlobj->asXML();
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.