我也提出问题

保留原著,并且问题被回答或审核通过时通知你:


Tag Archives: XML

向XML新增一个节点元素

//例子一:
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"] = “”;
*/
- 作者:SWFAQ | http://swfaq.org

向XML添加属性

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>
*/
- 作者:SWFAQ | http://swfaq.org

把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>
*/
- 作者:SWFAQ | http://swfaq.org

向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>
*/
- 作者:SWFAQ | http://swfaq.org

对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);
}
- 作者:SWFAQ | http://swfaq.org

XML读取文字节点及其值

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;
- 作者:SWFAQ | http://swfaq.org

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

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
- 作者:SWFAQ | http://swfaq.org

不论层级地访问整个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
- 作者:SWFAQ | http://swfaq.org

搜索XML的高级应用

使用术语过滤.(condition),可结合正则表达式来使用.
例子:
var example:XML = <foodgroup>
<fruits>
  <fruit color=”red”>Apple</fruit>
    <fruit color=”orange”>Orange</fruit>
    <fruit color=”green”>Pear</fruit>
    <fruit color=”red”>Watermelon</fruit>
  </fruits>
  <vegetables>
    <vegetable color=”red”>Tomato</vegetable>
    <vegetable color=”brown”>Potato</vegetable>
    <vegetable color=”green”>Broccoli</vegetable>
  </vegetables>
</foodgroup>;
trace(example..*.(hasOwnProperty(“@color”) && @color == “red”));
/*
hasOwnProperty所做的检测是确保元素具有color属性,然后,有的话,就测试color属性之值是否为red.只有当条件的计算结果为true时,该元素才会加入EX4运算式所返回的XMLList.
输出:
<fruit color=”red”>Apple</fruit>
<fruit color=”red”>Watermelon</fruit>
<vegetable color=”red”>Tomato</vegetable>
*/