[Gambas-user] XML - Display structure

Hans Lehmann hans at ...3219...
Fri Aug 11 14:25:33 CEST 2017


Hello,

I am unable to read and display the structure of the following XML file 
/xml.xml/:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Link: https://wiki.selfhtml.org/wiki/XML/Regeln/Baumstruktur -->
<E1>
  <E2 ATTR1="Berlin" ATTR2="Paris">
   <E3>
     <E4 ATTR3="w">Anna</E4>Text1 <E5 ATTR4="m">Paul</E5>Text2
   </E3>
  </E2>
</E1>


Note: Indents for the display - for example, in a textarea - can be 
safely read from the Node.Depth property.
The result should look like this:

COMMENT
RELEMENT: E1
   ELEMENT: E2
     ATTRIBUTE: ATTR1
         VALUE: Berlin
     ATTRIBUTE: ATTR2
         VALUE: Paris
     ELEMENT: E3
       ELEMENT: E4
         ATTRIBUTE: ATTR3
             VALUE: w
              TEXT: Anna
       /ELEMENT: E4
     TEXT: Text1
       ELEMENT: E5
         ATTRIBUTE: ATTR4
             VALUE: m
              TEXT: Paul
       /ELEMENT: E5
     TEXT: Text1 Text2
     /ELEMENT: E3
   /ELEMENT: E2
/ELEMENT: E1

Does anyone have an idea or suggestion or even a finished solution for 
the above mentioned task? My projects with XmlDocument and XmlReader do 
not display the structure of the xml.xml file correctly. But: Other XML 
files are displayed correctly. We are looking for a (general) solution 
for any XML files.

Best regards

Hans

Here is the source code used:

Private Sub Xml2TreeView(xmlDoc As XmlDocument, hTreeView As TreeView)
' Root-Element
   hTreeView.Clear()
   hTreeView.Add("0", xmlDoc.Root.TagName)
   AddChildren(1, xmlDoc.Root, 0, hTreeView)
End

Private Sub AddChildren(iIndex As Integer, xmlElement As XmlElement, 
iParent As Integer, hTreeView As TreeView) As Integer
   Dim xChild As XmlElement
   Dim S As Variant
   Dim xAttr As XmlNode
   Dim sAttr As String
   Dim elements As XmlElement[]
   Dim x As XMLElement
   Dim xVal As String

   For Each xChild In xmlElement.ChildElements
      xVal = ""
      'Extract Element-Value if the element has no children
      If (xChild.ChildElements.Count = 0) Then
         xVal = ": " & xChild.Value
         If InStr(xChild.value, gb.lf) Then
            xVal = Replace(xVal, gb.lf, " ")  'remove line feeds
            xVal = Left$(xVal, 35) & " ...."  'show an extract
         Endif
      Endif

      If xChild.Attributes.Count > 0 Then 'Include attributes if 
contained in element
       ' Collect Attributes of the element
         sAttr = ""
         For Each xAttr In xChild.Attributes
               sAttr &= xAttr.name & " = " & xAttr.Value & " "
         Next
         hTreeView.Add(Str$(iIndex), xChild.TagName & " " & xVal & " " & 
Trim(sAttr), Null, Str$(iParent))
      Else
         hTreeView.Add(Str$(iIndex), xChild.TagName & " " & xVal, Null, 
Str$(iParent))
      Endif

      iIndex = AddChildren(iIndex + 1, xChild, iIndex, hTreeView)
   Next
   Return iIndex

End

Public Sub btnStart_Click()

   Dim xmlDoc As New XmlDocument(Application.Path &/ "xml.xml")

   Xml2TreeView(xmlDoc, TreeView1)
   If TreeView1 <> Null And TreeView1.Count > 0 Then
      TreeView1.MoveFirst()
      Repeat
        TreeView1.Item.Expanded = True
      Until TreeView1.MoveBelow()
   Endif
   TreeView1.MoveBack()
   TreeView1.Item.EnsureVisible()

   txaXMLContent.Text = xmlDoc.Content

End



More information about the User mailing list