日期:2014-05-20  浏览次数:20695 次

flex中xmlist节点的操作
xmllist如下:
<node lable="mailbox">
  <node lable="AA" jb="A">
  <node lable="AA" jb="B" ntype="01"/>
  //如何实现在此处添加节点<node lable="AA" jb="B" ntype="02"/>
  </node>
  <node lable="BB" jb="A">
  <node lable="BB" jb="B" ntype="01"/>
  </node>
  <node lable="CC" jb="A">
  <node lable="CC" jb="B" ntype="01"/>
  </node>
 </node>
 
 如何实现对特定节点后添加节点,编辑节点和删除特定节点

------解决方案--------------------
自己写一个类
public class...{...

private var xmllist:XMLList;//定义XMLLIST
/**Insert
* 在子节点名称为nodeName的下面插入一行XML。如果nodeName=null,则在XMLLIST文档末尾增加
* nodeName 节点名称
* attributeName 属性名称
* attributeValue 属性值
*/
public function InsertToXmlList(xmlValue:String,nodeName:String=null):Object
{
if(nodeName==null)
{
var strXmllist:String = xmllist.toString();
strXmllist += "\r" + xmlValue;
xmllist = strXmllist as XMLList;
return strXmllist;
}
else
{
for each(var xml:XML in xmllist)
{
insertForeach(xml,xmlValue,nodeName);
}
}
return xmllist;
}
private function insertForeach(xml:XML,xmlValue:String,nodeName:String=null):void
{
if(xml.name() == nodeName){
xml.appendChild(xmlValue);
}
if(xml.hasComplexContent()){
var xmllistNew:XMLList = xml.children();
for each(var xmlNew:XML in xmllistNew){
insertForeach(xmlNew,xmlValue,nodeName);
}
}
}

/**Update
* 在子节点名称为nodeName的下面修改一行XML。如果nodeName=null,则修改整个XMLLIST中对应的xml属性值
* nodeName 节点名称
* attributeName 属性名称
* attributeValue 属性值
*/
public function updateXmlList(attributeName:String,attributeValue:String,nid:int,nodeName:String=null):XMLList
{
for each(var xml:XML in xmllist)
{
if(xml.name() == nodeName){
xml.@[attributeName] = attributeValue;
}
updateForeach(xml,attributeName,attributeValue,nid,nodeName);
}
return xmllist;
}
private function updateForeach(xml:XML,attributeName:String,attributeValue:String,nid:int,nodeName:String=null):void
{
if(nodeName!=null){
if(xml.name() == nodeName){
xml.@[attributeName] = attributeValue;
}
if(xml.hasComplexContent()){
var xmllistU:XMLList = xml.children();
for each(var xmlU:XML in xmllistU){
updateForeach(xmlU,attributeName,attributeValue,nid,nodeName);
}
}
}
else
{
xml.@[attributeName] = attributeValue;
if(xml.hasComplexContent()){
var xmllistUN:XMLList = xml.children();
for each(var xmlUN:XML in xmllistUN){
updateForeach(xmlUN,attributeName,attributeValue,nid,nodeName);
}
}
}
}

/**Delete
* 删除XMLLIST中的一项或者多项,nodeName为空时,查找所有合适条件的XML项目,并删除。attributeName和attributeValue
* 为空时,删除整个节点下所有XML。如果该方法不传参数,则删除整个XMLLIST
* nodeName 节点名称
* attributeName 属性名称
* attributeValue 属性值