Iterate over XML nodes

We add more iteration methods for our XML classes in the new MBS Xojo XML Plugin for our 22.4 release. You can not just loop over normal nodes, but also over attributes and elements.

In this example we do a for each loop over attributes:

Sub IterateAttributeNodes()
	Dim doc As New XMLDocumentMBS("<doc id=""123"" value=""test""></doc>")
	
	Dim root As XMLElementMBS = doc.DocumentElement
	For Each e As XMLAttributeMBS In root.IterateAttributeNodes
		MessageBox e.Name+": "+e.Value
	Next
End Sub

And now loop over child nodes, which may include text nodes:

Sub IterateChildNodes()
	Dim doc As New XMLDocumentMBS("<doc><test>abc</test><test>def</test></doc>")
	
	Dim root As XMLElementMBS = doc.DocumentElement
	For Each e As XMLElementMBS In root.IterateChildNodes
		MessageBox e.toString
	Next
End Sub

You can loop over elements:

Sub IterateElements()
	Dim doc As New XMLDocumentMBS("<doc><test>abc</test><test>def</test></doc>")
	
	Dim root As XMLElementMBS = doc.DocumentElement
	For Each e As XMLElementMBS In root.IterateElements
		MessageBox e.toString
	Next
End Sub

We can loop over elements of a certain tag, which reduces code needed in our projects a lot:

Sub IterateElementsByTagName()
	Dim doc As New XMLDocumentMBS("<doc><other>abc</other><test>def</test></doc>")
	
	Dim root As XMLElementMBS = doc.DocumentElement
	For Each e As XMLElementMBS In root.IterateElementsByTagName("test")
		MessageBox e.toString
	Next
End Sub

Please try the new classes and let us know how you like this.

2 Likes