操纵xml数据动作脚本

时间:2015-04-28 13:36:01

标签: xml actionscript-3

下面的代码从XML列表中返回了9个摄影孩子的实例。我当时想要做的是当用户点击其中一个返回值时,它会删除所有摄影值并加载具有该子图像的用户。即,当点击运动时,将返回ann lee以及XML文件中的任何其他名称。

        var list:XMLList = xmlinfo.profile.photography;

        var totalimage:Number = list.length();
        trace("length" + totalimage);

        for (var i:int =0; i<totalimage; i++){
            trace(xmlinfo.profile.photography[i]);


    //bkg.addEventListener(MouseEvent.CLICK,gotodata);
        background = new bkg();


        background.y = i*40;
        background.x = 20;
        addChild(background);

        textField = new TextField();
        textField.text = list[i];    
        background.addChild(textField);
            }
        }

XML FILE

           
<root>
  <profile>   
      <name>ann lee</name>
      <photography>sport</photography>
      <photography>landscape</photography>
      <photography>still life</photography>           
      <image>img1.jpg</image>
  </profile> <profile>        
      <name>john</name>
      <photography>wildlife</photography>
      <photography>landscape</photography>
      <image>img2.jpg</image> </profile>  
</root>

1 个答案:

答案 0 :(得分:1)

这是无效的xml:

<root>
<profile>   
    <name>ann lee</name>
    <photography>sport</photography>
    <photography>landscape</photography>
    <photography>still life</photography>           
    <image>img1.jpg</image>
<profile>   
    <name>john</name>
    <photography>wildlife</photography>
    <photography>landscape</photography>
    <image>img2.jpg</image>
</root>

配置文件标签永远不会关闭。

关于代码,应使用局部变量而不是括号:

var background:bkg;  //you should use names with better meaning and use capital letters for class names (compare to the next line, example: var background:Background;
var textField:TextField;  

for (var i:int =0; i<totalimage; i++){
    trace(xmlinfo.profile.photography[i]);


    background.addEventListener(MouseEvent.CLICK,gotodata);

    background = new bkg();
    background.y = i*40;
    background.x = 20;
    addChild(background);

    textField = new TextField();
    textField.text = list[i];    
    background.addChild(textField);

    }
}