Solved

read and writing XML file

Posted on 2008-10-23
10
214 Views
Last Modified: 2013-11-05
I have an XML file containing browser stats:

<?xml version="1.0" encoding="iso-8859-1"?>
<browsers>
      <browser name="Firefox" version="3.0.3" platform="Windows" users="314"/>
      <browser name="MSIE" version="7.0" platform="Windows" users="229" />
      <browser name="Firefox" version="3.0.1" platform="Windows" users="112" />
      <browser name="MSIE" version="6.0" platform="Windows" users="75" />
</browsers>

Basically what i'm trying to do is increment the "users" attribute value in each <browser>.  

So when a user visits the site, I get their browser name, version and platform.  I then search the XML file above to see if the users browser name, version & platform already exist?

If it does, increament the "users" attribute, otherwise create a new <browser> node in the XML file and write the users browser name, version and platform to the "name", "version" and "platform" attributes...

So far I can parse the XML file and find info using test data.  However I cannot increment the "users" attribute if browser name, version & platform already exist or create a new node if it doesnt exist.  

Please advise.  See PHP below.
<?php

// dummy test data

$name = 'Opera';

$version = '9.20';

$platform = 'Windows';
 

// create new DOM document object

$doc = new DOMDocument;

$doc->preserveWhiteSpace = false;
 

// load XML file

$doc->load('browser-stats.xml');
 

// create new DOM XPATH object

$xpath = new DOMXPath($doc);
 

// set XPATH pattern

$query = '/browsers/browser[@name="'.$name.'"][@version="'.$version.'"][@platform="'.$platform.'"]';
 

// execute XPATH query

$entries = $xpath->query($query);
 

// check if users browser name, version and platform have been recorded.  If an entry is found, then we increment otherwise create

if($entries->length > 1)

{

	//increment "users" attribute

}

else

{

	// create new node with browser name, version and platform

}
 

// this is only for testing purposes.

// visit all entries, get attributes and display each attributes value.

foreach ($entries as $entry) 

{

	foreach ($entry->attributes as $attribute)

	 {

		echo $attribute->nodeName.' = '.$attribute->nodeValue.'<br />';

	}

}

?>

Open in new window

0
Comment
Question by:ellandrd
  • 7
  • 2
10 Comments
 
LVL 11

Expert Comment

by:NoiS
ID: 22789677
For What PHP version is your PHP code?
If 5 use SimpleXML. Much better than DOM.
Have another question about XML with a code on the list.
0
 
LVL 16

Author Comment

by:ellandrd
ID: 22789779
I'm sorry, the live server has PHP 4 installed.

Can you please help with the above?  I can show you my code for creating new nodes (doesnt work though) ?
0
 
LVL 16

Author Comment

by:ellandrd
ID: 22789852
I have got my create node working myself.  However I still need help with the incrementation of users attribute if a node already exists?
0
 
LVL 16

Author Comment

by:ellandrd
ID: 22790219
OK all i need help with is updating the attribute value?  please see code below..
<?php

$name = 'Safari';

$version = '2.0.3';

$platform = 'Windows';

$users = 0;
 

$doc = new DOMDocument();

$doc->load('browser-stats.xml');
 

$xpath = new DOMXPath($doc);
 

$query = '/browsers/browser[@name="'.$name.'"][@version="'.$version.'"][@platform="'.$platform.'"]';
 

$entries = $xpath->query($query);
 

if($entries->length > 0)

{

	foreach($entries as $entry) 

	{

		foreach($entry->attributes as $attribute)

		{

			if($attribute->nodeName == 'users')

			{

				$users = $attribute->nodeName;

				$value = $attribute->nodeValue;

				

				$value = $value + 1;

				

				// update attribute value for "users"

			}

		}

	}

}

else

{

	$root = $doc->getElementsByTagName('browsers')->item(0);

	

	$browser_node = $doc->createElement('browser');

	$root->appendChild($browser_node);
 

	$name_attribute = $doc->createAttribute('name');

	$version_attribute = $doc->createAttribute('version');

	$platform_attribute = $doc->createAttribute('platform');

	$users_attribute = $doc->createAttribute('users');

	

	$browser_node->appendChild($name_attribute);

	$browser_node->appendChild($version_attribute);

	$browser_node->appendChild($platform_attribute);

	$browser_node->appendChild($users_attribute);

		

	$name_value = $doc->createTextNode($name);

	$version_value = $doc->createTextNode($version);

	$platform_value = $doc->createTextNode($platform);

	$users_value = $doc->createTextNode($users);

	

	$name_attribute->appendChild($name_value);

	$version_attribute->appendChild($version_value);

	$platform_attribute->appendChild($platform_value);

	$users_attribute->appendChild($users_value);

}
 

$doc->preserveWhiteSpace = true;

$doc->formatOutput = true;

$doc->save('browser-stats.xml');

?>

Open in new window

0
 
LVL 16

Author Comment

by:ellandrd
ID: 22798212
anybody?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 11

Expert Comment

by:NoiS
ID: 22798641
Man, I don't know much about XML with DOM.
On the manual have some codes like:
You need to get a DOMNode Element to to

DomNode->set_content('NEW CONTENT');
So, Try
$attribute->set_content(''); on your code above.

Later I will make a test with your code and maybe we can have a light.
0
 
LVL 16

Author Comment

by:ellandrd
ID: 22811191
any light?
0
 
LVL 16

Author Comment

by:ellandrd
ID: 22811195
plus i want to update a attribute value....
0
 
LVL 16

Author Comment

by:ellandrd
ID: 22811197
if you can help me in PHP5 and get a solution working to do what i want then ok?
0
 
LVL 6

Accepted Solution

by:
cr4ck3rj4ck earned 500 total points
ID: 22811460
The only change I've made to your code posted 23.10.08 is on line 27.

$value = $value + 1;

replaced with the following:

$attribute->nodeValue = $value + 1;

Seems to work on my setup.

Hope that helps.
<?php

$name = 'Safari';

$version = '2.0.3';

$platform = 'Windows';

$users = 0;

 

$doc = new DOMDocument();

$doc->load('browser-stats.xml');

 

$xpath = new DOMXPath($doc);

 

$query = '/browsers/browser[@name="'.$name.'"][@version="'.$version.'"][@platform="'.$platform.'"]';

 

$entries = $xpath->query($query);

 

if($entries->length > 0)

{

        foreach($entries as $entry) 

        {

                foreach($entry->attributes as $attribute)

                {

                        if($attribute->nodeName == 'users')

                        {

                                $users = $attribute->nodeName;

                                $value = $attribute->nodeValue;

                                

                                $attribute->nodeValue = $value + 1;

                                

                                // update attribute value for "users"

                        }

                }

        }

}

else

{

        $root = $doc->getElementsByTagName('browsers')->item(0);

        

        $browser_node = $doc->createElement('browser');

        $root->appendChild($browser_node);

 

        $name_attribute = $doc->createAttribute('name');

        $version_attribute = $doc->createAttribute('version');

        $platform_attribute = $doc->createAttribute('platform');

        $users_attribute = $doc->createAttribute('users');

        

        $browser_node->appendChild($name_attribute);

        $browser_node->appendChild($version_attribute);

        $browser_node->appendChild($platform_attribute);

        $browser_node->appendChild($users_attribute);

                

        $name_value = $doc->createTextNode($name);

        $version_value = $doc->createTextNode($version);

        $platform_value = $doc->createTextNode($platform);

        $users_value = $doc->createTextNode($users);

        

        $name_attribute->appendChild($name_value);

        $version_attribute->appendChild($version_value);

        $platform_attribute->appendChild($platform_value);

        $users_attribute->appendChild($users_value);

}

 

$doc->preserveWhiteSpace = true;

$doc->formatOutput = true;

$doc->save('browser-stats.xml');

?>

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Password forgotten. 3 40
Load google maps api into wordpress 5 36
test if query has no results 2 19
is this a cms? 8 33
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now