package includes
{
import flash.xml.XMLDocument;
import flash.xml.XMLNode;
import mx.controls.Alert;
import mx.rpc.xml.SimpleXMLEncoder;
public class ExtendXmlList
{
private var xmllist:XMLList;//定义一个XMLLIST来进行增删改查
private var strXmllist:String = "";
/**构造方法
*允许有3种构造方法,每次传入3个对象 XMLList,Object,XML,对象可以为null,但如果所有对象为空就会抛出异常
*/
public function ExtendXmlList(xl:XMLList=null,obj:Object=null,xml:XML=null)
{
if (xl != null)
{
xmllist=xl;
return;
}
else if (obj != null)
{
var qName:QName=new QName("root");
var xmlDocument:XMLDocument=new XMLDocument();
var simpleXMLEncoder:SimpleXMLEncoder=new SimpleXMLEncoder(xmlDocument);
var xmlNode:XMLNode=simpleXMLEncoder.encodeValue(obj, qName, xmlDocument);
xmllist=new XMLList(xmlDocument.toString());
return;
}
else if (xml != null)
{
xmllist=xml.elements();
return;
}
throw new ExtendXmlListException("传入的参数不能都为空。")
}
/**Select
* 查找XMLLIST中子节点名称为nodeName,属性名称为attributeName,属性值为attributeValue的所有XML。
* 当nodeName=null时,其他不能为空,根据attributeName和value查找整个XMLLIST。
* 当attributeName和attributeValue=null时,nodeName不能为""和null,根据nodeName反回节点下所有XML
* nodeName 节点名称
* attributeName 属性名称
* attributeValue 属性值
*/
public function queryXmlList(nodeName:String=null,attributeName:String=null,attributeValue:String=null):XMLList
{
if(nodeName==null)
{
if(attributeName==null||attributeValue==null||attributeName==""){
throw new ExtendXmlListException("当节点名称为Null时,属性名称不能为null或者空字符串,值不能为null")
}
}
if(attributeName==null||attributeValue==null||attributeName=="")
{
if(nodeName==null&&nodeName==""){
throw new ExtendXmlListException("属性名或值为null或空字符串时,节点名称不能为null或者空字符串。")
}
}
if(nodeName!=null)
{
if(attributeName==null || attributeValue==null)
{
for each(var xml:XML in xmllist)
{
if(xml.name()==nodeName){
strXmllist += xml.toXMLString()+"\n";
}
if(xml.hasComplexContent()){
queryForeach(xml,nodeName,attributeName,attributeValue);
}
}
}
else
{
for each(var xmlq:XML in xmllist)
{
if(xmlq.name() == nodeName && xmlq.@[attributeName]==attributeValue)
{
strXmllist += xmlq.toXMLString()+"\n";
}
if(xmlq.hasComplexContent()){
queryForeach(xmlq,nodeName,attributeName,attributeValue);
}
}
}
var qName:QName = new QName("root");
var xmlDocument:XMLDocument = new XMLDocument();
var simpleXMLEncoder:SimpleXMLEncoder = new SimpleXMLEncoder(xmlDocument);
var xmlNode:XMLNode = simpleXMLEncoder.encodeValue(strXmllist, qName, xmlDocument);
xmllist = new XMLList(xmlDocument.toString());
}
return xmllist;
}
/**递归查询节点
* nodeName 节点名称
* attributeName 属性名称
* attributeValue 属性值
*/
private function queryForeach(xml:XML,nodeName:String=null,attributeName:String=null,attributeValue:String=null):void
{
if(attributeName==null || attributeValue==null)
{
var xmll:XMLList = xml.children();
for each(var xmlNew:XML in xmll){
if(xmlNew.name()==nodeName){
strXmllist+=xmlNew.toXMLString() + "\n";
trace(xmlNew);
}
if(xmlNew.hasComplexContent()){
queryForeach(xmlNew,nodeName,attributeName,attributeValue);
}
}
}
else
{
var newXmllistQ:XMLList = xml.children();
for each(var newXmlQ:XML in newXmllistQ){
if(newXmlQ.name() == nodeName && newXmlQ.@[attributeName]==attributeValue){
strXmllist+=newXmlQ.toXMLString() + "\n";
}
if(newXmlQ.hasComplexContent()){
queryForeach(newXmlQ,nodeName,attributeName,attributeValue);
}
}
}
}
/**Insert
* 在子节点名称为nodeName的下面插入一行XML。如果nodeName=null,则在XMLLIST文档末尾增加
* xmlValue 传入的XML值
* nodeName 节点名称
*/
public function InsertToXmlList(xmlValue:String,nodeName:String=null):XMLList
{
if(xmlValue==null && xmlValue=="")
{
throw new ExtendXmlListException("传入的XML不能为空值。")
}
if(nodeName==null||nodeName=="")
{
var strXmllist:String = xmllist.toString();
strXmllist += "\r" + xmlValue;
var qName:QName = new QName("root");
var xmlDocument:XMLDocument = new XMLDocument();
var simpleXMLEncoder:SimpleXMLEncoder = new SimpleXMLEncoder(xmlDocument);
var xmlNode:XMLNode = simpleXMLEncoder.encodeValue(strXmllist, qName, xmlDocument);
xmllist = new XMLList(xmlDocument.toString());
return xmllist;
}
else
{
for each(var xmlT:XML in xmllist)
{
insertForeach(xmlT,xmlValue,nodeName);
}
}
return xmllist;
}
/**递归添加节点
* nodeName 节点名称
* xml 用于循环插入的XML
* xmlValue 传入的XML值
*/
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 属性值
*/
// "Keyproject","能力提升","修改后的值","items");
public function updateXmlList(attributeName:String,oldValue:String,attributeValue:String,nodeName:String=null):XMLList
{
if(nodeName==null||nodeName=="")
{
if(attributeName==null||attributeName==""||attributeName==null){
throw new ExtendXmlListException("节点名称为空时,属性名称不能为null或空字符,属性值不能为null。")
}
}
if(attributeName==null||attributeName==""||attributeValue==null){
throw new ExtendXmlListException("属性名称不能为null或空字符,属性值不能为null。")
}
for each(var xml:XML in xmllist)
{
updateForeach(xml,attributeName,oldValue,attributeValue,nodeName);
}
return xmllist;
}
/**递归查询并且修改节点
* 在子节点名称为nodeName的下面修改一行XML。如果nodeName=null,则修改整个XMLLIST中对应的xml属性值
* XML:用与递归的XML
* nodeName 节点名称
* attributeName 属性名称
* attributeValue 属性值
*/
private function updateForeach(xml:XML,attributeName:String,oldValue:String,attributeValue:String,nodeName:String=null):void
{
if(nodeName!=null||nodeName==""){
if(xml.name()==nodeName){
var xmllistU:XMLList = xml.children();
for each(var xmlUp:XML in xmllistU){
if(xmlUp.@[attributeName]==oldValue){
xmlUp.@[attributeName]=attributeValue;
}
}
}
else{
var xmllistUT:XMLList = xml.children();
for each(var xmlUpT:XML in xmllistUT){
updateForeach(xmlUpT,attributeName,oldValue,attributeValue,nodeName);
}
}
}
else //节点名称为null,修改attributeName=oldValue 为attributeName=attributeValue
{
if(xml.@[attributeName]==oldValue){
xml.@[attributeName]=attributeValue;
}
if(xml.hasComplexContent()){
var xmllistUN:XMLList = xml.children();
for each(var xmlUN:XML in xmllistUN){
updateForeach(xmlUN,attributeName,oldValue,attributeValue,nodeName);
}
}
}
}
/**Delete
* 删除XMLLIST中的一项或者多项,nodeName为空时,查找所有合适条件的XML项目,并删除。attributeName和attributeValue
* 为空时,删除整个节点下所有XML。如果该方法不传参数,则删除整个XMLLIST
* nodeName 节点名称
* attributeName 属性名称
* attributeValue 属性值
*/
public function deleteFromXmlList(attributeName:String=null,attributeValue:String=null,nodeName:String=null):XMLList
{
if(attributeName==null&&attributeValue==null&&nodeName==null) //删除整个XMLLIST
{
var num:int = xmllist.length()-1;
for(var i:int=num;i>=0;i--)
{
delete xmllist[i];
var xmll:XMLList = xmllist;
}
}
else
{
var numT:int = xmllist.length()-1;
for(var j:int=numT;j>=0;j--)
{
var xml:XML = xmllist[j] as XML;
if(attributeName==null&&attributeValue==null&&nodeName!=null)
{
if(xml.name()==nodeName){ //根据 节点名 删除
delete xmllist[j];
}
}
else if(nodeName==null&&attributeName!=null&&attributeValue!=null) //根据 属性与值 删除
{
if(xml.@[attributeName] == attributeValue){
delete xmllist[j];
}
}
else
{ //根据 节点名 属性与值 删除
if(xml.name()==nodeName && xml.@[attributeName] == attributeValue){
delete xmllist[j];
}
}
if(xml.hasComplexContent()){
deleteForeach(xml,attributeName,attributeValue,nodeName);
}
}
}
return xmllist;
}
private function deleteForeach(xml:XML,attributeName:String=null,attributeValue:String=null,nodeName:String=null):void
{
var xmllistD:XMLList = xml.children();
var num:int = xmllistD.length()-1;
for(var i:int=num;i>=0;i--)
{
var xmlNew:XML = xmllistD[i] as XML;
if(attributeName==null&&attributeValue==null&&nodeName!=null) //根据 节点名 删除
{
if(xmlNew.name()==nodeName){
delete xmllistD[i];
}
}
else if(nodeName==null&&attributeName!=null&&attributeValue!=null) //根据 属性与值 删除
{
if(xmlNew.@[attributeName] == attributeValue){
delete xmllistD[i];
}
}
else
{ //根据 节点名 属性与值 删除
if(xmlNew.name()==nodeName && xmlNew.@[attributeName] == attributeValue){
delete xmllistD[i];
}
}
if(xmlNew.hasComplexContent()){
deleteForeach(xml,attributeName,attributeValue,nodeName);
}
}
}
}
}