Forums / Setup & design / I'm lost with the DOM - how do I change one node attribute?

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("**********");
								 }
							}
						}
					}
				}

eZ debug

Timing: Jan 18 2025 11:03:19
Script start
Timing: Jan 18 2025 11:03:19
Module start 'content'
Timing: Jan 18 2025 11:03:20
Module end 'content'
Timing: Jan 18 2025 11:03:20
Script end

Main resources:

Total runtime1.0057 sec
Peak memory usage4,096.0000 KB
Database Queries214

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0084 587.9688180.7969
Module start 'content' 0.00840.8427 768.7656686.6875
Module end 'content' 0.85110.1545 1,455.4531348.4297
Script end 1.0056  1,803.8828 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00420.4188210.0002
Check MTime0.00180.1741210.0001
Mysql Total
Database connection0.00180.177210.0018
Mysqli_queries0.894288.90592140.0042
Looping result0.00250.25022120.0000
Template Total0.963695.820.4818
Template load0.00230.224720.0011
Template processing0.961395.580420.4806
Template load and register function0.00010.010110.0001
states
state_id_array0.00130.125810.0013
state_identifier_array0.00140.140020.0007
Override
Cache load0.00200.2025660.0000
Sytem overhead
Fetch class attribute can translate value0.00140.135540.0003
Fetch class attribute name0.00160.1548120.0001
XML
Image XML parsing0.00190.193040.0005
class_abstraction
Instantiating content class attribute0.00000.0032180.0000
General
dbfile0.00700.6915390.0002
String conversion0.00000.000530.0000
Note: percentages do not add up to 100% because some accumulators overlap

CSS/JS files loaded with "ezjscPacker" during request:

CacheTypePacklevelSourceFiles
CSS0extension/community/design/community/stylesheets/ext/jquery.autocomplete.css
extension/community_design/design/suncana/stylesheets/scrollbars.css
extension/community_design/design/suncana/stylesheets/tabs.css
extension/community_design/design/suncana/stylesheets/roadmap.css
extension/community_design/design/suncana/stylesheets/content.css
extension/community_design/design/suncana/stylesheets/star-rating.css
extension/community_design/design/suncana/stylesheets/syntax_and_custom_tags.css
extension/community_design/design/suncana/stylesheets/buttons.css
extension/community_design/design/suncana/stylesheets/tweetbox.css
extension/community_design/design/suncana/stylesheets/jquery.fancybox-1.3.4.css
extension/bcsmoothgallery/design/standard/stylesheets/magnific-popup.css
extension/sevenx/design/simple/stylesheets/star_rating.css
extension/sevenx/design/simple/stylesheets/libs/fontawesome/css/all.min.css
extension/sevenx/design/simple/stylesheets/main.v02.css
extension/sevenx/design/simple/stylesheets/main.v02.res.css
JS0extension/ezjscore/design/standard/lib/yui/3.17.2/build/yui/yui-min.js
extension/ezjscore/design/standard/javascript/jquery-3.7.0.min.js
extension/community_design/design/suncana/javascript/jquery.ui.core.min.js
extension/community_design/design/suncana/javascript/jquery.ui.widget.min.js
extension/community_design/design/suncana/javascript/jquery.easing.1.3.js
extension/community_design/design/suncana/javascript/jquery.ui.tabs.js
extension/community_design/design/suncana/javascript/jquery.hoverIntent.min.js
extension/community_design/design/suncana/javascript/jquery.popmenu.js
extension/community_design/design/suncana/javascript/jScrollPane.js
extension/community_design/design/suncana/javascript/jquery.mousewheel.js
extension/community_design/design/suncana/javascript/jquery.cycle.all.js
extension/sevenx/design/simple/javascript/jquery.scrollTo.js
extension/community_design/design/suncana/javascript/jquery.cookie.js
extension/community_design/design/suncana/javascript/ezstarrating_jquery.js
extension/community_design/design/suncana/javascript/jquery.initboxes.js
extension/community_design/design/suncana/javascript/app.js
extension/community_design/design/suncana/javascript/twitterwidget.js
extension/community_design/design/suncana/javascript/community.js
extension/community_design/design/suncana/javascript/roadmap.js
extension/community_design/design/suncana/javascript/ez.js
extension/community_design/design/suncana/javascript/ezshareevents.js
extension/sevenx/design/simple/javascript/main.js

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1node/view/full.tplfull/forum_topic.tplextension/sevenx/design/simple/override/templates/full/forum_topic.tplEdit templateOverride template
9content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
9content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
17content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
4content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
8content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 49
 Number of unique templates used: 7

Time used to render debug report: 0.0002 secs