[newbie question] Properly reading XML from an HTTP stream and correct way to demarcate packets?

polaris · · 377 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hi folks,</p> <p>Relatively new gopher here, but long time reader. I&#39;ve written a few projects in Go but nothing too major, and definitely nothing that parses xml. So I have an app that sends XML similar to this:</p> <pre><code>&lt;Activity&gt; &lt;Type&gt;dancing&lt;/Type&gt; &lt;Timestamp&gt;1238138422&lt;/Timestamp&gt; &lt;Fruit&gt;Banana&lt;/Fruit&gt; &lt;/Activity&gt; </code></pre> <p>The dilemma is that since I&#39;m reading from an HTTP stream, the XML can get cut off arbitrarily after read. I&#39;ve so far come up with a hack to check if we&#39;re &#34;inside&#34; of the stanza by doing a Contains to see if the &lt;Activity&gt; or &lt;/Activity&gt; tags are there and having a little flag, but I know there has to be a better way. </p> <p>Can anyone lend any suggestions? (PS. This is a little side project for myself to learn more Go, its not homework, work work, or otherwise production in any way)</p> <hr/>**评论:**<br/><br/>1lann: <pre><p>I&#39;m not exactly sure what&#39;s happening here, can you post the code you&#39;re trying to use? Are you using an <code>xml.Decoder</code> on the <code>Body</code> of the HTTP response? Or doing something else?</p></pre>TheBeasSneeze: <pre><p>This package is useless for more complicated namespaced xml, no concept of xsd either. It really needs to be re-written as a dom parser like the html package.</p></pre>nefaspartim: <pre><p>And I should mention that the xml I&#39;m parsing isn&#39;t well formed (in that it&#39;s just a bunch of fragments).</p></pre>zeiko_is_back: <pre><p>Are you not using encoding/xml?</p> <p><a href="https://play.golang.org/p/60Im6cr4Iv" rel="nofollow">https://play.golang.org/p/60Im6cr4Iv</a></p></pre>metamatic: <pre><p>You mean you need to be able to read incomplete <code>&lt;Activity&gt;</code> elements, i.e. non-well-formed XML?</p> <p>If that&#39;s the case you&#39;ll probably need to <a href="http://blog.davidsingleton.org/parsing-huge-xml-files-with-go/" rel="nofollow">stream parse</a> it SAX style.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

377 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传