/// <summary>Start visitor (initialization)</summary> /// <exception cref="System.IO.IOException"/> public virtual void Start(int version) { try { contentHandler.StartElement(string.Empty, string.Empty, "EDITS_VERSION", new AttributesImpl ()); StringBuilder bld = new StringBuilder(); bld.Append(version); AddString(bld.ToString()); contentHandler.EndElement(string.Empty, string.Empty, "EDITS_VERSION"); } catch (SAXException e) { throw new IOException("SAX error: " + e.Message); } }
private void TraverseNode(HtmlNode htmlNode) { if (htmlNode == null || htmlNode.NodeType == HtmlNodeType.Comment) { return; } var attributes = new Attributes(); if (htmlNode.HasAttributes) { foreach (HtmlAttribute attribute in htmlNode.Attributes) { attributes.AddAttribute(null, htmlNode.Name, attribute.Name, null, attribute.Value); } } ContentHandler.StartElement(null, htmlNode.Name, htmlNode.Name, attributes); if (htmlNode.NodeType == HtmlNodeType.Text) { ContentHandler.Characters(htmlNode.InnerText.ToCharArray(), 0, htmlNode.InnerText.Length); } else if (htmlNode.HasChildNodes) { foreach (HtmlNode childNode in htmlNode.ChildNodes) { TraverseNode(childNode); } } ContentHandler.EndElement(null, htmlNode.Name, htmlNode.Name); }
/// <summary>Add a SAX tag with a string inside.</summary> /// <param name="contentHandler">the SAX content handler</param> /// <param name="tag">the element tag to use</param> /// <param name="val">the string to put inside the tag</param> /// <exception cref="Org.Xml.Sax.SAXException"/> public static void AddSaxString(ContentHandler contentHandler, string tag, string val) { contentHandler.StartElement(string.Empty, string.Empty, tag, new AttributesImpl() ); char[] c = MangleXmlString(val, false).ToCharArray(); contentHandler.Characters(c, 0, c.Length); contentHandler.EndElement(string.Empty, string.Empty, tag); }
public void Parse(StreamReader input, ContentHandler handler) { var buffer = new StringBuilder(); var isInsideTag = false; var isStartTag = true; var lastChar = -1; int c; while ((c = input.Read()) != -1) { if ('<' == c) { if (isInsideTag) { throw new InvalidFormatException("Did not expect < char!"); } if (buffer.ToString().Trim().Length > 0) { handler.Characters(buffer.ToString().Trim()); } buffer = new StringBuilder(); isInsideTag = true; isStartTag = true; } // buffer.AppendCodePoint(c); <-- java buffer.Append(char.ConvertFromUtf32(c)); if ('/' == c && lastChar == '<') { isStartTag = false; } if ('>' == c) { if (!isInsideTag) { throw new InvalidFormatException("Did not expect > char!"); } if (isStartTag) { handler.StartElement(extractTagName(buffer), getAttributes(buffer)); } else { handler.EndElement(extractTagName(buffer)); } buffer = new StringBuilder(); isInsideTag = false; } lastChar = c; } if (isInsideTag) { throw new InvalidFormatException("Did not find matching > char!"); } }
private void TraverseNode(HtmlNode htmlNode) { if (htmlNode.NodeType == HtmlNodeType.Element) { var attributes = new Attributes(); if (htmlNode.HasAttributes) { foreach (HtmlAttribute attribute in htmlNode.Attributes) { attributes.AddAttribute(null, htmlNode.Name, attribute.Name, null, attribute.Value); } } //Debug.WriteLine($"START: {htmlNode.Name}"); ContentHandler.StartElement(null, htmlNode.Name, htmlNode.Name, attributes); if (htmlNode.HasChildNodes) { foreach (HtmlNode childNode in htmlNode.ChildNodes) { if (childNode.NodeType == HtmlNodeType.Text) { //Debug.WriteLine(childNode.InnerText); ContentHandler.Characters(childNode.InnerText.ToCharArray(), 0, childNode.InnerText.Length); } TraverseNode(childNode); } } //Debug.WriteLine($"ENDE: {htmlNode.Name}"); ContentHandler.EndElement(null, htmlNode.Name, htmlNode.Name); } else { if (htmlNode.HasChildNodes) { foreach (HtmlNode childNode in htmlNode.ChildNodes) { TraverseNode(childNode); } } } //if (!(htmlNode.NodeType == HtmlNodeType.Comment || // htmlNode.NodeType == HtmlNodeType.Text || // htmlNode.NodeType == HtmlNodeType.)) //{ // ContentHandler.StartElement(null, htmlNode.Name, htmlNode.Name, null); // if (htmlNode.HasChildNodes) // foreach (HtmlNode childNode in htmlNode.ChildNodes) // TraverseNode(childNode); // ContentHandler.EndElement(null, htmlNode.Name, htmlNode.Name); //} //else //{ // if (htmlNode.HasChildNodes) // foreach (HtmlNode childNode in htmlNode.ChildNodes) // TraverseNode(childNode); //} }