2016-05-30 94 views
2

我需要通過它的XPath獲取一些XML節點,我該怎麼做呢?Haxe和XPath有友誼嗎?

我試圖用這個庫https://github.com/djcsdy/haxe-xpath,但蹊蹺的https://github.com/djcsdy/haxe-xpath/issues/26

和我的任務XML快速不好的解決方案,因爲它看起來比XPath的「稍差」,我認爲:

JS(xpath的):

xml_doc.get('//project/classpaths/class[@path="' + src_path + '"]') 

haXe的(XML快速):

(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

用於文庫 「haXe的-的xpath」 被做一些小的修正https://github.com/djcsdy/haxe-xpath/issues/26 –

回答

2

因爲結果發現兩個庫,都需要一些修復。

"Haxe XPath"

它只能如果使用克隆目錄 「haXe的-的XPath/src目錄/ XPath的」 到你的來源(haxelib回購不包含此庫)。需要

一些修補此庫:thisthis

例如(刪除第一發現元素):

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()); 
    } 
}