Solved

php and xml

Posted on 2014-07-29
5
228 Views
Last Modified: 2014-09-02
I'm needing to modify the value of  a specific XML node with php, but I keep getting this error with the following code:

Catchable fatal error: Argument 1 passed to DOMNode::replaceChild() must be an instance of DOMNode, string given in...


      /*Here's the XML Structure:*/
<pages>
	<page>
		<contents>A bunch of text here
		</contents>
	</page>
	<page>
		<contents>A bunch of text here
		</contents>
	</page>
	<page>
		<contents>A bunch of text here
		</contents>
	</page>
</pages>

Open in new window

/*And here's the code I'm trying to use to edit specific content nodes*/
	$data = new DOMDocument('1.0', 'utf-8');
	$data->formatOutput = true;
	$data->preserveWhiteSpace = true;
	$data->load('../data.xml');
	
	//load elements

	$page = $data->getElementsByTagName('page')->item(0);
	$contents = $page->getElementsByTagname('contents')->item(0);
	
	$contents = $string; //This is a string variable

	$page->replaceChild($contents, $contents);  //This is the line that produces the error

Open in new window

So I'm not sure if my overall approach is wrong, or if I just need to modify the string variable make it an instance of DOMNode.  Please Help!  Thank you!
0
Comment
Question by:Kaiser
  • 2
5 Comments
 
LVL 45

Accepted Solution

by:
aikimark earned 100 total points
ID: 40228390
Once you execute this statement:
$contents = $string; //This is a string variable

Open in new window

$contents no longer points to the node.

Delete or comment that statement and try this version of the replaceChilld() statement.
$page->replaceChild($contents, $string);  //This is the line that produces the error

Open in new window

0
 
LVL 13

Expert Comment

by:duncanb7
ID: 40228416
Please read the manual and  example  for replaceChild() in php.net, it is used to replace it to
new node at http://php.net/manual/en/domnode.replacechild.php



Duncan
0
 
LVL 13

Assisted Solution

by:duncanb7
duncanb7 earned 150 total points
ID: 40228420
Try  this
$data = new DOMDocument('1.0', 'utf-8');
	$data->formatOutput = true;
	$data->preserveWhiteSpace = true;
	$data->load('../data.xml');
	
	//load elements

	$page = $data->getElementsByTagName('page')->item(0);
	$contents = $page->getElementsByTagname('contents')->item(0);
$contents->nodeValue=$string;

Open in new window

0
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 40228790
This looks like a very theoretical and redacted example.  It may be helpful if you have a better test case.  But that aside, here is how you can do it.  Please copy this, install it on your server and run it to see the output, and please post back if you still have any questions.  Best regards, ~Ray

<?php // demo/temp_kwkened.php
error_reporting(E_ALL);
echo '<pre>';

// SEE http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28486615.html

$xml = <<<EOD
<pages>
	<page>
		<contents>A bunch of text here
		</contents>
	</page>
	<page>
		<contents>A bunch of text here
		</contents>
	</page>
	<page>
		<contents>A bunch of text here
		</contents>
	</page>
</pages>
EOD;

// LOAD THE XML INTO AN OBJECT
$obj = SimpleXML_Load_String($xml);

// REPLACE SOMETHING IN THE OBJECT
$obj->page[0]->contents = 'New data' . PHP_EOL. "\t\t";

// RECOVER THE XML FROM THE OBJECT
$new = $obj->asXML();

// RENDER THE OLD XML AND THE MUTATED XML
echo htmlentities($xml);
echo PHP_EOL;

echo htmlentities($new);
echo PHP_EOL;

Open in new window

0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

816 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

11 Experts available now in Live!

Get 1:1 Help Now