public SimpleElement parse(XmlTextReader reader) { SimpleElement se = null; this.Reader = reader; while(!Reader.EOF) { Reader.Read(); // Proceed based on type of node encountered. We mostly care about Elements at this point. switch (Reader.NodeType) { case XmlNodeType.Element: // create a new SimpleElement se = new SimpleElement(Reader.LocalName); currentElem = se; if (elements.Count == 0) { rootElem = se; elements.Push(se); } else { SimpleElement parent = (SimpleElement) elements.Peek(); parent.ChildElems.Add(se); foreach (SimpleElement simpElem in parent.ChildElems) { if (simpElem.Text != "") { // rootElem.modelList.AddPoly(simpElem.Text); // Console.WriteLine(simpElem.Text); } } if (Reader.LocalName.Equals("float_array")) { } // don['t push empty elements onto the stack if (Reader.IsEmptyElement) // ends with "/>" { break; } else { elements.Push(se); } } if (Reader.HasAttributes) { while (Reader.MoveToNextAttribute()) { currentElem.setAttribute(Reader.Name, Reader.Value); } } break; case XmlNodeType.Attribute: se.setAttribute(Reader.Name, Reader.Value); break; case XmlNodeType.EndElement: //pop the top element elements.Pop(); break; case XmlNodeType.Text: currentElem.Text = Reader.Value; break; case XmlNodeType.CDATA: currentElem.Text = Reader.Value; break; default: // ignore break; } } return rootElem; }
public Parser() { elements = new Stack(); currentElem = null; }
private static void printTree(SimpleElement elements, StringBuilder sb, int depth) { sb.Append(new string('\t', depth) + "<" + elements.TagName); foreach (string attName in elements.Attrib.Keys) { sb.Append(" " + attName + "=" + "\"" + elements.Attribute(attName) + "\""); } sb.Append(">" + elements.Text.Trim()); if (elements.ChildElems.Count > 0) { sb.Append(System.Environment.NewLine); depth += 1; foreach (SimpleElement ch in elements.ChildElems) { //sb.Append(System.Environment.NewLine); printTree(ch, sb, depth); } depth -= 1; sb.Append(new string('\t', depth) + "</" + elements.TagName + ">" + System.Environment.NewLine); } else { sb.Append("</" + elements.TagName + ">" + System.Environment.NewLine); } }
public void Parse(String inputFile) { try { starttick = System.Environment.TickCount; // String fileToParse = @"c:/models/6d.dae"; // String fileToParse = @"c:/doc.kml"; String fileToParse = @inputFile; if (System.IO.Path.GetExtension(fileToParse).Equals(".kml")) { XmlTextReader readr = new XmlTextReader(fileToParse); Parser tempParse = new Parser(); while (!readr.EOF) { readr.Read(); if (readr.LocalName.Equals("href")) { readr.Read(); String newFilePath = readr.Value; String finalFileName = Path.GetDirectoryName(fileToParse); finalFileName = Path.Combine(@finalFileName, @newFilePath); fileToParse = finalFileName; break; } else if (readr.LocalName.Equals("float_array")) { System.Console.WriteLine(readr.LocalName); System.Console.WriteLine("Found"); } } } rdr = new XmlTextReader(@fileToParse); XmlTextReader temprdr = new XmlTextReader(fileToParse); sdp = new Parser(); while (!temprdr.EOF) { temprdr.Read(); if (temprdr.LocalName.Equals("float_array")) { if (temprdr.HasAttributes) { temprdr.MoveToNextAttribute(); if (temprdr.Value.Contains("position")) { temprdr.MoveToElement(); String testval = temprdr.ReadInnerXml(); Console.WriteLine("Testval is {0}", testval); models.AddPoly(testval); } } } } elements = sdp.parse(rdr); rdr.Close(); endtick = System.Environment.TickCount; sb = new StringBuilder(); if (DEBUG) { printTree(elements, sb, 0); System.Diagnostics.Debug.WriteLine(sb.ToString()); System.Diagnostics.Debug.WriteLine("There are " + this.models.ModelCount + " total models in the file."); for (int j = 0; j < this.models.ModelCount; j++) { System.Diagnostics.Debug.WriteLine("Model " + j + " has " + this.models.TotalVerts(j) + " total vertices.\n"); } System.Diagnostics.Debug.WriteLine("Total time to parse: "+ (endtick - starttick)); istr = Console.ReadLine(); //Pause to debug } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } }
public void Add(SimpleElement se) { this.List.Add(se); }