0

I'm using SimpleXML to generate xml based invoice.

The structure has to be like this (heavily simplified):

<invoice>
    <total>
        <price>100</price>
    </total>
    <items>
        <item>...</item>
    </items>
</invoice>

But if first loop my items and add totals together, and then insert <total>:

<invoice>
    <items>...</items>
    <total>...</total>
</invoice>

But CUSTOM XSD says it invalid. This probably will not cause an error in applications, but I'd like it to be valid.

So can I insert <total> tag before <items> tag?

Note: <items> tag is not the first element in <invoice>.

Jquery equivalent of the function in need is .insertBefore()

Cheers!

marc_s
  • 704,970
  • 168
  • 1,303
  • 1,425
Kristian
  • 3,047
  • 1
  • 20
  • 45

1 Answers1

1

You can do something like this:

    $domelement = dom_import_simplexml($items);

    $new = $dom->insertBefore(
        $dom->ownerDocument->createElement("total"),
        $dom->firstChild
    );

    $newsxml = simplexml_import_dom($new);

then add the items into total node.

Asad Saeeduddin
  • 45,116
  • 6
  • 87
  • 135
  • Yes found it just on http://stackoverflow.com/questions/3361036/php-simplexml-insert-node-at-certain-position. Thanks anyways! :) – Kristian Oct 23 '12 at 12:32