1

I am trying to change a value in an xml file using php. I am loading the xml file using php into an object like this..

if(file_exists('../XML/example.xml')) {
            $example = simplexml_load_file('../XML/example.xml');
        }
else {
exit ("can't load the file");
}

Then once it is loaded I am changing values within tags, by assigning them the contents of another variable, like this...

$example->first_section->second_section->third_section->title = $var['data'];

Then once I've made the necessary changes the file is saved. So far this process is working well, but have now hit a stumbling block.

I want to change a value within a particular tag in my xml file, which has an id. In the XML file it looks like this.

<first_section>
     <second_section>
          <third_section id="2">
               <title>Mrs</title>
          </third_section>
     </second_section>
 </first_section>

How can I change this value using similar syntax to what I've been using?

doing..

$example->first_section->second_section->third_section id="2" ->title = $var['data']

doesn't work as the syntax is wrong.

I've been scanning through stack overflow, and all over the net for an example of doing it this way but come up empty.

Is it possible to target and change a value in an xml like this, or do I need to change the way I am amending this file?

Thanks.

coca_coder
  • 13
  • 5
  • I don't know how to use xpath very well. How would I use it in this instance to edit the tag value? – coca_coder Mar 01 '16 at 14:11
  • Read the url i sent in the answer its very easy – Muhammed Mar 01 '16 at 14:12
  • Possible duplicate of [SimpleXML: Selecting Elements Which Have A Certain Attribute Value](http://stackoverflow.com/questions/992450/simplexml-selecting-elements-which-have-a-certain-attribute-value) – michi Mar 01 '16 at 15:54

2 Answers2

1

Some dummy code as your provided XML is surely not the original one.

$xml = simplexml_load_file('../XML/example.xml');
$section = $xml->xpath("//third_section[@id='2']")[0];
// runs a query on the xml tree
// gives always back an array, so pick the first one directly
$section["id"] = "3";

// check if it has indeed changed
echo $xml->asXML();

As @Muhammed M. already said, check the SimpleXML documentation for more information. Check the corresponding demo on ideone.com.

Jan
  • 42,290
  • 8
  • 54
  • 79
  • The documentation doesn't provide an example covering exactly what i am trying to do. I had looked there before coming to stack overflow. – coca_coder Mar 01 '16 at 15:16
  • I am trying this. $section = $xml->xpath("//third_section[@id='2']"); foreach ( $section[0]->attributes() as $key => $val ) { $val->street = "data"; } – coca_coder Mar 01 '16 at 15:16
  • $section = $xml->xpath("//third_section[@id='2']")[0]; This is incorrect syntax. Removing the [0] from the end of the line makes it legal, however i am still unable to target the tag this way. – coca_coder Mar 01 '16 at 15:18
  • @coca_coder: This is not an incorrect syntax, check the demo provided in my answer (at the bottom). The call to `xpath()` gives back an array. – Jan Mar 01 '16 at 15:28
0

Figured it our after much messing around. Thanks to your contributions I indeed needed to use Xpath. However the reason it wasn't working for me was because I wasn't specifying the entire path for the node I wanted to edit.

For example, after loading the xml file into an object ($xml):

    foreach($xml->xpath("/first_section/second_section/third_section[@id='2']") as $entry ) {   
        $entry->title = "mr"; 
    }

This will work, because the whole path to the node is included in the parenthesis. But in our above examples eg:

        foreach($xml->xpath("//third_section[@id='2']" as $entry ) {    
        $entry->title = "mr";

        }

This wouldn't work, even though it was my understanding that the double // will make it drill down, and I assumed that xpath will search the whole xml structure and return where id=2. It appears after spending hours testing this isn't the case. You must include the entire path to the node. As soon as I did that it worked. Also on a side note. $section = $xml->xpath("//third_section[@id='2']")[0]; IS incorrect syntax. You don't need to specify the index "[0]" at the end. Including it flags up Dreamweavers syntax checker. And ignoring Dreamweaver and uploading anyway breaks the code. All you need is.. $section = $xml->xpath(" entire path to node in here [@id='2']");

Thanks for helping and suggesting xpath. It works very well... once you know how to use it.

coca_coder
  • 13
  • 5