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 November 16th, 2004, 09:47 AM
Authorized User
 
Join Date: Sep 2004
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default Looping through attributes in an XML document

I'm trying to find a neat way of looping through an XML document so that I can get the attributes and the relevant values so that I can save them.

The only way i've managed to do this so far is nesting loops inside each other. One loop for each level. This seems a bit restrictive though. What if another level were to appear in the document?

Here's my code:

For Each Node In myNodeList
    If Node.HasChildNodes Then
        For Each childNode In Node
            logIt.dBug(childNode.Name)
               For Each Attribute In childNode.Attributes
                    saveAttribute(Attribute.Name, Attribute.Value)
               Next
               If childNode.HasChildNodes Then
                     For Each nextChildNode In childNode
                         logIt.dBug(nextChildNode.Name)
                             For Each Attribute In nextChildNode.Attributes
                                 saveAttribute(Attribute.Name, Attribute.Value)
                             Next
                     Next
              End If
         Next
    End If
Next

Any ideas would be greatly appreciated.

Many thanks,

Francis

 
Old November 16th, 2004, 10:03 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Sounds like you need recursion.
Pseudo code:
Code:
function saveAttribute(Name, Value)

function saveAttributes(Node)
{
  loop through attributes and call saveAttribute for each one
  if Node has children loop through children and call saveAttributes passing in each node
}

function main()
{
  get document element and call saveAttributes(documentElement)
}

main()
--

Joe (Microsoft MVP - XML)
 
Old November 16th, 2004, 10:12 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The classic way to walk a tree is with a recursive function. In pseudocode:

function processElement(Element e) {
  doSomethingWith(e);
  for each child c of e {
     processElement(c)
  }
}



Michael Kay
http://www.saxonica.com/
 
Old November 17th, 2004, 09:57 AM
Authorized User
 
Join Date: Sep 2004
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for your comments.

I have attempted a structure similar to your advice, but I am receiving a peculiar outcome.

I have a loop that goes through a nodlist:

For Each node In NodeList
   saveNodeAttributes(node)
Next node

Then setup the other function for the recursion:

Public Sub saveNodeAttributes(ByVal Node As Xml.XmlNode)
        Dim attribute As Xml.XmlAttribute
        If Not Node.Attributes Is Nothing Then
            For Each attribute In Node.Attributes
                logIt.dBug(attribute.Name)
            Next attribute
        End If

        If Node.HasChildNodes Then
            saveNodeAttributes(Node.FirstChild)
        ElseIf Not Node.NextSibling Is Nothing Then
            saveNodeAttributes(Node.NextSibling)
        ElseIf Not Node.ParentNode.NextSibling Is Nothing Then
            saveNodeAttributes(Node.ParentNode.NextSibling)
        End If
    End Sub

It checks to see if the node has any "children", if it does then select the "firstchild". If not check if there are any "siblings", if not then go back to the "parent" and choose the next sibling.

This however is outputting the 2nd half of the document's attributes twice?

Any idea what could be wrong?

Thanks,

Francis

 
Old November 17th, 2004, 11:02 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You don't need to worry about siblings and parents.



--

Joe (Microsoft MVP - XML)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Looping through XML li72 XSLT 2 February 20th, 2008 12:13 PM
copying xml attributes golddog XSLT 1 September 12th, 2007 01:05 PM
Retrieving xml attributes christianhau Classic ASP XML 0 July 19th, 2007 06:25 AM
XML to XML, checking attributes raoulvb XSLT 4 December 9th, 2004 10:15 AM
XML attributes miguel.ossa C# 2 February 9th, 2004 07:59 AM





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