Ask Question Forum:
Model Library:2025-02-08 Updated:A.I. model is online for auto reply question page
C
O
M
P
U
T
E
R
2
8
Show
#
ASK
RECENT
←
- Underline
- Bold
- Italic
- Indent
- Step
- Bullet
- Quote
- Cut
- Copy
- Paste
- Table
- Spelling
- Find & Replace
- Undo
- Redo
- Link
- Attach
- Clear
- Code
Below area will not be traslated by Google,you can input code or other languages
Hint:If find spelling error, You need to correct it,1 by 1 or ignore it (code area won't be checked).
X-position of the mouse cursor
Y-position of the mouse cursor
Y-position of the mouse cursor
Testcursor
caretPos
Attachment:===
Asked by Kaiser
at 2024-07-29 20:35:13
Point:500 Replies:5 POST_ID:829157USER_ID:10
Topic:
PHP Scripting Language;Programming Languages;Web Applications
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:*/
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> 1:2:3:4:5:6:7:8:9:10:11:12:13:14:
/*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 1:2:3:4:5:6:7:8:9:10:11:12:13:
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!
Assisted Solution
Expert: Ray Paseur replied at 2024-07-30 04:18:23
250 points EXCELLENT
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.phperror_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. " ";// RECOVER THE XML FROM THE OBJECT$new = $obj->asXML();// RENDER THE OLD XML AND THE MUTATED XMLecho htmlentities($xml);echo PHP_EOL;echo htmlentities($new);echo PHP_EOL; 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:22:23:24:25:26:27:28:29:30:31:32:33:34:35:36:37:38:
Assisted Solution
Expert: duncanb7 replied at 2024-07-29 22:05:02
150 points EXCELLENT
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; 1:2:3:4:5:6:7:8:9:10:
Expert: duncanb7 replied at 2024-07-29 21:59:04
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
new node at http://php.net/manual/en/domnode.replacechild.php
Duncan
Accepted Solution
Expert: aikimark replied at 2024-07-29 21:27:08
100 points EXCELLENT
Once you execute this statement:
$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 1:
Expert: aikimark replied at 2024-07-29 21:21:47
moved XML and PHP code into snippet
aikimark -- zone advisor
aikimark -- zone advisor