Solved

php and xml

Posted on 2014-07-29
5
233 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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 …

831 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