How can one change the name of the root of only one dom node?
We can not change the property Note: I can do this ... but this is a solution, not a DOM-solution. PHP example PHP example (does not work, but why?): @ suggestion (only pointing link) and my interpretation of the poor An error occurred with the renameNode () method, An error occurred due to the replaceChild () method, As it has not actually been answered yet, the error you get is slightly You have copied the I have seen this problem well and do not have this error: You can see it in the last line of the function body: this It has not yet been outlined in PHP manual users, however, if you follow the link of the blog-post, the original form From the Anyway, my version uses a few different variables naming here and is more specific, as in the example in the PHP manual about the types it works with names nodes. I have not been booked yet what would be best, e.g. An additional work is working with it, taking from the node to the namespace to clearly change or change a different function at a different function. document element in the
DOMElement object in the DOM document model, therefore, we need to "rebuild" the node. .. But how to "rebuild" with
childNodes property?
try-1
/ H3>
// DOMElement :: documentElement can not be changed, hence ... function DomElement_renameRoot1 ($ ele, $ ROOTAG = 'newRoot') {if (Gettype ($ ele)) == 'Object' & amp; $ Ele- & gt; Node type == XML_ELEMENT_NODE) {$ doc = new DOMDocument (); $ Eaux = $ doc- & gt; Create Element ($ ROOTAG); // DOMElement foreach ($ ele-> Child nodes as $ node) if ($ node-> node type1 = 1) // domilement $ eaux-> AppendChild ($ node); // Error! Otherwise ($ node-> node type == 3) / DOMText $ eaux-> Append chald ($ node); // Error! $ Eaux return; } And die ("Error: invalid DOM object as input"); }>
Append shield ($ node) Due to error:
Fatal error: 'Exception exception' with message 'DOMException' Wrong document error '
try-2
function DomElement_renameRoot2 ($ ele, $ ROOTAG = 'newRoot') {$ ele- & gt; Owner Document- & gt; Rename ($ ele, null, "h1"); $ Ele returned; }
Warning: DOMDocument :: renameNode (): Not yet implemented Rename Function (DOMElement $ node, $ newname) {$ newNode =
Fatal error: message 'not found error' with 'exception' DOMException '
renameNode () function.
/ ** * A node changes the name of a DOM document. * * @ Param Domellement $ node * @ Ultimate String $ name * * @ Return Domod * / Function dom_rename_element (Domain $ node, $ name) {$ name = $ node-> Owner Document- & gt; Create Element ($ name); Forex currency ($ node-> attributes as $ attribute) {$ renamed-> Set attribute ($ attribute- & gt; node name, $ attribute-> node); } While ($ node-> First Child) {$ named-> Append chald ($ node-> first hair); } $ Node- & gt; ParentNode-> Replace the changed child (changing $ name, $ node); }
- & gt; Owner.document instead of
-> ParentNode is using . As the
$ node was not a child of the document, you found the error and it was wrong to believe that it should be. Instead use the basic element to change the child there;)
renameNode () function it has been suggested that you can also offer this solution to a comment given below.
No comments:
Post a Comment