Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
|
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 19th, 2003, 07:08 PM
Registered User
 
Join Date: Jun 2003
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default passing attribute info while traversing nodes

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.
 
Old June 20th, 2003, 07:42 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Ashley,

Each node has a "parentNode" attribute which you can use to get at the attribute you want. OTMH I think the syntax goes like this:

nodNode.parentNode.attributes.getNamedItem("name") .text

hth
Phil





Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorting some nodes by attribute value acw274 XSLT 8 July 2nd, 2008 01:22 AM
How can I go through all child nodes & attribute.. vishnu108mishra C# 1 November 13th, 2007 05:00 AM
Comparing nodes attribute(Michael Kay help meeee) sumapathy XSLT 4 August 25th, 2007 03:21 AM
XPath - Selecting nodes based on attribute values billy_bob_the_3rd XML 4 December 1st, 2004 06:12 PM
Passing logon info sandeep Crystal Reports 1 October 6th, 2004 08:41 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.