[ create a new paste ] login | about

Link: http://codepad.org/xOJG4nHW    [ raw code | output | fork ]

PHP, pasted on Oct 9:
<?php

$xml = new DOMDocument("1.0", "ISO-8859-1");
$xml->formatOutput = true;
$xml->preserveWhiteSpace = false;
$xml->loadXML('<?xml version="1.0"?>
<friends>
  <friend id="1">
    <name>MyTest</name>
    <games>5</games>
    <wins>3</wins>
  </friend>
</friends>');

echo "<xmp>OLD:\n". $xml->saveXML() ."</xmp>";

$xpath = new DOMXPath($xml);

$result = $xpath->query('//friend[@id="1"]/games');
$result2 = $xpath->query('//friend[@id="1"]/wins');

if($result){
    $cgames = $result->item(0)->nodeValue;
    $result->item(0)->nodeValue = $cgames+1;
}

if($result2){
    $cwins = $result2->item(0)->nodeValue;
    $result2->item(0)->nodeValue = $cwins+1;
}

echo "

<xmp>NEW:\n". $xml->saveXML() ."</xmp>";


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<xmp>OLD:
<?xml version="1.0"?>
<friends>
  <friend id="1">
    <name>MyTest</name>
    <games>5</games>
    <wins>3</wins>
  </friend>
</friends>
</xmp>

<xmp>NEW:
<?xml version="1.0"?>
<friends>
  <friend id="1">
    <name>MyTest</name>
    <games>6</games>
    <wins>4</wins>
  </friend>
</friends>
</xmp>


Create a new paste based on this one


Comments: