Retrieving Sub-XML Elements to display a Pie Chart
I have designed an application that will load the information from an XML File to a Pie Chart.
First my XML looked like this
<books>
<stock>
<booktype>Novels</booktype>
<amountofbooks>100</amountofbooks>
</stock>
</books>
And my AS code looked like
[Bindable]
private var bookStock:ArrayCollection = new ArrayCollection();
var myPieSeries:PieSeries = new PieSeries();
myPieSeries.nameField = "booktype";
myPieSeries.field = "amountofbooks";
in the result
event I do this
bookStock = evt.result.books.stock;
Now this works perfectly and I can see the generated Pie Chart.
But now let's say that I changed the XML in the following manner.
<books>
<stock>
<bookinfo>
<booktype>Fiction</booktype>
<amountofbooks>150</amountofbooks>
</bookinfo>
</stock>
<stock>
<bookinfo>
<booktype>Novels<开发者_运维问答/booktype>
<amountofbooks>100</amountofbooks>
</bookinfo>
</stock>
</books>
in the results event how do I access it?
bookStock = evt.result.books.stock.bookinfo;
doesn't work. I get a "Error: Unknown Property: 'bookinfo'."
When I analysed the bookStock object I get this.
How do I access the XML element now? Does anything needs to be changed here?
myPieSeries.nameField = "booktype";
myPieSeries.field = "amountofbooks";
UPDATE
Here is the project that I am working with.http://min.us/mvkoXsU
Actually xml.books.stock.bookinfo will get you an XMLList of XML nodes and I think your error comes either from assigning that XMLList to the ArrayCollection instance, or by treating every item of that XMLList as an Array, when in fact they are XML nodes.
I've never worked with ArrayCollection, but I think you'll need to traverse (for loop) your XMLList and put together the ArrayCollection manually.
The problem you have right now, is that you are treating an XMLList like XML.
// this works because there is only one XML node for each item in the chain
bookStock = evt.result.books.stock;
// how does this work?
bookStock = evt.result.books.stock.bookinfo;
// there are multiple stock nodes inside of "books"
bookStock = evt.result.books.stock[0].bookinfo;
trace(bookStock);//should trace the bookinfo from the first element
Give that a try.
精彩评论