View unanswered posts | View active topics


Reply to topic  [ 4 posts ] 
W.alk(Xml) Haxe utility for XML. 
Author Message
Member

Joined: Sun May 11, 2008 3:01 pm
Posts: 860
Post W.alk(Xml) Haxe utility for XML.
Hi all.
This is my first serious experience with HaXe, however, I think many would find this thing useful. It's a pain to port oneself from AS3 with E4X to HaXe where working with XML is good comparing to JavaScript and maybe PHP, but E4X beats the living crap out of any other XML library that I know... so, neither HaXe Fast not Xml can be as simple and easy to come up with as E4X.
I've tried to write something that could simplify if not replace AS3 E4X with some HaXe code... The work is still in the process, but I shall improve.
So far the core features are like this:
You can run long queries on XML object which include customizable filtration.
The queries are made in one operation, although they have execution order.
If you're familiar with the concept of Mock Object and State Machine, then it'll be easy to understand how things work. The syntax is somewhat similar to JQuery, however, the mechanics behind the stage are rather different.

Here's an example usage:

Code:
/**
* ...
* @author wvxvw
*/
package org.wvxvws;

import flash.Lib;
import flash.display.Sprite;
import org.wvxvws.xml.W;

class TestW extends Sprite
{
   private var _xml:Xml;
   
   public function new()
   {
      super();
      this._xml = Xml.parse("
      <a>
         <b/>
         <b/>
         <b>
            <c foo=\"bar0\">
               <d abcd=\"123\" qwerty=\"shouldn't catch\"/>
            </c>
            <c foo=\"bar1\"/>
            <c foo=\"bar2\">
               <d abcd=\"456\" qwerty=\"678\"/>
            </c>
            <c foo=\"bar3\"/>
         </b>
         <b/>
      </a>");
      
      // AS3 analogue: this._xml..*.(valueOf().@*.(toXMlString() == "bar2").length()).*.@qwerty;
      
      var a:Array<Hash<String>> = cast W.alk(this._xml).d().a(this.filter).c().a(this.filter2).z();
      
      trace(a); // TestW.hx 36: [{qwerty=>678}]
   }
   
   private inline function filter(attName:String, attValue:String, node:Xml):Bool
   {
      return attValue == "bar2";
   }
   
   private inline function filter2(attName:String, attValue:String, node:Xml):Bool
   {
      return attName == "qwerty";
   }
   
   public static function main() { Lib.current.addChild(new TestW()); }
}


The utility itself is here:
http://code.google.com/p/e4xu/source/br ... s/xml/W.hx

Your comments / ideas welcome :)

_________________
http://www.couchsurfing.com/people/wvxvw


Last edited by wvxvw on Mon Jan 11, 2010 7:13 pm, edited 1 time in total.



Sun Jan 10, 2010 10:32 pm
Profile
Member

Joined: Sun May 11, 2008 3:01 pm
Posts: 860
Post Re: W.alk(Xml) Haxe utility for XML.
Oh, so we are here :) Well, then, maybe I could post some more stuff.

Here's one example that you'd probably find useful since HaXe Xml doesn't have ignoreWhite or similar feature:

Code:
this._removeWhite = ~/^[\t\s\r\n]*([^\t\s\r\n]+)*[\t\s\r\n]*$/g;
W.alk(this._xml).d().t(this.filterTexts);
....
private function filterTexts(text:String, node:Xml):Bool
{
   node.nodeValue = this._removeWhite.replace(text, "$1");
   return true;
}

This way we will make it behave like AS3 XML does when you set it to XML.ignoreWhite = true;

_________________
http://www.couchsurfing.com/people/wvxvw


Mon Jan 11, 2010 4:27 pm
Profile
Member

Joined: Mon Oct 16, 2006 12:02 am
Posts: 280
Location: Lviv, Ukraine
Post Re: W.alk(Xml) Haxe utility for XML.
I think you should give more significant names for your class and functions.
... d().t(...) is not readable for me.


Tue Jan 12, 2010 7:18 pm
Profile WWW
Member

Joined: Sun May 11, 2008 3:01 pm
Posts: 860
Post Re: W.alk(Xml) Haxe utility for XML.
On the one hand I definitely agree with you, and in any other case I would certainly do it! But, the specific of this thing is that normally the code of such format:

Code:
foo.fooBar().barFoo().anotherMeanigfulFunctionName().yetAnotherFunction()
* Macaroni style *

Would be considered hard to read and, well, I just don't like to write it like:

Code:
foo.fooBar().barFoo(
    ).anotherMeanigfulFunctionName(
    ).yetAnotherFunction()
* Linq style *

Well, it seems even more wrong to me...

Ideally, if I was able to use some special characters rather then letters (compare AS3 @ for attributes and . for nodes), that would be best, but, since it's not really possible, I'd rather stick to what I have... Well, because the intent for it is to be more like a regexp / xpath, not really function calls.

But I do understand your attitude. Well, let's say, I'll do more cleaning of the code, give it some more comments etc, and will post it in a few more boards, and we'll see, if I'll get more feedback like yours I'll change that to "normal" names.

_________________
http://www.couchsurfing.com/people/wvxvw


Tue Jan 12, 2010 9:43 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 4 posts ] 

Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.