Jump to content

Domdocument : Select Element By Unique Id


mayid
 Share

Recommended Posts

Hi. I´m learning about xml to feed a photo gallery. I´m able to read the file by simple_xml. But now i´m trying to set new values by users interface.I think the best way to achieve taht is using PHP DomDocument . But how? Documentation is really hard to find and to understand.Here my xml structure: <galeria><photo id='a2.jpg'><titulo>Heres the title</titulo><descripcion>This picture´s about dada</descripcion></photo><photo id='Henry-Rousseau.jpg'><titulo></titulo><descripcion></descripcion></photo></galeria>And i should be able to select the photo by id, and modify the title of it.I´ll appreciate your comments. Regards. G.

Link to comment
Share on other sites

You have to loop through every <photo> element, and check the id attribute of each one.

foreach($xmlDoc->getElementsByTagName('photo') as $element) {  if($element->getAttribute('id') == 'a2.jpg') {	// Do something here  }}

Link to comment
Share on other sites

Thanks! I´m trying not to use this method, because i can´t imagine how to "Do something here" for childs nodes.Now i can access child node i wanted by xpath (using id, and so). This way:$xpath = new DOMXPath($xml);$set = $xpath->query("//*[@id = '".$foto."']");$result = $set->item(0)->getElementsByTagName($seccion)->item(0)->nodeValueAnd i´m getting results. But how to define a new content to the node?

Link to comment
Share on other sites

Instead of storing the result in $result, assign a new value at that point, like:

$set->item(0)->getElementsByTagName($seccion)->item(0)->nodeValue = "New value";

Note that instead of using XPath or checking each node, you could also use getElementById(), but only after you explicitly specify the "id" attribute as being the ID attribute, like:

$xml = new DOMDocument;$xml->load('galeria.xml');$xml->setIdAttribute('id', true);$a2 = $xml->getElementById('a2.jpg');

Link to comment
Share on other sites

Instead of storing the result ...
This seems to be key.And thanks for the id setting tip.I´m working into a funtion, that returns the xml perfectly, with the new contents. But they are not actually being saved.$xml->formatOutput = true;// $xml->saveXML();return $xml->saveXML();
Link to comment
Share on other sites

saveXML() only gives you the resulting XML. Nothing more, nothing less.To actually save the file you need to use save() instead of saveXML(), or write the results from saveXML() with file_put_contents(). There is a slight performance difference between these two options, but which one you chose should depend on your full scenario. In short - if you want to only save the file, save() is better. If you want to display AND save the file, saveXML() + file_put_contents() is better than having calls to both save() and saveXML().

Edited by boen_robot
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...