Procházet zdrojové kódy

Updated tinyxml demo.

Mark Sibly před 9 roky
rodič
revize
6c4074cc7b
1 změnil soubory, kde provedl 78 přidání a 30 odebrání
  1. 78 30
      bananas/tinyxml2/tinyxml2_test.monkey2

+ 78 - 30
bananas/tinyxml2/tinyxml2_test.monkey2

@@ -1,46 +1,94 @@
 
-#Import "<tinyxml2>"
-#Import "<std>"
-
 #Import "dream.xml"
 
+#Import "<std>"
+#Import "<mojo>"
+#import "<mojox>"
+#Import "<tinyxml2>"
+ 
 Using std..
+Using mojo..
+Using mojox..
 Using tinyxml2..
-
-Function Dump( node:XMLNode,indent:String )
-
-	Print indent+node.Value()
-	
-	Local child:=node.FirstChild()
+ 
+Function Main()
+ 
+	New AppInstance
 	
-	While child
+	New MyWindow()
 	
-		Dump( child,indent+"  " )
-		
-		child=child.NextSibling()
-	Wend
-
+	App.Run()
 End
-
-Function Main()
-
-	Local xml:=LoadString( "asset::dream.xml" )
-	
-	Local doc:=New XMLDocument
+ 
+Class MyWindow Extends Window
+ 
+	Field _tree:TreeView
+ 
+	Method New()
 	
-	If doc.Parse( xml )<>XMLError.XML_SUCCESS
-		Print "Failed to parse"
-		Return
-	Endif
+		Super.New("XML TreeView Test", 640, 480, WindowFlags.Resizable)
+ 
+		Local xml := LoadString( "asset::dream.xml" )
+		
+		Local doc := New XMLDocument()
+		
+		If doc.Parse(xml) <> XMLError.XML_SUCCESS
+			Print "Failed to parse embedded XML!"
+			Return
+		Endif
+				
+		Local _tree := New TreeView
+ 
+		_tree.RootNode.Text = "XML document"
+ 
+		AddXMLNodeToTree(doc, _tree.RootNode)
+		
+		ContentView = _tree
+		
+		doc.Destroy()
+	End
+ 
+	Method AddXMLNodeToTree(xmlNode:XMLNode, parent:TreeView.Node)
 	
-	Print "Parsed!"
+		Local str := ""
 	
-	doc.PrintDocument()
+		Local xmlElement := xmlNode.ToElement()
+		
+		If xmlElement
+		
+			str += "<" + xmlNode.Value()
+			
+			Local attrib := xmlElement.FirstAttribute()
+			While attrib 
+				str += " " + attrib.Name() + "=~q" + attrib.Value() + "~q "
+				attrib=attrib.NextAttribute()
+			wend
+			
+			str += ">"
+		Else
+			str += xmlNode.Value()
+		Endif
+ 
+		Local treeNode:TreeView.Node
 	
-'	Dump( doc,"" )	'Their's looks better!
+		If str
+			treeNode = New TreeView.Node(str, parent)
+		Endif
+		
+		Local xmlChild := xmlNode.FirstChild()
 	
-	doc.Destroy()
+		While xmlChild
+		
+			If Not xmlChild.NoChildren()
+				If treeNode Then parent = treeNode
+			Endif
+		
+			AddXMLNodeToTree(xmlChild, parent)
+			
+			xmlChild = xmlChild.NextSibling()
 	
-	Print "Bye!"
+		Wend
 	
+	End
+ 
 End