public void BuildTreeFromXML(string xml_string) { /*Preprocess XML String to remove extra stuff and make it suitable for processing*/ CleanXMLString(xml_string); root = BuildTree(xml_string); }
public XMLTag GetValue(String xml_string) { String local_xml_string = xml_string; XMLTag tag = new XMLTag(); Dictionary <String, String> attributes = new Dictionary <String, String>(); string opening_tag_pattern = @"\<[\w]+(\s+[\w]+\s*=\s*[""][\d\w\s]*[""]\s*)*\s*\>"; Regex opening_tag_rgx = new Regex(opening_tag_pattern); int index = opening_tag_rgx.Match(local_xml_string).Index; bool success = opening_tag_rgx.Match(local_xml_string).Success; /*The first tag in string is opening tag*/ if (opening_tag_rgx.Match(local_xml_string).Index == 0 && opening_tag_rgx.Match(local_xml_string).Success) { String tag_string = local_xml_string.Substring(0, local_xml_string.IndexOf('>') + 1); /*Tag name*/ String tag_name_pattern = @"\<[\w]+"; Regex tag_name_rgx = new Regex(tag_name_pattern); tag.tag_name = tag_name_rgx.Match(tag_string).Value.Remove(0, 1); String value_pattern = @"\s+[\w]+\s*=\s*[""][\d\w\s]*[""]"; Regex value_rgx = new Regex(value_pattern); while (value_rgx.Match(tag_string).Success) { Match match = value_rgx.Match(tag_string); String[] attribute = match.Value.Split('='); tag.attributes.Add(attribute[0], attribute[1]); /*Operate on rest of string to extract remaining attributes*/ tag_string = tag_string.Substring(match.Index + match.Value.Count()); } } return(tag); }
private XMLTag BuildTree(string xml_string) { List <String> children = GetChildren(xml_string); XMLTag tag_node = GetValue(xml_string); if (children != null && tag_node != null) { foreach (String child in children) { tag_node.children.Add(BuildTree(child)); } } else { tag_node = null; } return(tag_node); }
public XMLParser() { root = null; }