一个关于在Flex中解析xml文件的问题 [英] a question about parsing xml file in Flex

查看:22
本文介绍了一个关于在Flex中解析xml文件的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在面临一个小项目,我必须向用户展示一个周期表.但是,它比单纯的周期表图像文件要复杂一些.让我分解一下:

1.在同一个文件夹下,有一个xml文件,格式如下:

<元素><名称>钙</名称><abbreviation>Ca</abbreviation><排放><波长>118</波长></排放><标准><浓度>0.01</浓度><浓度>0.1</浓度></标准></元素>

它基本上只是指定应该在peoridic table中突出显示哪些元素.所以从这个xml文件中读取后,我们的peoridic table应该通过使这些元素变为黄色且可点击而让其他元素变为灰色且不可点击来反映.但只有name 属性正是我所需要的.

2.其次,只需要确保xml文件中已经指定的元素周期表元素上的元素可以被用户点击.

由于我对Flex编程有点陌生,所以现在最让我担心的有两件事.第一是如何在FLEX中进行xml解析?我在C#中进行了此解析,但不知道如何进行.其次,我应该如何描绘元素周期表?我应该让元素可点击.第三,我应该如何让元素周期表知道从xml文件中解析出来的元素,即我应该在哪里存储这些提取的元素?

听起来有点难,我只需要一些想法或一些代码来帮助我开始,谢谢!

解决方案

XML 解析在带有 E4X 的 actionscript 3 中非常容易,并且有点.

HTH,乔治

I am faced with a small project now,in which I have to present a peoridic table to the user.However,it is bit more complicated than just a pure peoridic table image file.Let me break it down a bit:

1.In the same folder,there is an xml file,which have the following format:

<constraints>
<element>
 <name>Calcium</name>
 <abbreviation>Ca</abbreviation>
 <emissions>
  <wavelength>118</wavelength>
 </emissions>
 <standards>
  <concentration>0.01</concentration>
  <concentration>0.1</concentration>
 </standards>
</element>

it basically just specifies what elements should be highlighted in the peoridic table.So after reading from this xml file,our peoridic table should be reflected by making these elements yellow and clickable while leaving others grey and not-clickable.But only the name attribute is what I needed.

2.Secondly,it just needs to make sure that the elements on the periodic table elements that have been specified by the xml file should be enabled for clicking by the user.

Since I am bit new to Flex programming,now two things concern me most.The first is how to do the xml parsing in FLEX?I did this parsing in C#,but have no idea how to do this. Secondly,how should I depict the periodic table?I should make elements clickable. Thirdly,how should I make the periodic table be aware of the elements that have been parsed out from the xml file,i.e,where should I store these extracted elements?

Sounds bit tough,I just need some ideas or maybe some code to help me start,thanks!

解决方案

XML parsing is pretty easy in actionscript 3 with E4X and somewhat similar to C#.

e.g.

var table:XML = <constraints>
<element>
 <name>Calcium</name>
 <abbreviation>Ca</abbreviation>
 <emissions>
  <wavelength>118</wavelength>
 </emissions>
 <standards>
  <concentration>0.01</concentration>
  <concentration>0.1</concentration>
 </standards>
</element>
</constraints>
trace(table.element.name);

Do checkout the E4X tutorial on the Yahoo Developer Network to get started easily. The rest should be easy enough.

For point 2 you should just loop through the element names and then enable their view/visual asset equivalent.

e.g.

for each (var element:XML in table.element.*) trace(element.name);//enable here instead of tracing

Update

I had a bit of fun with the great libspark SvgParser and the wikipedia periodic table svg. This is NOT how anyone should write code, this is just a quick and dirty test.

You can view the test here and the source here.

here's a preview:

Also displaying the C# version of parsing would help. And I found another great E4X article.

HTH, George

这篇关于一个关于在Flex中解析xml文件的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆