<p>Hi folks,</p>
<p>Relatively new gopher here, but long time reader. I'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><Activity>
<Type>dancing</Type>
<Timestamp>1238138422</Timestamp>
<Fruit>Banana</Fruit>
</Activity>
</code></pre>
<p>The dilemma is that since I'm reading from an HTTP stream, the XML can get cut off arbitrarily after read. I've so far come up with a hack to check if we're "inside" of the stanza by doing a Contains to see if the <Activity> or </Activity> 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'm not exactly sure what's happening here, can you post the code you'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'm parsing isn't well formed (in that it'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><Activity></code> elements, i.e. non-well-formed XML?</p>
<p>If that's the case you'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>
[newbie question] Properly reading XML from an HTTP stream and correct way to demarcate packets?
polaris · · 377 次点击这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
0 回复
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传