PHP XML inserting element after (or before) another element

后端 未结 3 468
再見小時候
再見小時候 2020-11-30 14:19

I have a previously generated XML like this:



    
3条回答
  •  时光说笑
    2020-11-30 14:49

    Fetch the node and use

    • DOMNode::insertBefore — Adds a new child before a reference node

    instead of appending to the documentElement.

    You can do that from the DOMDocument as well when passing in the shop node as second argument. Personally, I find it easier to just do that from the shop node because you have to fetch it anyway:

    $shopNode->insertBefore($newNode);
    

提交回复
热议问题