Seite 1 von 1

XML2Treegadget

Verfasst: 10.11.2005 13:17
von Kiffi
Hallo,

hier ist eine kleiner Code, der demonstriert, wie man eine XML-Datei
direkt in einem TreeGadget anzeigen kann.

Wichtig: Dieser Code benötigt die MSXML3-Library aus dem PBOSL-Paket

Grüße ... Kiffi

Code: Alles auswählen

Enumeration
  #frmMain
  #TG
EndEnumeration

Procedure XML2TreeGadget(oDoc.l)
  
  Static NodeCounter.l
  
  If MSXML3_HasChildNodes(oDoc)
    oNodeList = MSXML3_GetChildNodes(oDoc)
    If oNodeList
      For lCounterN = 0 To MSXML3_NodeListGetLength(oNodeList)
        oNode = MSXML3_NodeListGetItem(oNodeList, lCounterN)
        If oNode
          If MSXML3_GetNodeName(oNode) = "#text"
            NodeText$ = "Text: '" + MSXML3_GetText(oNode) + "'"
          Else
            NodeText$ = "<" + MSXML3_GetNodeName(oNode) 
            oAttributeList = MSXML3_GetAttributes(oNode)
            If oAttributeList
              For lCounterA = 0 To MSXML3_AttributesGetLength(oAttributeList)
                oAttribute = MSXML3_AttributesGetItem(oAttributeList, lCounterA)
                If oAttribute
                  NodeText$ + " " + MSXML3_GetNodeName(oAttribute) + "=" + Chr(34) + MSXML3_AttributesGetText(oAttribute) + Chr(34)
                  MSXML3_ReleaseObject(oAttribute)
                EndIf
              Next lCounterA
              MSXML3_ReleaseObject(oAttributeList)
            EndIf
            NodeText$ + ">"
          EndIf
          
          AddGadgetItem(#TG, -1, NodeText$)
          
          NodeCounter + 1
          
          If NodeCounter > 50 
            DOEVENTS()
            NodeCounter = 0
          EndIf
          
          If MSXML3_HasChildNodes(oNode) 
            OpenTreeGadgetNode(#TG)
            XML2TreeGadget(oNode)
            CloseTreeGadgetNode(#TG)
          EndIf            
          
          MSXML3_ReleaseObject(oNode)
          
        EndIf
      Next lCounterN
      MSXML3_ReleaseObject(oNodeList)
    EndIf
  EndIf
  
EndProcedure

If OpenWindow(#frmMain, 0, 0, 500, 500, #PB_Window_ScreenCentered | #PB_Window_SystemMenu, "XML2TreeGadget-Demo")
  
  If CreateGadgetList(WindowID(#frmMain))
    
    TreeGadget(#TG, 0, 0, 500, 500)
    
    oDoc = MSXML3_CreateDomDocument()
    
    If oDoc
      If MSXML3_Load(oDoc, "c:\test.xml")
        HideGadget(#TG, #True)
        XML2TreeGadget(oDoc)
        HideGadget(#TG, #False)
      Else
        MessageRequester("XML2TreeGadget-Demo", "Couldn't load xmlfile")
      EndIf
      MSXML3_ReleaseObject(oDoc)
    Else
      MessageRequester("XML2TreeGadget-Demo", "Couldn't create xmlobject")
    EndIf
    
    Repeat
      
      WaitWindowEvent = WaitWindowEvent()
      EventGadgetID   = EventGadgetID()
      
      Select WaitWindowEvent
        
        Case #PB_Event_Gadget
          
          Select EventGadgetID
            
          EndSelect
          
        Case #PB_Event_CloseWindow
          Quit = 1
          
      EndSelect
      
    Until Quit = 1
    
  EndIf
  
EndIf
und hier noch eine simple XML-Struktur zu Austesten:

Code: Alles auswählen

<node>This is just a simple XML-File
	<node testattribute="42" anotherattribute="hello world">
		<node>
			<node>This is not an empty node</node>
		</node>
	</node>
</node>

Verfasst: 15.11.2005 02:40
von ts-soft
Sehr nützlich :allright: , werde es mal zum Examples-Pack vom PBOSL hinzufügen, wenn ich darf ? :wink:

Verfasst: 15.11.2005 09:21
von Kiffi
> wenn ich darf ? :wink:

na klar doch. :-)

Grüße ... Kiffi

Verfasst: 01.06.2007 04:07
von ts-soft
Anpassung an PB 4.10 in Aussicht? <)

Verfasst: 01.06.2007 05:28
von Kiffi
ts-soft hat geschrieben:Anpassung an PB 4.10 in Aussicht? <)
so in etwa:

Code: Alles auswählen

EnableExplicit

Enumeration
  #frmMain
  #TG
EndEnumeration

Procedure XML2TreeGadget(Node.l, Indentation.l)
  
  Protected ChildNode.l
  Protected NodeCounter.l
  Protected TreeNodeText.s
  Protected AttributText.s
  
  For NodeCounter = 1 To XMLChildCount(Node)
    
    ChildNode = ChildXMLNode(Node, NodeCounter)
    
    AttributText=""
    If ExamineXMLAttributes(ChildNode)
      While NextXMLAttribute(ChildNode)
        AttributText + "'" + XMLAttributeName(ChildNode) + "'='" + XMLAttributeValue(ChildNode) + "' "
      Wend
    EndIf
    If AttributText = "" : AttributText = "[keine]" : EndIf
    
    TreeNodeText = " Path: '" + XMLNodePath(ChildNode) + "'"
    TreeNodeText + " Text: '" + GetXMLNodeText(ChildNode) + "'"
    TreeNodeText + " Attribut(e): " + AttributText
      
    AddGadgetItem(#TG, -1, TreeNodeText, 0, Indentation)
    
    Indentation + 1
    XML2TreeGadget(ChildNode, Indentation)
    Indentation - 1
    
  Next  
  
EndProcedure

Define.s XML
Define.l xmlObject

XML + "<node>This is just a simple XML-File"
XML + "   <node testattribute='42' anotherattribute='hello world'>"
XML + "      <node>"
XML + "         <node>This is not an empty node</node>"
XML + "      </node>"
XML + "   </node>"
XML + "   <node testattribute='23' anotherattribute='hello universe'>"
XML + "      <node>"
XML + "         <node>This is not an empty node</node>"
XML + "      </node>"
XML + "   </node>"
XML + "</node>"

If OpenWindow(#frmMain, 0, 0, 500, 200, "XML2TreeGadget-Demo", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
  
  If CreateGadgetList(WindowID(#frmMain))
    
    TreeGadget(#TG, 0, 0, 500, 500)
    
    xmlObject=CatchXML(#PB_Any, @XML, Len(XML))
    
    If xmlObject
      XML2TreeGadget(RootXMLNode(xmlObject), 0)
      FreeXML(xmlObject)
    Else
      MessageRequester("XML2TreeGadget-Demo", "Couldn't create xmlobject") 
    EndIf
    
    Repeat
    Until WaitWindowEvent()=#PB_Event_CloseWindow
    
  EndIf
  
EndIf
Grüße ... Kiffi

Verfasst: 01.06.2007 05:46
von ts-soft
Kiffi hat geschrieben:so in etwa:
Ich sitze die halbe Nacht, immer abwechselnd engl. Hilfe, dt. Hilfe und IDE
und versuche die Lib zu verstehen (nur unlesbare Zeichen und IMAs :lol: )
und Du schreibst das so mal eben um.

Falls Du noch ein paar simple XML Beispiele hast, bitte per E-Mail an mich,
ich muß das jetzt lernen :D

Gruß
Thomas

Verfasst: 01.06.2007 05:51
von Kiffi
ts-soft hat geschrieben:ich muß das jetzt lernen :D
endlich wirst Du mal einsichtig :allright:

Grüße ... Kiffi