MegaXMLReader.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. 
  2. using UnityEngine;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. public class MegaXMLValue
  7. {
  8. public string name;
  9. public string value;
  10. }
  11. public class MegaXMLNode
  12. {
  13. public String tagName;
  14. public MegaXMLNode parentNode;
  15. public List<MegaXMLNode> children;
  16. public List<MegaXMLValue> values;
  17. public MegaXMLNode()
  18. {
  19. tagName = "NONE";
  20. parentNode = null;
  21. children = new List<MegaXMLNode>();
  22. values = new List<MegaXMLValue>();
  23. }
  24. }
  25. public class MegaXMLReader
  26. {
  27. private static char TAG_START = '<';
  28. private static char TAG_END = '>';
  29. private static char SPACE = ' ';
  30. private static char QUOTE = '"';
  31. private static char SLASH = '/';
  32. private static char EQUALS = '=';
  33. private static String BEGIN_QUOTE = "" + EQUALS + QUOTE;
  34. public MegaXMLReader()
  35. {
  36. }
  37. public MegaXMLNode read(String xml)
  38. {
  39. int index = 0;
  40. int lastIndex = 0;
  41. MegaXMLNode rootNode = new MegaXMLNode();
  42. MegaXMLNode currentNode = rootNode;
  43. xml = xml.Replace(" \n", "");
  44. xml = xml.Replace("\n", "");
  45. while ( true )
  46. {
  47. index = xml.IndexOf(TAG_START, lastIndex);
  48. if ( index < 0 || index >= xml.Length )
  49. break;
  50. index++;
  51. lastIndex = xml.IndexOf(TAG_END, index);
  52. if ( lastIndex < 0 || lastIndex >= xml.Length )
  53. break;
  54. int tagLength = lastIndex - index;
  55. String xmlTag = xml.Substring(index, tagLength);
  56. if ( xmlTag[0] == SLASH )
  57. {
  58. currentNode = currentNode.parentNode;
  59. continue;
  60. }
  61. bool openTag = true;
  62. if ( xmlTag[tagLength - 1] == SLASH )
  63. {
  64. xmlTag = xmlTag.Substring(0, tagLength - 1);
  65. openTag = false;
  66. }
  67. MegaXMLNode node = parseTag(xmlTag);
  68. node.parentNode = currentNode;
  69. currentNode.children.Add(node);
  70. if ( openTag )
  71. currentNode = node;
  72. }
  73. return rootNode;
  74. }
  75. public MegaXMLNode parseTag(String xmlTag)
  76. {
  77. MegaXMLNode node = new MegaXMLNode();
  78. int nameEnd = xmlTag.IndexOf(SPACE, 0);
  79. if ( nameEnd < 0 )
  80. {
  81. node.tagName = xmlTag;
  82. return node;
  83. }
  84. String tagName = xmlTag.Substring(0, nameEnd);
  85. node.tagName = tagName;
  86. String attrString = xmlTag.Substring(nameEnd, xmlTag.Length - nameEnd);
  87. return parseAttributes(attrString, node);
  88. }
  89. public MegaXMLNode parseAttributes(String xmlTag, MegaXMLNode node)
  90. {
  91. int index = 0;
  92. int attrNameIndex = 0;
  93. int lastIndex = 0;
  94. while ( true )
  95. {
  96. index = xmlTag.IndexOf(BEGIN_QUOTE, lastIndex);
  97. if ( index < 0 || index > xmlTag.Length )
  98. break;
  99. attrNameIndex = xmlTag.LastIndexOf(SPACE, index);
  100. if ( attrNameIndex < 0 || attrNameIndex > xmlTag.Length )
  101. break;
  102. attrNameIndex++;
  103. String attrName = xmlTag.Substring(attrNameIndex, index - attrNameIndex);
  104. index += 2;
  105. lastIndex = xmlTag.IndexOf(QUOTE, index);
  106. if ( lastIndex < 0 || lastIndex > xmlTag.Length )
  107. {
  108. break;
  109. }
  110. int tagLength = lastIndex - index;
  111. String attrValue = xmlTag.Substring(index, tagLength);
  112. MegaXMLValue val = new MegaXMLValue();
  113. val.name = attrName;
  114. val.value = attrValue;
  115. node.values.Add(val);
  116. }
  117. return node;
  118. }
  119. }