mko
mko

Reputation: 22064

add xml node at the first of the it's parent node

<?xml version="1.0" encoding="utf-8"?>

<users>
  <user>
    <firstname>Mark</firstname>
    <surname>Zhu</surname>
    </user>
</users>

this is the user node I want to add in the front of the existing one

  <user>
<firstname>Andy</firstname>
<surname>Li</surname>
</user>

The SimpleXMLElement::addChild can add the in the back of the existing one, Is there anybody know how to add in the front?

Upvotes: 0

Views: 549

Answers (3)

Andrew67
Andrew67

Reputation: 357

You'll have to use DOM, in particular

DOMNode::insertBefore(DOMNode, DOMNode)

See http://www.php.net/manual/en/domnode.insertbefore.php

Upvotes: 2

jwueller
jwueller

Reputation: 30996

I would recommend to simply add the xml-declaration by hand. You can simply output it before the xml structure.

Upvotes: 0

Olical
Olical

Reputation: 41362

What if you read the xml then in a new document, printed your user node + the original xml?

Upvotes: 0

Related Questions