Joran Dob
Joran Dob

Reputation: 360

How do I add an attribute to XML in PHP

I need to get the id inside the wonen-object like this:

<wonen-object ObjectID="259">

because it now looks like this:

<wonen-object>
<Id>1113</Id>

So the objectid needs to be inside the wonen-object

This my code:

while($row = mysql_fetch_assoc($result)) {
$mydata = $xml->addChild('wonen-object');
$mydata->addChild('Id',$row['id']);

Thanks!!

Upvotes: 1

Views: 52

Answers (1)

It looks for all the world like you are using SimpleXML. This is the assumption I am going with.

Adding a child creates a new set of nested tags (child elements) inside the currently selected DOM element (pair of XML tags) which is not what you want.

What you want to do is add an attribute to the existing child. So you are looking for the addAttribute method.

while($row = mysql_fetch_assoc($result)) {
   $mydata = $xml->addChild('wonen-object');
   $mydata->->addAttribute('Id',$row['id']);
   // ...
}

This should get you where you are trying to go.

Upvotes: 1

Related Questions