Copy link to clipboard
Copied
Hi im currently creating a database search using XML,PHP and AS3, I have a number of products which i need to retrive the name and product id.
It is all being added to a list in flash. The items are being added to the list but its not showing any text in the list.
Any help is greatly appreciated.
[AS]
import fl.data.DataProvider;
import flash.events.MouseEvent;
var allProdbase:Array = [];// contains all products like in xml
var currentProdbase:Array = [];// contains products that need to be displayed
var _loader:URLLoader = new URLLoader();
var _data:XML = new XML();
_loader.addEventListener(Event.COMPLETE, readXML);
_loader.load(new URLRequest("http://anonadd.com/product.xml"));
function readXML(event:Event):void
{
_data = new XML(event.target.data);
for each (var prod in _data.product)
{
allProdbase.push({id: prod.@id, name: prod.@productname});
}
currentProdbase = allProdbase.concat();
updateList();
}
function updateList():void
{
productList.dataProvider = new DataProvider ();
for (var i:int = 0; i<currentProdbase.length; i++)
{
productList.addItem({label:currentProdbase.productname , label:currentProdbase.id});
}
}
/*productList.addEventListener(MouseEvent.MOUSE_UP, traceName);
function traceName(e:MouseEvent):void
{
trace(e.target.name);
}*/
[/AS]
[XML]
<?xml version="1.0"?>
<xml><product><id>28</id><productname>HTC Touch HD</productname></product><product><id>29</id><productname>Palm Treo Pro</productname></product><product><id>30</id><productname>Canon EOS 5D</productname></product><product><id>31</id><productname>Nikon D300</productname></product><product><id>32</id><productname>iPod Touch</productname></product><product><id>33</id><productname>Samsung SyncMaster 941BW</productname></product><product><id>34</id><productname>iPod Shuffle</productname></product><product><id>35</id><productname>Product 8</productname></product><product><id>36</id><productname>iPod Nano</productname></product><product><id>40</id><productname>iPhone</productname></product><product><id>41</id><productname>iMac</productname></product><product><id>42</id><productname>Apple Cinema 30"</productname></product><product><id>43</id><productname>MacBook</productname></product><product><id>44</id><productname>MacBook Air</productname></product><product><id>45</id><productname>MacBook Pro</productname></product><product><id>46</id><productname>Sony VAIO</productname></product><product><id>47</id><productname>HP LP3065</productname></product><product><id>48</id><productname>iPod Classic</productname></product><product><id>49</id><productname>Samsung Galaxy Tab 10.1</productname></product><product><id>50</id><productname>FLASH TEST</productname></product></xml>
[/XML]
remove the @ signs from your line
allProdbase.push({id: prod.@id, name: prod.@productname});
@-sign is reserved for Attributes (<product id=""/>)
in your xml id is an element not an attribute
(<product><id/></product>)
Copy link to clipboard
Copied
Make a
var dp:DataProvider = new DataProvider();
in the beginning, then later...
function updateList():void
{
for (var i:int = 0; i<currentProdbase.length; i++)
{
dp.addItem({label:currentProdbase.productname , label:currentProdbase.id});
}
productList.dataProvider = dp;
}
Copy link to clipboard
Copied
Hi mocca thanks for the reply,
I inserted your code
[AS]
import fl.data.DataProvider;
import flash.events.MouseEvent;
var allProdbase:Array = [];// contains all products like in xml
var currentProdbase:Array = [];// contains products that need to be displayed
var _loader:URLLoader = new URLLoader();
var _data:XML = new XML();
var dp:DataProvider = new DataProvider ();
_loader.addEventListener(Event.COMPLETE, readXML);
_loader.load(new URLRequest("http://shane.hailstormcommerce.com/product.xml"));
function readXML(event:Event):void
{
_data = new XML(event.target.data);
for each (var prod in _data.product)
{
allProdbase.push({id: prod.@id, name: prod.@productname});
}
currentProdbase = allProdbase.concat();
updateList();
}
function updateList():void
{
productList.dataProvider = new DataProvider ();
for (var i:int = 0; i<currentProdbase.length; i++)
{
dp.addItem({label:currentProdbase.name , label:currentProdbase.id});
}
productList.dataProvider = dp;
}
productList.addEventListener(MouseEvent.MOUSE_UP, traceName);
function traceName(e:MouseEvent):void
{
trace(productList.length);
}
[/AS]
But its still not showing th text.
Copy link to clipboard
Copied
remove the @ signs from your line
allProdbase.push({id: prod.@id, name: prod.@productname});
@-sign is reserved for Attributes (<product id=""/>)
in your xml id is an element not an attribute
(<product><id/></product>)
Copy link to clipboard
Copied
Thanks very much Mocca
The product id is not showing though just productname.
Could you also help with this?
Copy link to clipboard
Copied
you are using the same identifier two times in one datagrid row ("label") the second one will therefore not be processed.
Write instead of:
dp.addItem({label:currentProdbase.name , label:currentProdbase.id});
something like this:
dp.addItem({prodname:currentProdbase.name , id:currentProdbase.id});
Copy link to clipboard
Copied
Just put a trace on the allProdbase array and it doesnt seem to contain the id.... So the problem must be here?
function readXML(event:Event):void
{
_data = new XML(event.target.data);
for each (var prod in _data.product)
{
allProdbase.push({id: prod.id , name: prod.productname});
}
currentProdbase = allProdbase.concat();
trace(allProdbase.length);
updateList();
}
Copy link to clipboard
Copied
Syntax check of your xml gives me errors:
iPhone</ productname> should be iPhone</productname> (without space in front of productname)
make sure you got all your tags right
Copy link to clipboard
Copied
It doesnt have any errors on my side, its created with php.
Copy link to clipboard
Copied
If you are sure your xml is valid, check why you fail to retrieve the information you want.
Basics how to extract data from valid xml files with AS3 can be found here.
Copy link to clipboard
Copied
Ok thanks for your help again mocca
Find more inspiration, events, and resources on the new Adobe Community
Explore Now