0

I have the following xml file test.xml:

<a>
  <b c="test1" d="test2"/>
  <b c="test3" d="test4"/>
</a>

This xml file can have 1 or more b elements. I would like to ask how could I change the value of item c in a specific b using php.

Thanks in advance.

  • 1
    possible duplicate of [edit XML with simpleXML](http://stackoverflow.com/questions/2092172/edit-xml-with-simplexml) – Pekka Apr 11 '11 at 08:16
  • possible duplicate of [A simple program to CRUD node and node values of xml file](http://stackoverflow.com/questions/4906073/a-simple-program-to-crud-node-and-node-values-of-xml-file) – Gordon Apr 11 '11 at 09:14
  • at this point I would like to comment that this question is related to attributes editing of an element and not the value of the element itself – Dimosthenis Kontogiorgos Apr 11 '11 at 10:25

2 Answers2

0

Using: http://php.net/manual/en/book.dom.php

Sample (all c-attributes):

<?php
$dom = new DomDocument;
$dom->load('sample.xml');

$xpath = new DomXPath($dom);

$cAttribs = $xpath->query('//*[@c]');
foreach ($cAttribs as $entry) {
    $entry->setAttribute('c', 'newValue');
}

header('Content-Type: text/xml; charset="UTF-8"');
echo $dom->saveXml();

Only specific nodes:

$dom = new DomDocument;
$dom->loadXml('<?xml version="1.0" encoding="UTF-8" ?>
<a>
  <b c="test1" d="test2" id="special-case" />
  <b c="test3" d="test4" />
</a>');

$xpath = new DomXPath($dom);

$cAttribs = $xpath->query('//*[@id="special-case"]');
foreach ($cAttribs as $entry) {
    $entry->setAttribute('c', 'newValue');
}

header('Content-Type: text/xml; charset="UTF-8"');
echo $dom->saveXml();
Yoshi
  • 54,081
  • 14
  • 89
  • 103
  • Thanks for your reply. I have already been looking the examples on php.net. However this code above changes the value of all c items. For example test1 and test3 become newValue. But, I would like to change only one of them. So far I have managed to change all of the elements at the same time or just the first. But I did not find a way to change a specific – Dimosthenis Kontogiorgos Apr 11 '11 at 08:45
  • To change only specific nodes, you'll need some sort of identification for that node. After that, you'll need to change the xpath expression to only select this node. I'll change the example to reflect that. – Yoshi Apr 11 '11 at 08:48
  • This worked good, not as I expected, but using an ID might be easier to handle the items. Thanks – Dimosthenis Kontogiorgos Apr 11 '11 at 10:27
0

make a ".htaccess" file which is something that controls pages server-side....

add the following line in your page:

AddType application/x-httpd-php .xml

this will make ANY xml document into PHP but if you don't want php in your xml, it's still ok since php is flexible and the page can work without actually having to write php in it.

Now use some str replace to get what you want working...

<?php

$xml = "<a>
  <b c="test1" d="test2"/>
  <b c="test3" d="test4"/>
</a>";

$find = array("test1","test2","test3","test4");

$xml = str_replace($find, "stuff", $xml);

echo $xml;

// output:
// <a>
//   <b c="stuff" d="stuff"/>
//   <b c="stuff" d="stuff"/>
// </a>

?>

I hope this is what you were looking for and I hope it helps...

Ninjiangstar
  • 225
  • 1
  • 3
  • 12