Solved

php and xml

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

760 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