Monday 15 June 2015

php - How to change root of a node with DomDocument methods? -


How can one change the name of the root of only one dom node?

We can not change the property 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?


Note: I can do this ... but this is a solution, not a DOM-solution. PHP example

PHP example (does not work, but why?):

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

@ suggestion (only pointing link) and my interpretation of the poor

  function DomElement_renameRoot2 ($ ele, $ ROOTAG = 'newRoot') {$ ele- & gt; Owner Document- & gt; Rename ($ ele, null, "h1"); $ Ele returned; }   

An error occurred with the renameNode () method,

  Warning: DOMDocument :: renameNode (): Not yet implemented   Rename Function (DOMElement $ node, $ newname) {$ newNode =    $ node- & gt; OwnerDocument- & gt; CreateElement ($ NewName); Forex Currency ($ node-> Features $ attribute) $ newNode- & gt; Set attribute ($ attribute- & gt; node name, $ attribute-> node); While ($ node-> first hair) $ newNode-> Append chald ($ node-> first hair); // Change baby first! Next! $ Node- & gt; Owner Document- & gt; Replaceable children ($ newnode, $ node); // change $ node? // $ newNode's return is not required; }   

An error occurred due to the replaceChild () method,

  Fatal error: message 'not found error' with 'exception' DOMException '  

As it has not actually been answered yet, the error you get is slightly You have copied the renameNode () function.

I have seen this problem well and do not have this error:

  / ** * 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); }   

You can see it in the last line of the function body: this - & 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;)

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 renameNode () function it has been suggested that you can also offer this solution to a comment given below.

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.

No comments:

Post a Comment