tinyxml2_test.monkey2 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #Import "dream.xml"
  2. #Import "<std>"
  3. #Import "<mojo>"
  4. #import "<mojox>"
  5. #Import "<tinyxml2>"
  6. Using std..
  7. Using mojo..
  8. Using mojox..
  9. Using tinyxml2..
  10. Function Main()
  11. New AppInstance
  12. New MyWindow()
  13. App.Run()
  14. End
  15. Class MyWindow Extends Window
  16. Field _tree:TreeView
  17. Method New()
  18. Super.New("XML TreeView Test", 640, 480, WindowFlags.Resizable)
  19. Local xml := LoadString( "asset::dream.xml" )
  20. Local doc := New XMLDocument()
  21. If doc.Parse(xml) <> XMLError.XML_SUCCESS
  22. Print "Failed to parse embedded XML!"
  23. Return
  24. Endif
  25. Local _tree := New TreeView
  26. _tree.RootNode.Text = "XML document"
  27. AddXMLNodeToTree(doc, _tree.RootNode)
  28. ContentView = _tree
  29. doc.Destroy()
  30. End
  31. Method AddXMLNodeToTree(xmlNode:XMLNode, parent:TreeView.Node)
  32. Local str := ""
  33. Local xmlElement := xmlNode.ToElement()
  34. If xmlElement
  35. str += "<" + xmlNode.Value()
  36. Local attrib := xmlElement.FirstAttribute()
  37. While attrib
  38. str += " " + attrib.Name() + "=~q" + attrib.Value() + "~q "
  39. attrib=attrib.NextAttribute()
  40. wend
  41. str += ">"
  42. Else
  43. str += xmlNode.Value()
  44. Endif
  45. Local treeNode:TreeView.Node
  46. If str
  47. treeNode = New TreeView.Node(str, parent)
  48. Endif
  49. Local xmlChild := xmlNode.FirstChild()
  50. While xmlChild
  51. If Not xmlChild.NoChildren()
  52. If treeNode Then parent = treeNode
  53. Endif
  54. AddXMLNodeToTree(xmlChild, parent)
  55. xmlChild = xmlChild.NextSibling()
  56. Wend
  57. End
  58. End