Skip to main content
February 6, 2013
Answered

AS3 and XML text not showing

  • February 6, 2013
  • 1 reply
  • 1026 views

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]

This topic has been closed for replies.
Correct answer moccamaximum

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

1 reply

Inspiring
February 6, 2013

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;

}

February 6, 2013

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.

moccamaximumCorrect answer
Inspiring
February 6, 2013

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