我需要通过它的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)
感谢
答案 0 :(得分:2)
结果发现有两个库,都需要一些修复。
1。 "Haxe XPath"
只有当克隆目录“haxe-xpath / src / xpath”进入您的源(haxelib repo不包含此库)时才可以使用它。
示例(删除第一个找到的元素):
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());
}
}