public XMLNode() { tagName = "NONE"; parentNode = null; children = new ArrayList(); attributes = new Dictionary<String, String>(); innerText = ""; cdata = ""; }
public static int ParseInt(XMLNode element) { if (element == null) { //Asserts.Assert(false);//, "Invalid node name: " + element.tagName + " in: " + element.parentNode.tagName); return 0; } return int.Parse(element.innerText);//, CultureInfo.InvariantCulture); }
public static float ParseFloat(XMLNode element) { if (element == null) { //Asserts.Assert(false);//, "Invalid node name: " + element.Name + " in: " + element.ParentNode.Name); return float.NaN; } return float.Parse(element.innerText);//, CultureInfo.InvariantCulture); }
public static float[] ParseFloatArray(XMLNode element) { if (element == null) { //Asserts.Assert(false);//, "Invalid node name: " + element.Name + " in: " + element.ParentNode.Name); return null; } char[] delimeters = { ',' }; string[] parts = element.innerText.Split(delimeters, StringSplitOptions.RemoveEmptyEntries); float[] result = new float[parts.Length]; for (int i = 0; i < result.Length; i++) result[i] = float.Parse(parts[i]);//, CultureInfo.InvariantCulture); return result; }
public XMLNode parseTag(String xmlTag) { XMLNode node = new XMLNode(); int nameEnd = xmlTag.IndexOf(SPACE, 0); if (nameEnd < 0) { node.tagName = xmlTag.Trim('\r', '\n', ' '); return node; } String tagName = xmlTag.Substring(0, nameEnd); node.tagName = tagName.Trim('\r', '\n', ' '); String attrString = xmlTag.Substring(nameEnd, xmlTag.Length - nameEnd); return parseAttributes(attrString, node); }
public static SBSVector3 ParseVector3(XMLNode element) { if (element == null) { //Asserts.Assert(false);//, "Invalid node name: " + element.Name + " in: " + element.ParentNode.Name); return SBSVector3.zero; } char[] delimeters = { ',' }; string[] parts = element.innerText.Split(delimeters, StringSplitOptions.RemoveEmptyEntries); //if (parts.Length < 3) //Asserts.Assert(false);//, "Invalid Vector3 param length node: " + element.Name + " in: " + element.ParentNode.Name); float x = float.Parse(parts[0]);//, CultureInfo.InvariantCulture); float y = float.Parse(parts[1]);//, CultureInfo.InvariantCulture); float z = float.Parse(parts[2]);//, CultureInfo.InvariantCulture); return new SBSVector3(x, y, z); }
public XMLNode parseAttributes(String xmlTag, XMLNode node) { int index = 0; int attrNameIndex = 0; int lastIndex = 0; while (true) { index = xmlTag.IndexOf(BEGIN_QUOTE, lastIndex); if (index < 0 || index > xmlTag.Length) { break; } attrNameIndex = xmlTag.LastIndexOf(SPACE, index); if (attrNameIndex < 0 || attrNameIndex > xmlTag.Length) { break; } attrNameIndex++; String attrName = xmlTag.Substring(attrNameIndex, index - attrNameIndex); // skip the equal and quote character // index += 2; lastIndex = xmlTag.IndexOf(QUOTE, index); if (lastIndex < 0 || lastIndex > xmlTag.Length) { break; } int tagLength = lastIndex - index; String attrValue = xmlTag.Substring(index, tagLength); node.attributes.Add(attrName, attrValue);//[attrName] = attrValue; } return node; }
public void printXML(XMLNode node, int indent) { indent++; foreach (XMLNode n in node.children) { String attr = " "; foreach (KeyValuePair<String, String> p in n.attributes) { attr += "[" + p.Key + ": " + p.Value + "] "; //Debug.Log( attr ); } String indentString = ""; for (int i = 0; i < indent; i++) { indentString += "-"; } Debug.Log("" + indentString + " " + n.tagName + attr); printXML(n, indent); } }
public XMLNode read(String xml) { int index = 0; int lastIndex = 0; int prevLastIndex = 0; XMLNode rootNode = new XMLNode(); XMLNode currentNode = rootNode; while (true) { index = xml.IndexOf(TAG_START, lastIndex); if (index < 0 || index >= xml.Length) { break; } index++; prevLastIndex = lastIndex + 1; lastIndex = xml.IndexOf(TAG_END, index); if (lastIndex < 0 || lastIndex >= xml.Length) { break; } int tagLength = lastIndex - index; String xmlTag = xml.Substring(index, tagLength); // if the tag starts with ? or ! go on if ('?' == xmlTag[0]) continue; if ('!' == xmlTag[0]) { if (1 == xmlTag.IndexOf("[CDATA[")) { lastIndex = xml.IndexOf("]]>", index); if (lastIndex < 0 || lastIndex >= xml.Length) break; else { index += 8; currentNode.cdata += xml.Substring(index, lastIndex - index); index = lastIndex + 4; lastIndex = xml.IndexOf(TAG_END, index); tagLength = lastIndex - index; xmlTag = xml.Substring(index, tagLength); } } else { lastIndex = xml.IndexOf("-->", index); if (lastIndex < 0 || lastIndex >= xml.Length) break; else continue; } } // if the tag starts with a </ then it is an end tag // if (xmlTag[0] == SLASH) { if (0 == currentNode.children.Count) currentNode.innerText = xml.Substring(prevLastIndex, index - prevLastIndex - 1); currentNode = currentNode.parentNode; continue; } bool openTag = true; // if the tag ends in /> the tag can be considered closed if (xmlTag[tagLength - 1] == SLASH)// || xmlTag[tagLength - 1] == ']') { // cut away the slash xmlTag = xmlTag.Substring(0, tagLength - 1); openTag = false; } XMLNode node = parseTag(xmlTag); node.parentNode = currentNode; currentNode.children.Add(node); if (openTag) { currentNode = node; } } return rootNode; }