I imagine this is really easy, but I can't seem to figure things out. I'm using code from Chapter 11 of Professional ASP 3.0 to build this page. What I'm trying to do is build SQL statements from an XML file, but one pieces of the data that I need to be put in the statements is an attribute value of the node's parent.
I also want to know how I can determine the type of data, if it's string or integer, so that I can properly build the SQL statements.
Here's the code as is:
Code:
<SCRIPT LANGUAGE="JScript">
function parseXML()
{
// check there were no parsing errors
var domXMLData = dsoData;
if (domXMLData.parseError.errorCode != 0)
{
alert('Invalid XML file: ' + domXMLData.parseError.reason);
return;
}
// traverse the nodes
txtData.innerHTML = showChildNodes(domXMLData, 0);
}
function showChildNodes(nodNode, intLevel)
{
var strNodes = '';
var intCount = 0;
var intNode = 0;
var nodAttrList;
// check there are some attributes
nodAttrList = nodNode.attributes;
if (nodAttrList != null)
{
intCount = nodAttrList.length;
if (intCount > 0)
{
strNodes += 'INSERT INTO Members (';
// for each attribute, display the attribute information
for (intAttr = 0; intAttr < intCount; intAttr++)
strNodes += nodAttrList(intAttr).nodeName + ',';
strNodes += ')';
strNodes = strNodes.replace(",)",")");
strNodes += ' VALUES (';
// for each attribute, display the attribute information
for (intAttr = 0; intAttr < intCount; intAttr++)
strNodes += nodAttrList(intAttr).nodeValue + ',';
strNodes += ')';
strNodes = strNodes.replace(",)",")");
strNodes += '<br>';
}
}
// check for any child nodes
intCount = nodNode.childNodes.length;
if (intCount > 0)
// for each child node, display the node, attributes and its child node information
for (intNode = 0; intNode < intCount; intNode++)
strNodes += showChildNodes(nodNode.childNodes(intNode), intLevel + 1);
return strNodes;
}
</SCRIPT>
So, I'm getting a nice little output so far that looks like this:
[code]
INSERT INTO Members (name,title,level,gender,species,xp_passed_up,num_ vassals) VALUES (Silver Dragon,Count,50,Male,Lugian,0,7)
INSERT INTO Members (name,title,level,gender,species,xp_passed_up,num_ vassals) VALUES (Mithoron,Baron,50,Male,Tumerok,165768770,7)
[code]
What I need to figure out is how to make it do this:
[code]
INSERT INTO Members (name,title,level,gender,species,xp_passed_up,num_ vassals,patron_name) VALUES ('Silver Dragon','Count',50,'Male','Lugian',0,7,null) <-- This guy is the topmost person
INSERT INTO Members (name,title,level,gender,species,xp_passed_up,num_ vassals,patron_name) VALUES ('Mithoron','Baron',50,'Male','Tumerok',165768770, 7,'Silver Dragon')
[code]
If you didn't catch it, I just added one more field, "patron_name", to the list of INSERT fields and need the "name" attribute from the parent passed on to the child record so it knows what to put in that field.