blondebier
Programmer
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
Another train of thought was to use recursion:
PSEUDO:
function processElement(Element e) {
doSomethingWith(e);
for each child c of e {
processElement(c)
}
}
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
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
Another train of thought was to use recursion:
PSEUDO:
function processElement(Element e) {
doSomethingWith(e);
for each child c of e {
processElement(c)
}
}
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