PDA

View Full Version : Searching XML?


karldettmann
03-19-2008, 12:35 AM
I have a very simplified version of some XML I need to search and return the SKU value.


<productsSet>
<productLink>
<sku>BTCD.BTBB/BTBB</sku>
<gender>BOY</gender>
<skin>LIGHT</skin>
<hair>BLOND</hair>
</productLink>
<productLink>
<sku>BTCD.BTBG/BTBG</sku>
<gender>GIRL</gender>
<skin>LIGHT</skin>
<hair>BLOND</hair>
</productLink>
</productsSet>

What I need to search by is three variables. Gender, Skin, and Hair. What I then need returned is the SKU that matches the three variables.

So for the example above. I want to find the SKU where the Gender = GIRL, Skin = Light, and Hair = Blond. What is the best way to do this?

dimpiax
03-19-2008, 01:33 AM
Here is)
var _xml : XML = <productsSet>
<productLink>
<sku>A</sku>
<gender>BOY</gender>
<skin>LIGHT</skin>
<hair>BLOND</hair>
</productLink>
<productLink>
<sku>B</sku>
<gender>GIRL</gender>
<skin>LIGHT</skin>
<hair>BLOND</hair>
</productLink>
</productsSet>

for(var i : int = 0; i < _xml.productLink.length(); i++) {
if(_xml.productLink[i].gender.text() == 'GIRL' && _xml.productLink[i].skin.text() == 'LIGHT' && _xml.productLink[i].hair.text() == 'BLOND')
trace(_xml.productLink[i].sku.text());
}

UPD:
or more good view of code:
for(var i : String in _xml.productLink) {
if(_xml.productLink[i].gender.text() == 'GIRL' && _xml.productLink[i].skin.text() == 'LIGHT' && _xml.productLink[i].hair.text() == 'BLOND')
trace(_xml.productLink[i].sku.text());
}

sgartner
03-19-2008, 04:17 AM
I have a very simplified version of some XML I need to search and return the SKU value.

What I need to search by is three variables. Gender, Skin, and Hair. What I then need returned is the SKU that matches the three variables.

So for the example above. I want to find the SKU where the Gender = GIRL, Skin = Light, and Hair = Blond. What is the best way to do this?

Here you go:


var productsSet:XML = <productsSet>
<productLink>
<sku>BTCD.BTBB/BTBB</sku>
<gender>BOY</gender>
<skin>LIGHT</skin>
<hair>BLOND</hair>
</productLink>
<productLink>
<sku>BTCD.BTBG/BTBG</sku>
<gender>GIRL</gender>
<skin>LIGHT</skin>
<hair>BLOND</hair>
</productLink>
</productsSet>;

var skuList:XMLList = productsSet
.productLink
.( gender == "GIRL"
&& skin == "LIGHT"
&& hair == "BLOND")
.sku;
trace(skuList[0]);

karldettmann
03-19-2008, 01:17 PM
These worked great! Thanks

ABM
03-19-2008, 02:36 PM
Hi,

you will use the e4x notation, it samplified the work..

for your question the code will be like this:

var str:String = '<productsSet><productLink>
<sku>BTCD.BTBB/BTBB</sku><gender>BOY</gender><skin>LIGHT</skin><hair>BLOND</hair></productLink><productLink>
<sku>BTCD.BTBG/BTBG</sku><gender>GIRL</gender><skin>LIGHT</skin><hair>BLOND</hair></productLink></productsSet>';
var xml:XML = new XML(str);

var productLink:Object = xml.productLink.(gender == 'GIRL' && skin == 'LIGHT' && hair == 'BLOND');
var sku:Object = productLink.sku;

trace("sku=" + sku ); // outpu : -->BTCD.BTBG/BTBG

Regards,
Adil

dimpiax
03-19-2008, 04:36 PM
This more clever =)

var xml : XML = <productsSet>
<block>
<category>A.</category>
<gender>Male</gender>
<skin>White</skin>
</block>
<block>
<category>B.</category>
<gender>Female</gender>
<skin>Black</skin>
</block>
</productsSet>;

var block : XMLList = xml['block'].(gender == 'Male' && skin == 'White');
trace(block['category']);