var xmldoc = xmlparse(arguments.XML);
if (isdefined('arguments.XPath')) {
topnodes = XMLSearch(xmldoc,arguments.xpath);
} else {
topnodes = xmldoc.XmlRoot.XmlChildren;
}
/* return empty query if there are no childnodes */
if (arraylen(topnodes) eq 0) {
xquery = querynew('noresults');
return xquery;
break;
}
currentelement = topnodes[1].XMLName;
columns = currentelement;
// get attribute names
listofattributes = arraytolist(StructKeyArray(topnodes[1].XMLAttributes));
for (j = 1; j LTE listlen(listofattributes); j = j + 1) {
attributename = currentelement & '_' & listgetat(listofattributes,j);
if (listfindnocase(columns,attributename) eq 0) {
columns = listappend(columns,attributename);
}
}
// get child element names
arrayofchildren = topnodes[1].XMLChildren;
for (k = 1; k LTE arraylen(arrayofchildren); k = k + 1) {
childname = currentelement & '_' & arrayofchildren[k].XMLName;
if (listfindnocase(columns,childname) eq 0) {
columns = listappend(columns,childname);
}
}
// add rows to the recordset
xquery = querynew(columns);
for (i = 1; i LTE arraylen(topnodes); i = i + 1) {
// first set the element value
queryaddrow(xquery);
currentelement = topnodes[i].XMLName;
currentelementvalue = topnodes[i].XMLText;
QuerySetCell(xquery, currentelement, currentelementvalue);
// now all the attributes
structofattributes = topnodes[i].XMLAttributes;
for (j in structofattributes) {
attributename = currentelement & '_' & j;
if (listfindnocase(columns,attributename)) {
QuerySetCell(xquery, attributename,structFind(structofattributes, j));
}
}
// now all the child elements (probably not necessary for rationalmedia)
arrayofchildren = topnodes[i].XMLChildren;
for (k = 1; k LTE arraylen(arrayofchildren);k = k + 1) {
childname = currentelement & '_' & arrayofchildren[k].XMLName;
QuerySetCell(xquery, childname,arrayofchildren[k].XMLText);
}
}