I'm lost with the DOM - how do I change one node attribute?

Author Message

Pascal Specht

Monday 10 September 2007 7:47:32 am

Hi,

can someone help me understand what I'm doing wrong? I'm using a text attribute (data_text_1) in an order. This text attribute contains a serialized dom tree like:

<?xml version="1.0" encoding="UTF-8"?>
<shop_account>
<salutation>1</salutation>
<first-name>Pascal</first-name>
<last-name>Specht</last-name>
...
<transaction>[no transaction]</transaction>
</shop_account>

and I'd like to change the content "[no transaction]" to "[test]"...

I thought that I could do that by unserializing, then grabbing the element with elementsByName, and applying setAttribute and writing back as a string, but it doesn't... The node still contains [no transaction]!

				$xml = new eZXML();
				$xmlDoc =& $order->attribute( 'data_text_1' );
				if( $xmlDoc != null )
				{
					$dom =& $xml->domTree( $xmlDoc );
					if ($dom) 
					{
						$item =& $dom->elementsByName( "transaction" );
						$item->setAttribute( 'data_text', "[test]" );
					}
				}
				$order->setAttribute( 'data_text_1', $xmlDoc->toString() );

Obviously this doesn't work. Any ideas how to correct the problem would be greatly appreciated!

Pascal

Pascal von Büren

Monday 10 September 2007 8:55:42 am

Hi Pascal,

your code ends quite quickly, did you call

$order->store();

afterwards?

Pascal Specht

Tuesday 11 September 2007 2:27:29 am

Hi Pascal,

I must admit I hadn't the order->store() instruction, but unfortunately, after adding it (and clearing the cache) it doesn't get better, the order still is unchanged. I suppose the two $item-> instructions to be wrong...

Thanks for your help,
Pascal

Pascal von Büren

Tuesday 11 September 2007 3:15:19 am

Ok, i can see two more possilbe problems.

First of all, <b>elementsByName</b> returns an Array. So you could look over the result or use <b>elementByName</b>.

Additionally, you want to set the TextContent of a Node, but you use setAttribute. You'd be off better using <b>setContent</b>.

One more question: How are you calling this script? By CLI or as HTTP request?

André R.

Tuesday 11 September 2007 3:43:56 am

Something like this maybe?

        $items =& $dom->elementsByName( "transaction" );
        
        // Note: php4 foreach copy's array values instead of referencing them
        for( $i = 0, $c = count( $items ); $c > $i ; $i++)
        {
            $items[$i]->setContent("[test]");
            //$items[$i]->setAttribute( 'data_text', "[test]" ); this will result in <transaction data_text="[test]">[no transaction]</transaction>
        }

eZ Online Editor 5: http://projects.ez.no/ezoe || eZJSCore (Ajax): http://projects.ez.no/ezjscore || eZ Publish EE http://ez.no/eZPublish/eZ-Publish-Enterprise-Subscription
@: http://twitter.com/andrerom

Pascal Specht

Tuesday 11 September 2007 5:39:15 am

I built in both of your suggestions, and I have now this code, which apparently does not raise any warnings or errors. The thing is that the setContent("[test]") now gets called but when I look at my data_text_1, it still contains the original XML + the two ## on each side...

				$xml = new eZXML();
				$xmlDoc =& $order->attribute( 'data_text_1' );

				if( $xmlDoc != null )
				{
					$dom =& $xml->domTree( $xmlDoc );
					if ($dom) 
					{
						$items =& $dom->elementsByName( "transaction" );
						for( $i = 0, $c = count( $items ); $c > $i ; $i++)
						{
							$items[$i]->setContent("[test]");
							$items[$i]->store();    
						}
					}
				}
				$str = "##" . $dom->toString() . "##";
				$order->setAttribute( 'data_text_1', $str );
				$order->store();

Originally, what I wanted to do is simply add one item into a string based XML text...

Any help much appreciated!

Pascal von Büren

Tuesday 11 September 2007 7:24:54 am

Hi Pascal,
one more try:

                                $xml = new eZXML();
                                $xmlDoc =& $order->attribute( 'data_text_1' );
 
                                if( $xmlDoc != null )
                                {
                                        $dom =& $xml->domTree( $xmlDoc );
                                        if ($dom) 
                                        {
                                                $root =& $dom->root(); 
                                                $items =& $root->elementsByName( "transaction" );
                                                for( $i = 0, $c = count( $items ); $c > $i ; $i++)
                                                {
                                                        $items[$i]->setContent("[test]");
                                                }
                                        }
                                }
                                $str = "##" . $dom->toString() . "##";
                                $order->setAttribute( 'data_text_1', $str );
                                $order->store();

(getting the elements starting from DOM-Root)

Pascal Specht

Tuesday 11 September 2007 8:24:59 am

Hi,

This really drives me crazy! I spent the whole day on it, and it still doesn't work...
Obviously the code changes the node content, but the final result doesn't reflect the change!

				$xml = new eZXML();
				$xmlDoc = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <a> <xxx>*</xxx> </a>";

				if( $xmlDoc != null )
				{
					$dom =& $xml->domTree( $xmlDoc );
					if ($dom) 
					{
						$root =& $dom->root(); 

                         $items =& $root->elementsByName( "xxx" );						
						for( $i = 0, $c = count( $items ); $c > $i ; $i++)
						{
							$items[$i]->setContent("**********");
							echo "!item change occured!";
						}
					} 
				}

				echo "<hr><pre>";
				echo htmlspecialchars( $xmlDoc );
				echo "<hr>";
				echo htmlspecialchars( $dom->toString() );
				echo "<hr></pre>";

produces the following output:

!item change occured!
--------------------------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?> 
<a> 
  <xxx>*</xxx> 
</a>
--------------------------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<a>
  <xxx>*</xxx>
</a>
--------------------------------------------------------------------------------------------------------------------------------

Pascal Specht

Tuesday 11 September 2007 9:18:33 am

OK, so for anyone interrested in doing it, I found the solution now:

To replace all occurences of <xxx>*</xxx> you can do:

				$xml = new eZXML();
				$xmlDoc = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <a> <xxx>*</xxx> </a>";

				if( $xmlDoc != null )
				{
					$dom =& $xml->domTree( $xmlDoc );

					if ($dom) 
					{
						$root =& $dom->root(); 

                        $items =& $root->elementsByName( "xxx" );						
						for( $i = 0, $c = count( $items ); $c > $i ; $i++)
						{
							$element = $items[$i];
							if ( count( $element->Children ) > 0 )
							{
								 foreach( array_keys( $element->Children ) as $key )
								 {
									 $child =& $element->Children[$key];
									 $child->setContent("**********");
								 }
							}
						}
					}
				}

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.

eZ debug

Timing: Jan 31 2025 01:28:57
Script start
Timing: Jan 31 2025 01:28:57
Module start 'layout'
Timing: Jan 31 2025 01:28:57
Module start 'content'
Timing: Jan 31 2025 01:28:57
Module end 'content'
Timing: Jan 31 2025 01:28:57
Script end

Main resources:

Total runtime0.0219 sec
Peak memory usage4,096.0000 KB
Database Queries3

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0040 588.3906151.2422
Module start 'layout' 0.00400.0039 739.6328220.7500
Module start 'content' 0.00790.0125 960.38281,013.9922
Module end 'content' 0.02040.0015 1,974.375045.9922
Script end 0.0218  2,020.3672 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.002712.2279140.0002
Check MTime0.00114.8144140.0001
Mysql Total
Database connection0.00052.294910.0005
Mysqli_queries0.002411.144230.0008
Looping result0.00000.060010.0000
Template Total0.00104.710.0010
Template load0.00083.800510.0008
Template processing0.00020.837310.0002
Override
Cache load0.00062.725510.0006
General
dbfile0.00104.553880.0001
String conversion0.00000.029440.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 1
 Number of unique templates used: 1

Time used to render debug report: 0.0002 secs