I figured out how to create a DOM object for XML with the following code (part of it):
$xml_item = $xml->createElement('item');
$xml_location = $xml->createElement('location');
$xml_item->setAttribute('item-id', 'abcd');
$xml_item->appendChild($xml_location);
$xml_location->setAttribute('location-id', '1234');
$xml_location->appendChild($xml_quantity);
$xml_quantity = $xml->createElement('quantity', '0');
Gives:
<item item-id="abcd">
<location location-id="1234">
<quantity>0</quantity>
</location>
</item>
I want to keep adding more item elements of different attributes to obtain something like this:
<item item-id="abcd">
<location location-id="1234">
<quantity>99</quantity>
</location>
</item>
<item item-id="qwer">
<location location-id="1234">
<quantity>55</quantity>
</location>
</item>
But I'm having a hard time figuring this out. How do I use the same variable $xml_item to create multiple entries of "item" element with different attribute as above (i.e. abcd and qwer)? It seems to just over write the first one when I do another $xml_item->setAttribute('item-id', 'qwer') after creating "abcd."
Am I supposed to create multiple copies of "$xml_item" with different variable names (e.g. $xml_item1, _item2, etc. but this seems unreasonably tedious) or can I somehow reuse the same variable ($xml_item) to create multiple entries? The idea is to create as many of those "item" element as I need with different attributes.