Hello there!
I’m trying to check the existence of an Item in a XML File here is my XML:
myXML = (<data>
<cart>
<id>1</id>
<product>Item 1</product>
</cart>
<cart>
<id>2</id>
<product>Item 1</product>
</cart>
<cart>
<id>3</id>
<product>Item 1</product>
</cart>
<cart>
<id>4</id>
<product>Item 1</product>
</cart>
</data>);
and what i want to do is now to check if 5 is already taken or not, and if not it should insert this. So I tried:
for each (var ids:XML in myXML.cart) {
if (ids.id == "5") {
trace ("item is taken");
}else{
myXML.appendChild(
<cart>
<id>5</id>
<product>Item 1</product>
</cart>);
trace (myXML);
}
}
So in this example I don’t have an Item 5 but if I have one, it returns correctly “item is taken”, but if not it returns this:
<data>
<cart>
<id>1</id>
<product>Item 1</product>
</cart>
<cart>
<id>2</id>
<product>Item 1</product>
</cart>
<cart>
<id>3</id>
<product>Item 1</product>
</cart>
<cart>
<id>4</id>
<product>Item 1</product>
</cart>
<cart>
<id>5</id>
<product>Item 1</product>
</cart>
</data>
<data>
<cart>
<id>1</id>
<product>Item 1</product>
</cart>
<cart>
<id>2</id>
<product>Item 1</product>
</cart>
<cart>
<id>3</id>
<product>Item 1</product>
</cart>
<cart>
<id>4</id>
<product>Item 1</product>
</cart>
<cart>
<id>5</id>
<product>Item 1</product>
</cart>
<cart>
<id>5</id>
<product>Item 1</product>
</cart>
</data>
<data>
<cart>
<id>1</id>
<product>Item 1</product>
</cart>
<cart>
<id>2</id>
<product>Item 1</product>
</cart>
<cart>
<id>3</id>
<product>Item 1</product>
</cart>
<cart>
<id>4</id>
<product>Item 1</product>
</cart>
<cart>
<id>5</id>
<product>Item 1</product>
</cart>
<cart>
<id>5</id>
<product>Item 1</product>
</cart>
<cart>
<id>5</id>
<product>Item 1</product>
</cart>
</data>
<data>
<cart>
<id>1</id>
<product>Item 1</product>
</cart>
<cart>
<id>2</id>
<product>Item 1</product>
</cart>
<cart>
<id>3</id>
<product>Item 1</product>
</cart>
<cart>
<id>4</id>
<product>Item 1</product>
</cart>
<cart>
<id>5</id>
<product>Item 1</product>
</cart>
<cart>
<id>5</id>
<product>Item 1</product>
</cart>
<cart>
<id>5</id>
<product>Item 1</product>
</cart>
<cart>
<id>5</id>
<product>Item 1</product>
</cart>
</data>
I already know why I get the whole XML 5 times, because there are then 5 items. So how can I make this better? Please help.
King regards