goxpath is more mature and close to XPath 1.0 compliance.

blov · · 476 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p><a href="https://github.com/ChrisTrenkamp/goxpath">https://github.com/ChrisTrenkamp/goxpath</a></p> <p>goxpath is almost at XPath 1.0 compliance (it&#39;s just missing the id() function). The API can now register custom functions. The command-line tool is also more usable.</p> <p>If you&#39;re looking for an XPath parser, goxpath may be useful. I&#39;d like to hear your feedback if you&#39;ve used it.</p> <hr/>**评论:**<br/><br/>geodel: <pre><p>Thank you! </p> <p>I am wondering whether goxpath provides API where I can modify some XML element/attribute value and get new XML returned. e.g. something like </p> <p><code>&lt;el1&gt;John Doe&lt;/el1&gt; or &lt;el1 atr1 = &#34;John&#34; atr2 = &#34;Doe&#34;/&gt;</code> be changed to <code>&lt;el1&gt;Jane Doe&lt;/el1&gt; or &lt;el1 atr1 = &#34;Jane&#34; atr2 = &#34;Doe&#34;/&gt;</code></p></pre>ChrisTrenkamp: <pre><p>The core API works with the Node/Elem interfaces defined in the tree package. xmltree is a default implementation for those interfaces. xmltree was meant to be lightweight and non-volatile. You can change the structure like you would with Java&#39;s DOM, but you&#39;re on your own.</p> <p>With that said, it&#39;s quite possible to create a tree that&#39;s more like Java&#39;s DOM that&#39;s designed to be altered in-memory.</p> <p>EDIT: Off the top of my head, the two things that will be affected when adding or removing nodes from the tree is the node positions and namespaces. If that doesn&#39;t matter to you, you can probably add or remove nodes without too many repercussions.</p></pre>geodel: <pre><p>To add some detail in my use case I am using XML as a template so rather than creating whole XML from scratch I would just search paths of relevant element/attribute using Xpath and replacing them using Java Xpath API in a pre-existing xml file by reading it in memory. </p> <p>So far I have not found a straight forward way to do that in Go. </p></pre>ChrisTrenkamp: <pre><p>I see. goxpath can do half of that. You can definitely retrieve nodes and elements in the tree using XPath, but unfortunately the xmltree package falls short when it comes to editing the tree. Simple operations like changing the local name of an element or attribute, or changing the value of a node or attribute should be no problem. Anything beyond that will cause problems.</p> <p>Feel free to file a bug report if you&#39;d like to be able to alter the xmltree structure. Some Java DOM-like functionality can be added to the xmltree package.</p></pre>geodel: <pre><p>Ah Great. I will give it a try. </p> <p>This is my Java code snippet which I would like to try in Go</p> <pre><code>Document newDoc = dbuilder.parse(filename); XPathExpression xExpr = Xpath.compile(&#34;/Properties/Property/Value[@value=&#39;PHONENUM&#39;]&#34;); xExpr.evaluate(newDoc, XPathConstants.STRING); NodeList nodes = (NodeList) xExpr.evaluate(newDoc, XPathConstants.NODESET); nodes.item(0).setNodeValue(req.phoneNum1); nodes.item(1).setNodeValue(req.phoneNum2); return newDoc; </code></pre></pre>

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

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