Solved

php and xml

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

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

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
Windows Tool to Build Android and iOS App 3 51
PHPStorm debugging issues 1 21
php string masking 4 27
highcharts wont fix inside a bootstrap modal window 6 27
This article will show, step by step, how to integrate R code into a R Sweave document
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.

911 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

21 Experts available now in Live!

Get 1:1 Help Now