XML 的其他使用

【URLLoader.dataFormat的三种类型】

URLLoader.dataFormat的可选值是:

flash.net.URLLoaderDataFormat.BINARY

flash.net.URLLoaderDataFormat.TEXT

flash.net.URLLoaderDataFormat.VARIABLES

当dataFormat是BINARY时,URLLoader.data属性就是flash.utils.ByteArray类型;

当dataFormat是TEXT时,URLLoader.data属性就是String类型;

当dataFormat是VARIABLES时,uRLLoader.data属性就是Object类型.

 
【在XML内使用动态数据】

把变量放在大括号({和})内:

var userName:String = "Youthoy";

var myXML:XML = <person><userName>{userName}</userName></person>;

 
【新增一个节点元素】

//例子一:

var example:XML = <person/>;

example.father = <father/>; //内容属性和元素名称不一定要相同,也可以: example.abc = <father/>,但会以元素名称为准,即会建立一个<father/>元素

trace(example);

/*

输出:

<person>

  <father/>

</person>

*/

//例子二:

var example:XML = <person/>;

example.father = ""; //建立一个<father/>元素

trace(example.toString());

/*

输出:

<person>

  <father/>

</person>

*/

//例子三:

var example:XML = <person/>;

var id:int = 10;

example["user" + id] = "";

trace(example);

/*

输出:

<person>

  <user10/>

</person>

注意: 有些情况必须使用第三种情况.例如,元素节点名称内含连字符("-")是完全合法的,但在ActionScript里会得到编译器错误:

example.some-element = ""; //产生编译器错误

正确的写法是:

example["some-element"] = "";

*/

 
【新增属性】

var example:XML = <person/>;

example.element = "";

example.element.@name = "Youthoy";

example.element.@["bad-variable-name"] = "yes";

example.element.@other = ["riahome.cn", undefined, true, 44, null];

var id:int = 44;

example.element.@["user" + id] = "love";

trace(example);

/*

输出:

<person>

  <element name="Youthoy" bad-variable-name="yes" other="riahome.cn,,true,44," user44="love"/>

</person>

*/

 
【向XML插入文字元素和节点元素】

//在指定元素前和后插入,在最前端和尾端插入

var example:XML = <person/>;

example.two = "";

example.insertChildBefore(example.two, <one/>); //在two节点前插入one节点

example.insertChildBefore(example.two, "在two节点前插入文字");

example.insertChildAfter(example.two, <three/>); //在two节点后插入three节点

example.prependChild(<start/>); //在最顶端插入start节点

example.appendChild(<end/>); //在尾端插入end节点

example.start = "start内容"; //向节点增加内容

trace(example);

/*

输出:

<person>

  <start>start内容</start>

  <one/>

  在two节点前插入文字

  <two/>

  <three/>

  <end/>

</person>

*/

 
【把XML实例的内容属性转换为任何可以转换成字符串的内容】

//先把等号右边转为可以转换的字符串

var example:XML = <person/>;

example.one = "one";

example.two = new URLLoader();

example.three = new Sprite();

example.four = new Boolean; //Boolean对象的值初始化为false

example.five = new Number();

example.six = new String();

example.seven = {a:"Youthoy", b:true, c:undefined};

example.eight = ["three", undefined, true, 44, null];

/*

输出:

<person>

  <one>one</one>

  <two>[object URLLoader]</two>

  <three>[object Sprite]</three>

  <four>false</four>

  <five>0</five>

  <six/>

  <seven>[object Object]</seven>

  <eight>three,,true,44,</eight>

</person>

*/

 
【对XML属性层层递归的算法】

var example:XML = <menu>

                    <menuitem label="File">

                      <menuitem label="New"/>

                      <menuitem label="Save"/>

                    </menuitem>

                    <menuitem label="Help">

                      <menuitem label="About"/>

                    </menuitem>

                  </menu>;

walk(example);

function walk(node:XML):void {

for each(var element:XML in node.elements()) {

    trace(element.@label);

    walk(element);

}

 
【读取文字节点及其值】

var example:XML = <menu>

                    My name is Youthoy.

                    <myName>Youthoy</myName>

                  </menu>;

example.text(); //输出: My name is Youthoy.

example.myName.toXMLString(); //输出: <myName>Youthoy</myName>

//以下三行都输出: Youthoy

example.myName.toString();

example.myName.text();

example.myName;

 
【递归元素属性时读取属性名与其值】

var example:XML = <person name="Youthoy" age="21"/>;

    for each(var i:XML in example.attributes()) {

    trace(i.name() + "=" + i);

}

/*

输出:

name=Youthoy

age=21

*/

另类方法:

var example:XML = <person name="Youthoy" age="21"/>;

trace(example.@*[1]); //输出: 21

 
【不论层级地访问整个XML中指定的节点或属性】

使用后代存取器运算符..

例子1(对于节点元素):

var example:XML = <a>

                    <z>I am z.</z>

                    <b>

                     <c></c>

                     <z>I am z.</z>

                   </b>

                 </a>;

trace(example..z.text()); //输出: I am z.I am z.

例子2(对于属性):

var example:XML = <person>

                    <a>

                      <item name="Youthoy" age="21"/>

                    </a>

                    <item name="Jimpa" age="21"/>

                  </person>

trace(example..@name); //输出: YouthoyJimpa

你可能感兴趣的:(xml)