I just thought I would have a go at a quick and dirty script

The script below uses ECMAScript for XML (E4X) to change the xml into a XML object and the traverses the subnodes until simple nodes are found and then create a tilde separated list for simple nodes on the same level. It changes
- Code: Select all
<FirstSetOfTags>
<Person>
<Name>Prashanth</Name>
<Place>Bangalore</Place>
<Country>India</Country>
</Person>
<Person>
<Name>Meena</Name>
<Place>Bangalore</Place>
<Country>India</Country>
</Person>
</FirstSetOfTags>
<Dummy>
<Tag1>Value of Tag 1</Tag1>
<Tag2>Value of Tag 2</Tag2>
<Tag3>Value of Tag 3</Tag3>
</Dummy>
Into
- Code: Select all
Name~Place~Country
Prashanth~Bangalore~India
Meena~Bangalore~India
Tag1~Tag2~Tag3
Value of Tag 1~Value of Tag 2~Value of Tag 3
Here is the script. Some comments are added. Not much error checking are added.
- Code: Select all
// This script uses ECMAScript for XML (E4X) which is supported by the
// javascript engine in UltraEdit.
// Continue reading at: https://developer.mozilla.org/en/E4X
UltraEdit.activeDocument.selectAll(); /* Select whole xml document */
var sel = UltraEdit.activeDocument.selection; /* get selection into variable */
var oldHeader = ""; /* Only write header when it changes */
var outputBuffer = ""; /* Collect output so just writing to screen once */
var NEWLINE = "\r\n"; /* DOS newline to be used */
var SEPARATOR = "~"; /* tilde to be used as separator */
processXml(sel);
if(outputBuffer!="") UltraEdit.activeDocument.write(outputBuffer);
function processXml(xmlStr) {
var xmlNode;
try {
xmlNode = new XML ( xmlStr ); /* try converting into XML object */
}
catch (err) { /* failed - most likely missing root tag */
try {
xmlNode = new XML ("<root>"+xmlStr+"</root>"); /* add a "dummy" root */
}
catch (err2) { /* still errors - write first error to output Window */
UltraEdit.outputWindow.write("XML error: "+err.toString());
return; /* quit */
}
}
traverseSubnodes(xmlNode,1); /* run through XML tree */
}
function traverseSubnodes(xmlNode,level) {
var subNodes = xmlNode.*; /* Get subnodes */
var header = ""; /* collect headers as tag names */
var values = ""; /* collect values of tags */
for (i in subNodes) { /* run through subnodes */
if(subNodes[i].nodeKind()=="element") {
if(subNodes[i].hasComplexContent()) { /* complex - go down one level */
traverseSubnodes(subNodes[i], level + 1);
}
else {
header = header + (header==""?"":SEPARATOR) + subNodes[i].localName(); /* tag name */
values = values + (values==""?"":SEPARATOR) + subNodes[i].toString(); /* tag value */
}
}
}
if(header!="") { /* only write header when it changes */
if(header!=oldHeader) outputBuffer += header + NEWLINE;
oldHeader=header;
}
if(values!="") outputBuffer += values + NEWLINE; /* output values */
}
Have fun changing this to fit your purpose
