soycharliente Posted February 1, 2013 Share Posted February 1, 2013 (edited) I am working on keeping track of page views. Unfortunately, I can't do this with a database. I need to keep track of this in an XML file. The XML file contents are only available through web services, which I read and edit using a Soap Client. In the end, each page on teh website will have this block of code. The block of code automatically updates itself to contain a unique ID for each page. So when the code runs, each page will run it using a different ID. If check if the page already exists in the XML file and process either updating the number of views or adding it to the XML. The later works just fine. I cannot seem to update the XML structure and pass it back. Any help is very much welcome. XML structure: <pages> <page> <id>o8427fho84uhfir</id> <views>1</views> </page> </pages> Relevant code: <?php define('PAGE_ID', 'UNIQUE_ID'); $readResponse = $client->read($readRequest); if ( $readResponse->readReturn->success == 'true' ) { $asset = (array) $readResponse->readReturn->asset->xmlBlock; $xml = new SimpleXMLElement($asset['xml']); // Check if current page already exists $page = $xml->xpath("page[id='".PAGE_ID."']"); if ( $page ) { // Cannot get this part to work $page[1] += 1; $xml->asXml(); } else { // This section works well $page = $xml->addChild('page'); $page->addChild('id',PAGE_ID); $page->addChild('views',1); $asset['xml'] = $xml->asXML(); } // Edit block $editResponse = $client->edit($editRequest); } ?> Edited February 1, 2013 by charlieholder Quote Link to comment https://forums.phpfreaks.com/topic/273911-edit-simplexml-node/ Share on other sites More sharing options...
salathe Posted February 1, 2013 Share Posted February 1, 2013 (edited) $page[1] += 1; Here, $page is an array containing some SimpleXMLElement objects (each <page> element with the chosen <id>). You then try to update the second object in the array (it starts counting up from zero), is that really what you wanted? I only ask because there is otherwise no hint that there might be more than one <page> with a given <id>. If there is only supposed to be one matching element, then you'll want to use [0] there instead. Now onto the real problem: you cannot directly modify a SimpleXMLElement object as you are trying to do. Modification only occurs using the property-style or array-style syntax: $element->… = 'something' or $element[…] = 'something'. Usually this is for modifying child elements or the element's attributes, respectively. However there is a trick to modify the element itself. SimpleXMLElement objects are also hidden collections of elements that can be referenced by number using the array-style syntax. Since the "collection" only has one element, then you can use [0]. This means changing the above line to become: $page[1][0] += 1; // or maybe $page[0][0] Disclaimer: This is a strange quirk of SimpleXML and don't be surprised if you look back at this code at some point (later today, tomorrow or in a year) and make this face... P.S. The DOM extension is often a nice alternative to SimpleXML's confusing magic. Edited February 1, 2013 by salathe Quote Link to comment https://forums.phpfreaks.com/topic/273911-edit-simplexml-node/#findComment-1409559 Share on other sites More sharing options...
soycharliente Posted February 1, 2013 Author Share Posted February 1, 2013 is that really what you wanted? Yes, the XML structure is *supposed* to have unique pages. Further testing will determine if multiple pages will get created with the same ID. P.S. The DOM extension is often a nice alternative to SimpleXML's confusing magic. Are you talking about this? I will look into this. However there is a trick to modify the element itself. Ok cool. Thanks for the direction. I'll hit this up this weekend and report back. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/273911-edit-simplexml-node/#findComment-1409570 Share on other sites More sharing options...
soycharliente Posted February 5, 2013 Author Share Posted February 5, 2013 (edited) Thanks again salathe. Here's the final code that I got working: <?php define('PAGE_ID', 'UNIQUE_ID'); $readResponse = $client->read($readRequest); if ( $readResponse->readReturn->success == 'true' ) { $asset = (array) $readResponse->readReturn->asset->xmlBlock; $xml = new SimpleXMLElement($asset['xml']); $page = $xml->xpath("page[id='".PAGE_ID."']"); if ( $page ) { $page[0]->views += 1; // This is the line } else { $page = $xml->addChild('page'); $page->addChild('id',PAGE_ID); $page->addChild('views',1); } $asset['xml'] = $xml->asXML(); $editResponse = $client->edit($editRequest); } ?> Edited February 5, 2013 by charlieholder Quote Link to comment https://forums.phpfreaks.com/topic/273911-edit-simplexml-node/#findComment-1410322 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.