开发者

Creating multiple attributes for an element

开发者 https://www.devze.com 2023-03-18 01:17 出处:网络
I figured out how to create a DOM object for XML with the following code (part of it): $xml_item = $xml->createElement(\'item\');

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 ite开发者_运维技巧m-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.


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);


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.


$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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号