[Gambas-user] [Gambas Bug Tracker] Bug #1105: XmlElement.GetChildrenByTagName does get parent too if tag is the same

bugtracker at ...3416... bugtracker at ...3416...
Wed May 24 12:47:31 CEST 2017


http://gambaswiki.org/bugtracker/edit?object=BUG.1105&from=L21haW4-

Daniel BLANCH BATALLER reported a new bug.

Summary
-------

XmlElement.GetChildrenByTagName does get parent too if tag is the same

Type             : Bug
Priority         : Medium
Gambas version   : 3.7
Product          : XML components


Description
-----------

http://gambaswiki.org/wiki/comp/gb.xml/xmlelement/getchildrenbytagname

As the documentation states, this should get the children, but if parent shares the same tag name as children, parent is included in position [0] of the returned XmlElement[] array.

XmlElement.GetChildrenByTagName (gb.xml)

Function GetChildrenByTagName ( TagName As String [ , Mode As Integer, Depth As Integer ] ) As XmlElement[]

Returns all the **child elements** whose tag name matches Name.
The Mode argument defines the comparison method used. It supports GB.Binary, GB.IgnoreCase and GB.Like.
See Predefined Constants for further information.
The Depth argument defines where to stop the search : a negative value will stop only at the end of the tree (default), 1 will check only the current element, 2 will check **only the direct children** of the element, and so on ...

Here is some sample code to reproduce the issue.

Thank you in advance

Daniel.

''''
' Unit test inhouse module
' TODO: Find unit test library
''''

Private errors As Integer = 0

' Fixture, add tests here
Public Sub Main()
  xmlTest()
End


' Unit test
Sub xmlTest()
    
  Dim children_level_0 As XmlElement[]
  Dim children_level_1 As XmlElement[]
      
  Dim document As XmlDocument = New XmlDocument
  document.FromString(
  "<root>" &
  "  <child>" &
  "    <child>" &
  "    </child>" &
  "    <child/>" & 
  "  </child>" &
  "</root>")
  
  children_level_0 = document.Root.GetChildrenByTagName("child", GB.Binary, 2) '2 direct children
  equals(children_level_0.Length, 1, "one child expected")
  
  children_level_1 = children_level_0[0].GetChildrenByTagName("child", GB.Binary, 2)
  equals(children_level_1.Length, 2, "two child expected")  'it returns three instead, first element is parent node
  endTests()
  
End

Sub equals(actual As Integer, expected As Integer, msg As String) 
  
  If actual = expected Then
    Print "ok    : " & msg
  Else
    errors = errors + 1
    Print "error : " & msg & " [expected =" & expected & ", actual = " & actual & "]"
  Endif
  
End

Sub endTests() 
  
  If errors > 0 Then 
    Print "Result: Failed [" & errors & "] tests"
  Else 
    Print "Result: All tests OK"
  Endif
  
End








More information about the User mailing list