1.定位元素

1.1XML中搜索元素是根据元素名称及层次关系来确定的,如果用户知道需要的元素所在位置可以直接定位.

格式:XML对象.元素所在位置;

1.2如果用户不是很清楚的知道元素的所在位置可以用".."方式.".."表示中间过程不清楚,采用搜索的方式查找(效率比较低);

格式:XML对象..搜索的元素;

1.3XML中经常会有相同的元素名称,可以用下标来确定具体的元素.

格式:XML对象.元素[下标];


            {
                var example:XML=<items>
                        
<item>
                            
<name>Apple</name>
                            
<color>red</color>
                        
</item>
                        
<item>
                            
<name>Orange</name>
                            
<color>orange</color>
                        
</item>
                    
</items>;
                
//定位
                Alert.show("定位:" + example.item.name);
                
//搜索
                Alert.show("搜索:" + example..name);
            
/*上面俩个返回的结果都是一样
               <name>Apple</name>
               <name>Orange</name>
             * 
*/
              
//下标
                Alert.show("下标:" + example.item[0].name); 
                
//返回Apple
            }

2.读取元素内容

格式:XML对象.XML元素.toString();


            {
                var book:XML=<book>
                             
<title>Flex 程序设计</title>
                             
</book>;
                 var title:String
=book.title.toString();
                 Alert.show(title);
                 
//返回Flex 程序设计
            }

3.读取属性值

3.1使用"@"操作符读取属性值

格式:XML对象名.元素名.@属性名;

3.2使用attribute方法读取属性值

格式:XML对象名.元素名.attribute(属性名);

3.3使用attributes方法读取全部属性值:用户可以根据下标来区分同一元素下的属性

格式:XML对象名.元素名.attributes()[i];

 function ReadElementAttribute()
            {
                var fruit:XML=<fruit name="Apple" color="red" />;
                
//返回red
                Alert.show(fruit.@color);
                
//返回 Apple red
                Alert.show(fruit.@*);
                
//返回red
                Alert.show(fruit.attribute("color"));
                
//返回red
                Alert.show(fruit.attributes()[1]);
            }

4.删除元素和属性:使用delete语句可以删除XML中的元素和属性

格式:delete XML对象.元素;

    delete XML对象.元素.@属性;

 function DeleteElement()
            {
                var example:XML=<example>
                        
<fruit color="red">Apple</fruit>
                        
<vagetable color="Green">Broccoli</vagetable>
                        
<dairy color="White">Milk</dairy>
                    
</example>;
                delete example.fruit.@color;
                delete example.dairy;
                delete example.vagetable.text()[
0];
                Alert.show(example);
                
/*返回:
                   <example>
                   <fruit>Apple</fruit>
                   <vagetable color="Green"/>
                   </example>
                 * 
*/
                var example2:XML
=<example>
                        
<fruit color="red">Apple</fruit>
                        
<vagetable color="Green">Broccoli</vagetable>
                        
<dairy color="White">Milk</dairy>
                    
</example>;
                var attributes:XMLList
=example2.fruit.@*;
                
for (var i:int=attributes.length() - 1; i >= 0; i--)
                {
                    delete attributes[i];
                }
                Alert.show(example2);
            
/*返回:
             *
               <example>
               <fruit>Apple</fruit>
               <vagetable color="Green">Broccoli</vagetable>
               <dairy color="White">Milk</dairy>
               </example>
             
*/
            }

5.加载XML文件

1.创建指向外部XML文件的URLRequest对象:该对象在falsh.net包里.

格式:var URLRequest对象:URLRequest=new URLRequest(外部XML文件地址);

2.添加对"Event.COMPLETE"状态的监听.当然URLLoad对象加载XML完成后会触发"Event.COMPLETE"状态,用户需要手工添加对该状态监听.

格式:URLLoad变量名.addEventListener(Event.COMPLETE,处理函数名);

3.使用Load方法开始加载XML.

格式:URLLoad变量名.Load(URLRequest对象);

 

package Test
{
    import flash.display.*;
    import flash.events.
*;
    import flash.net.
*;
    import flash.utils.
*;

    import mx.controls.Alert;

    
public class LoadXml extends Sprite
    {

        
public function LoadXml()
        {
            Alert.show(
"1");
            var loader:URLLoader
=new URLLoader();
            loader.dataFormat
=URLLoaderDataFormat.TEXT;
            loader.addEventListener(Event.COMPLETE, handleComplete);
            Alert.show(
"2");
            loader.load(
new URLRequest("example.xml"));
            Alert.show(
"3");

        }

        
private function handleComplete(event:Event):void
        {
            
try
            {
                Alert.show(
"4");
                var example:XML
=new XML(event.target.data);
                Alert.show(example);
            }
            
catch (e:TypeError)
            {

            }
        }
    }
}

相关文章: