Check existing XML item

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

What i also tried is:

	for each (var ids:XML in myXML.cart){
		if (ids.id.toString() == "4") {
			trace("taken");
		}else{
			trace("free");
		}
	}
}

then i get:

free
free
free
taken

But I want to get free or taken this is absolutly important for any further functions

Thank you!

Are there multiple carts or just one? I mean, I see that you have several cart XML elements, but I’m trying to understand your data model.

In general it would help to understand the purpose of your XML. Are you appending things to it as a way to keep track of items in a shopping cart? Why is there a loop that specifically targets ID 5?

XML lists are trickier to treat as lists than an Array or Vector, since they don’t have many of the convenient operations that array types do, which I think is causing you trouble.

I think the problem is that you’re modifying the XML in the same loop you’re using to find the missing dataset. You need to separate them, and you can use e4x filters to simplify the search.

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

var fifth = (<cart> 
			<id>5</id> 
			<product>Item 1</product> 
		</cart>);

if (myXML.cart.(id == "5").length() === 0) {
	myXML.appendChild(fifth);
}

trace(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>
  <cart>
    <id>5</id>
    <product>Item 1</product>
  </cart>
</data>
1 Like