-3

I'm using SimpleXML for parsing XMl documents. I need to be able to read/update node attributes.

In this XML document

<root>
  <node>ABC</node>
  <key>123</key>
  <node2>
     <key>456</key>
  </node2>
  <key>789</key>
</root>

How can I read/update all key nodes? the document doesn't have a specific structure, so I need to be able to find them without knowing their position. Let's say I want to multiply by 2 the numbers in the key nodes. How can I do it?

Tks.

oscarm
  • 2,630
  • 6
  • 41
  • 74
  • 2
    Is your name "fast-dev" because you have all your code written on Stack Overflow? ;) – Pekka Nov 26 '10 at 22:19
  • DIY: http://php.net/manual/en/simplexmlelement.xpath.php – Alin Purcaru Nov 26 '10 at 22:26
  • 4
    @Orbling asking a lot of questions is OK, it's just better if they are specific questions and not write-me-a-bunch-of-code ones – cambraca Nov 26 '10 at 22:29
  • 1
    @cambraca Yes, I'm waiting for someone to say "I've got a website to build, here's the spec, 15 reputation available for first to complete". I know the job market is bad... – Orbling Nov 26 '10 at 22:31
  • wow, never thought you would encourage users to not use the site toooo much... good to know. – oscarm Nov 26 '10 at 23:01
  • 2
    @fast-dev it's not so much the frequency of your question than it is the quality. Your question is valid (which is why I didnt dv it), but SO asks you to "Do your Homework" before asking questions. Finding out how to do basic things like changing attributes with SimpleXml can be found easily (unlike the solution to your other question; the one you deleted) – Gordon Nov 26 '10 at 23:31

3 Answers3

8

I find the question extremely lazy, but anyway

$sxe = new SimpleXmlElement($xml);
foreach($sxe->xpath('//key') as $key) $key[0] *= 2;
echo $sxe->asXML();

There is plenty of questions about SimpleXml on Stack Overflow. Please search for them before asking your question. The PHP Manual also has examples covering Basic Usage.

Community
  • 1
  • 1
Gordon
  • 312,688
  • 75
  • 539
  • 559
1
<?php

$xml = '<root>
  <node>ABC</node>
  <key>123</key>
  <node2>
     <key>456</key>
  </node2>
  <key>789</key>
</root>';

$dom = new DOMDocument();
$dom->loadXml($xml);

$xpath = new DOMXPath($dom);
foreach ($xpath->query('//text()') as $node)
{
 $newNode = $dom->createDocumentFragment();
 $newNode->appendXML($node->wholeText . ' replaced');
 $node->parentNode->replaceChild($newNode, $node);
}

echo $dom->saveXML();
István Ujj-Mészáros
  • 3,228
  • 1
  • 27
  • 46
  • Hmmm, but this is not using SimpleXml as requested (though I like DOM better too) and it is also not multiplying the key element values by two and for adding the string 'replaced' to the existing wholeText, you could simply use `$node->appendData(' replaced');` instead of replacing the entire node with a fragment. But basically, you dont even need to query the text nodes at all, but just the key elements and change their nodeValue. – Gordon Nov 27 '10 at 00:32
  • I know, I know. That's why I added the details. But the main reason I am pointing it out is because the OP accepted when it seems to be something completely different than was asked. But that's StackOverflow :) – Gordon Nov 27 '10 at 19:18
1

The simplest possible API is QueryPath (or phpQuery):

$qp = qp($xml);

foreach ($qp->find("key") as $key) {
     //@todo: add verification that it's indeed numeric text
     $key->text(  $key->text() * 2  );
}

But something similar is possible with "SimpleXML"

mario
  • 144,265
  • 20
  • 237
  • 291