2

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.

1
  • Do you guys happen to know how to convert this object $xml into a string? Commented Jul 6, 2011 at 23:29

3 Answers 3

3

From the php.net page for createElement,

This node will not show up in the document unless it is inserted with (e.g.) DOMNode->appendChild().

So just make sure to keep appending $xml_item to your DomDocument object between createElement() calls.
ie) $xml->appendChild($xml_item);

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I wondering what that actually meant.. So I call createElement() followed by appendChild() every time. Superb! It is working.
2

I think what you are missing is that $xml-item is a reference to an object - every call you make to one of its function is being called on the same instance of the object, so setAttribute will override whatever value you set before.

To create a new instance of the object you need to call

$xml_item = $xml->createElement('item');

again - once for every item that you want to add.

You can use the same variable name - that way $xml-item will be referencing a different new instance of 'item' element, and the old instance will no longer be accessible (except from the parent $xml).

As brian_d mentioned, after each call to createElement you will need to call

$xml->appendChild($xml_item);

so all of the items will appear in the parent DOM document.

Comments

0
$img = $doc->createElement( "img" );

  $imgattr = $doc->createAttribute( "src" );

  $imgattr1 = $doc->createAttribute( "width" );

  $imgattr1->value = 300;

  $imgattr->value = $image['path'];// this the source of my image

  $img->appendChild( $imgattr );

  $img->appendChild( $imgattr1 );

  $b->appendChild( $img );

Here img is the element and i have added src and width attribute and then add value of the attribute and append them to element If any queries tell me i will share my complete

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.