2009/09/25

New Feature for modify XML with java script

Next major release will support this feature (replace the Modify element wizard) , so user can write java script to perform complex modification. A variable named node is created automatic. It represented the selected node in dialog box.
Here is a example://get id attribute from first child node named 'span'
var nid=node.span['@id'];
//above call is same as: var nid=node.getChild('span')['@id'];
//delete first child node
node.getChild('span').remove();
//insert text before node
node.insertBefore(''+nid+':'+node.getChild('span').getInnerHTML()+'');
The node object will provide lots of function(Detail can be found here). Most of get methods are W3C API like... and additional support:
  • For get child node : node.childName is the same as node.getChild('childName') unless the childName is the same as any supported function.
  • For get attribute value : node['@attrName'] is the same as node.getAttribute('attrName')
Basic rules for modification: 1) All node text/data are evaluated as original value.
For example://if node['@attr']='789'
node.setAttribute('attr','123');
//try to set @id=@attr
node.setAttribute('id',node['@attr']);
//node['@id'] will be '789' not '123'
2) If a node is modified several times, high priority modification will overwrite others.
For example://remove a node
node.remove();
//below call will not take effect, because remove() conflict with setOuterHTML();
node.setOuterHTML('test');
If you have any suggesion, please feedback.

No comments:

Post a Comment