How to remove xml tag using as3
I have exported data .xls file using as3.
var xml:ArrayCollection;
xml=Labneuron8.lastResult.Response.Terminal as ArrayCollection;
var str:String=xml[0].Value;
var xml1:XML=new XML(str);
myData=new XMLListCollection(xml1.children());
var bytes:ByteArray = new ByteArray();
bytes.writeObject(m开发者_JAVA技巧yData.children().toString());
var f:FileReference = new FileReference();
f.save(bytes,"output.xls");
while running a .xls file is created with values
<Val>0.307280</Val>
<Val>0.307280</Val>
<Val>0.307280</Val>
<Val>0.307280</Val>
<Val>0.307280</Val>
<Val>0.307280</Val> .......
Now I have to delete this Val tags, which is not required.
Please let me know script to delete this tag.
Thanks .............UPDATE................
Now I modified the code as
var xml:ArrayCollection;
xml=Labneuron8.lastResult.Response.Terminal as ArrayCollection;
var str:String=xml[0].Value;
var xml1:XML=new XML(str);
var vals:XMLList = xml1.Val;
for each(var val:XML in vals)
{
delete (val.parent().children()[val.childIndex()]);
Alert.show("hi");
}
myData=new XMLListCollection(xml1.children());
var bytes:ByteArray = new ByteArray();
bytes.writeObject(myData.children().toString());
var f:FileReference = new FileReference();
f.save(bytes,"output.xls");
But the for each loop is not working.So I get the same output as before with val tags.How this can be solved? To delete an xml nodes, first you need to find them:
var sourceXML:XML = ...
var vals:XMLList = sourceXML.Val; //enter actual path to Val nodes here
Then you delete each one using parent:
for each(var val:XML in vals)
{
delete (val.parent().children()[val.childIndex()]);
}
If all your nodes have same parent, you can cache val.parent().children() in local variable, so it will be faster.
..................UPDATE...................
@Patrick
var xml:ArrayCollection;
xml=Labneuron8.lastResult.Response.Terminal as ArrayCollection;
var str:String=xml[0].Value;
var xml1:XML=new XML(str);
xml1..Val.(parent().replace(valueOf().childIndex(),valueOf().text()))
myData=new XMLListCollection(xml1.children());
var bytes:ByteArray = new ByteArray();
bytes.writeObject(myData.children().toString());
var f:FileReference = new FileReference();
f.save(bytes,"output.xls");
...............SOLUTION ....................
var xml6:ArrayCollection;
xml6=Labneuron8.lastResult.Response.Terminal as ArrayCollection;
var str:String=xml6[0].Value;
var xml1:XML=new XML(str);
xml1..Val.(parent().replace(valueOf().childIndex(),valueOf().text()+"\n"))
myData3=new XMLListCollection(xml1.children());
var bytes:ByteArray = new ByteArray();
bytes.writeObject(myData3.children().toString());
var fo:FileReference = new FileReference();
fo.save(bytes,"output.xls");
To delete an xml nodes, first you need to find them:
var sourceXML:XML = ...
var vals:XMLList = sourceXML.Val; //enter actual path to Val nodes here
Then you delete each one using parent:
for each(var val:XML in vals)
{
delete (val.parent().children()[val.childIndex()]);
}
If all your nodes have same parent, you can cache val.parent().children() in local variable, so it will be faster.
Ok i think i have understand what you want , you don't want to delete the Val node but only replace it with it's content.
So you can do it like that using the replace function if your parent node have only one children:
in one line:
xml1..Val.(parent().replace(valueOf().childIndex(), valueOf().text()))
or with a loop:
for each (var val:XML in xml1..Val) {
val.parent().replace(val.childIndex(), val.toString())
}
Added a live example to show how the delete works: http://wonderfl.net/c/fHph
You can do it also in one line as this:
//...
var xml1:XML=new XML(str);
xml1.Val.(delete parent().children()[valueOf().childIndex()])
//...
If you want to delete all Val node wherever they are use ..
notation
//...
var xml1:XML=new XML(str);
xml1..Val.(delete parent().children()[valueOf().childIndex()])
//...
To get the internal value of xml-nodes you can (sometimes) use: nodeValue.
example:
rootNode.childNodes[a].nodeValue
This will "delete"/does not return the xml-tag. I've used it in a AS2 project, and I'm not sure if nodeValue is still accepted in AS3.
精彩评论