E4X - set element value where attribute equals

620 views Asked by At

Hoping someone can help me.

I have and XML document that looks like this.

<root>
  <items>
     <item id='data1' itemType = 'integer'></item>
     <item id='data2' itemType = 'character'></item>
     <item id='data3' itemType = 'decimal'></item>
     <item id='data4' itemType = 'boolean'></item>
  </items>
</root>

And a data object that looks like this.

var dataObj = {};
dataObj.item1 = 10;
dataObj.item2 = "hello world";
dataObj.item3 = 10.23;
dataObj.item4 = true;
dataObj.item5 = new Date();

What I am trying to do is to iterate through the data object and set the appropriate XML element with the data objects value where the element's id attribute equals the dataObj item.

This is what I have tried so far.

for (var data in dataObj)
{
  xml.items.item.(@id = data) = dataObj[data];
}

But I get this error:

rhinojavascript.JavascriptExecutionHandler$RhinoScriptException: Invalid assignment left-hand side

1

There are 1 answers

0
IanBram On

OK I have figured it out so thought I would leave the answer in case anyone else comes across the same issue.

It would appear that if you want to set the value of the node itself you have to use the setChildren method (not very intuitive).

So my code needed to change as follows:

for (var data in dataObj)
{
  xml.items.item.(@id == data).setChildren(dataObj[data]);
}