Jump to content

Query - PHP with XML - probably simple to solve


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();
}
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.