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
  • 732,580
  • 175
  • 1,330
  • 1,459
Kristian
  • 3,283
  • 3
  • 28
  • 52

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
  • 46,193
  • 6
  • 90
  • 139
  • 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