Jump to content

Recommended Posts

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 by charlieholder
Link to comment
https://forums.phpfreaks.com/topic/273911-edit-simplexml-node/
Share on other sites

$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... :blink:

 

P.S. The DOM extension is often a nice alternative to SimpleXML's confusing magic.

Edited by salathe
Link to comment
https://forums.phpfreaks.com/topic/273911-edit-simplexml-node/#findComment-1409559
Share on other sites

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!

Link to comment
https://forums.phpfreaks.com/topic/273911-edit-simplexml-node/#findComment-1409570
Share on other sites

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 by charlieholder
Link to comment
https://forums.phpfreaks.com/topic/273911-edit-simplexml-node/#findComment-1410322
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.