开发者

XML attributes <item id="1"> vs <id> 1 </id> [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

XML attribute vs XML element

What is the difference between snippet "A":

<root>
    <item id="1">
        <attr1> foo </attr1>
        <attr2> bar </attr2>
    </item>
</root>

and placing the "id" attribute within the element in snippet "B":

<root>
    <item>
        <id> 1 </id>
        <attr1> foo </attr1>
        <attr2> bar </attr2>
    </item>
</root>

and how would I add the attribute "id" when creating xml with PHP in snippet "A" assuming I can already create the format of snippet "B" just fine using DOM's createElement() and appendChi开发者_C百科ld?

Thanks!


"id" is an attribute in snippet "A" and a node in snippet "B". You can use setAttribute to add this.


Use setAttribute to add the attribute to the DOM element:

$domNode->setAttribute("id", "youridvalue");

The only difference between the two is the syntax used to get the values back, and in one you're adding an attribute, the other you're adding a tag. Either works, I prefer attributes.


You can call $element->setAttribute($name, $value) after you have created the id element

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜