Solved

php and xml

Posted on 2014-07-29
5
235 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 110

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
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…
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.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…

749 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