PHP Updating XML Node Value



I have the following section in my XML file;



<CstmrDrctDbtInitn>
<GrpHdr>
<MsgID>FKichbSczD</MsgID>
<CreDtTm>2014-07-08T12:10:00</CreDtTm>
<NbOfTxs>1</NbOfTxs>
<CtrlSum>400.4</CtrlSum>
<InitgPty>
<Id>
<PrvtId>
<Othr>
<Id>IE40ZZZ303504</Id>
</Othr>
</PrvtId>
</Id>
</InitgPty>
</GrpHdr>
</CstmrDrctDbtInitn>


How would I go about changing the value of <NbOfTxs>?


I tried the following code attempt;



function CountTransactions($xml_file) {
$filename = date('Y-W').'.xml'; //2014-26.xml
$xml = new DOMDocument;
$xml->load($filename);
$xpath = new DOMXPath($xml);

$Transaction = $xml->getElementsByTagName('DrctDbtTxInf');
$NumberTransactions = 0;
foreach ($Transaction as $Payment) {
$NumberTransactions = $NumberTransactions + 1;
}

// add more elements to xml file
$CustomerDDInfo = $xml->getElementsByTagName('CstmrDrctDbtInitn')->item(0);
// add this element to the root
$xml->documentElement->appendChild($CustomerDDInfo);
// add more elements to xml file
$GroupHeader = $xml->getElementsByTagName('GrpHdr')->item(0);
// add this element to the root
$CustomerDDInfo->appendChild($GroupHeader);

$query = $xpath->query('NbOfTxs',$GroupHeader);
$query->item(0)->nodeValue = $NumberTransactions;
$xml->save($xml_file);
}


I am trying to count the number of transactions in the XML file each time it is updated. A single transaction is identified between the <DrctDbtTxInf> and </DrctDbtTxInf> tags.


No comments:

Post a Comment