enconding/xml - tag with multiple possible parents?

blov · · 736 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;m trying to parse an XML file where tag D can be a child of tag A, B, and C (additionally, tag C may be a child of A and B, etc). How can I define my structs so that all of D tags are captured? I am currently trying: type TagC struct { //stuff here } type StructA struct { XMLName xml.Name <code>xml:&#34;root tag name&#34;</code> TagC []TagC <code>xml:&#34;tagA&gt;tagB&gt;tagD&#34; xml:&#34;tagA&gt;tagD&#34;</code> }</p> <p>It looks like the parser is only finding elements of TagD where they are under a tagA&gt;tagB structure. Is there a way to do this?</p> <hr/>**评论:**<br/><br/>jerf: <pre><p>It&#39;s worth pointing out that <code>encoding/xml</code> is two things: 1. A simple XML parser. 2. An easy way of serializing Go objects into and out of XML with a few things tossed in for controlling that process that were easy.</p> <p>#2 is by no means a complete XML parsing solution; it is primarily focused on serializing Go objects. When that happens to be enough to match a given format, hey, great, but it isn&#39;t intended to be a complete solution. (Having spent more time than I care to admit fiddling with this, my considered opinion is that there is no such thing as an easy and complete solution of that kind. In the end, the XML parser itself is already the easiest thing a complete solution can be.)</p> <p>You may have to fall back to #1, or find a different solution, if your XML is sufficiently complicated.</p></pre>threemux: <pre><p>If you do have to use option #1 above and parse xml without the convenience of struct tags, this blog post has some information about the streaming api provided in encoding/xml. </p> <p><a href="http://blog.davidsingleton.org/parsing-huge-xml-files-with-go/" rel="nofollow">http://blog.davidsingleton.org/parsing-huge-xml-files-with-go/</a></p></pre>jerf: <pre><p>Excellent link, thank you.</p> <p>It&#39;s a simple API, but surprisingly powerful. Sometimes I think having <code>io.Reader</code> and friends in the API from nearly day one and getting so many things implemented in terms of them is at least a quarter of the appeal of Go. Many languages have the theoretical constructs necessary to do it, but they were almost always implemented initially in terms of strings, with the streaming getting bolted on years later.</p></pre>threemux: <pre><p>Agreed - I&#39;ve done Java for a while now and it has all the same stream constructs, but they&#39;re just not first class for whatever reason. People seem to rag on SAX parsers all the time but I think the concept is really nice for large and/or complex XML documents. </p></pre>nathj07: <pre><p>I think you may have to defineultiple elements on the structure, each with tags holding the relevant and distinct path. Only data that is found will be populated.</p> <p>I have had to do some seriously messy things like this before. However, you can make it clean-ish by separating in the right place. For example you could define a new stru t called C and have one member on it with the structure tag of the common path, probably just the end. Then the parent can have multiple items of type C on it. Each of these with the distinct path parts in a strict tag.</p> <p>I&#39;m on my phone right now but could post a clearer example later if you want?</p></pre>

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

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