【问题标题】:Does Haxe have a friendship with XPath?Haxe 与 XPath 有友谊吗?
【发布时间】:2016-05-30 14:32:58
【问题描述】:
我需要通过 XPath 获取一些 XML 节点,我该怎么做?
我尝试使用这个库https://github.com/djcsdy/haxe-xpath,但是出了点问题https://github.com/djcsdy/haxe-xpath/issues/26
对于我的任务而言,xml-fast 并不是一个好的解决方案,因为我认为它看起来比 XPath“略差”:
js(xpath):
xml_doc.get('//project/classpaths/class[@path="' + src_path + '"]')
haxe (xml-fast):
(new Fast(xml_doc))).node.project.node.classpaths.nodes.class.filter(function (x:Fast) return x.has.path ? x.att.path == src_path : false)
谢谢
【问题讨论】:
标签:
xml
xpath
xml-parsing
haxe
【解决方案1】:
结果发现了两个库,都需要一些修复。
1. "Haxe XPath"
只有在将目录“haxe-xpath/src/xpath”克隆到你的源中时才能使用它(haxelib repo 不包含这个库)。
此库需要一些修复:this 和 this
示例(删除第一个找到的元素):
package;
import xpath.XPathHx;
using Lambda;
class Main {
public static function main () {
var xml = Xml.parse("<a> <b> <c>qwe</c> <c>asd</c> </b> </a>");
trace(xml.toString());
var xpExpr = new XPathHx("//a/b/c"); // create new XPath expression object
var result = xpExpr.selectNodes(xml).array()[0]; // get first element from array of founded xml-nodes
result.parent.removeChild(result); // remove selected node from xml-tree
trace(xml.toString());
}
}
2. "xmlTools"
可以用haxelib安装:
haxelib install xmlTools
haxelib install composure
Some fix was needed for this library (in my task) 和一个nuance for xpath。
示例(删除第一个找到的元素):
package;
import xmlTools.XPath;
using Lambda;
class Main {
public static function main () {
var xml = Xml.parse("<a> <b> <c>qwe</c> <c>asd</c> </b> </a>");
trace(xml.toString());
var xpath = new XPath(); // create new XPath expression object
var result = xpath.resolve(xml, "*/a/b/c").array()[0]; // get first element from array of founded xml-nodes
result.parent.removeChild(result); // remove selected node from xml-tree
trace(xml.toString());
}
}