A box tree seems to be the best fit, since XML is a tree anyway.I actually
attempted a JSON serializer/deserializer in J. I got the serialization part
(the easy part) done,but not tested and I lost interest.Of course XML is a
little different to JSON (e.g. attributes) and JSON is much easier.Anyway this
is my serialization code, which may be helpful in some way:
OPEN =: '{' CLOSE =: '}' MID =: ':' COMMA =: ',' QUOTE =: '"'
LISTOPEN =: '[' LISTCLOSE =: ']'
NB. to JSON string (double quote)str =: (,"E)@:(QUOTE&,)@:":
NB. serialize box tree to JSONtoJSON =: verb defineif. ((<datatype y) =
<'boxed') = 0 do.result =: str yelse.isList =. 0NB. left sideleft =. >@:(0&{)
yif. (< datatype left) = <'boxed' do. isList =. 1 left =. toJSON
leftelse. left =. OPEN , str leftend.NB.right sideright =. (1&{) yif. (<
datatype right) = <'boxed' do. right =. toJSON > rightelse. right =. (":
right) NB. literal for all end.
result =: left , MID , rightif. isList = 0 do. result =: left , MID , right ,
CLOSEelse. result =: LISTOPEN , left , COMMA , right , LISTCLOSE
end.end.result)
Some data:
data =: 'level1';(<('level2a';'data data data');<('level2b';<('level3';'more
data data')))
My example data is trying to be equivalent to Dictionary<string, object>
structures, as in C#.
i.e. inside left box is a string, and inside right box is an object, in the
above case, string data, and in the case of
level2a and level2b - a list of data.
> From: [email protected]
> Date: Fri, 5 Dec 2014 16:27:41 -0500
> To: [email protected]
> Subject: [Jprogramming] xml in J
>
> I would like to revisit the idea of using J to parse xml.
>
> The xml/sax addon was a nice idea, but not very stable. It represented
> xml as a series of events (function calls), and left it up to the user
> how they would structure the result. Unfortunately, it also rather
> reliably crashes J.
>
> This can be mitigated in various ways. If what you are parsing is
> simple enough, and you can live with 32 bit j602, xml/sax can work
> great. But those are not always ideal constraints to work with.
>
> But... what's a good data structure in J, to represent xml?
>
> A problem is that xml is something of a living example of "the nice
> thing about standards is that there are so many to choose from". The
> standards documents describing xml are voluminous, and there are many
> alternatives which are physically different but logically similar to
> wade through.
>
> Still, at a basic level, xml is something of a nested sequence type of
> a thing. So one approach might leverage boxed character arrays. This
> will not be particularly efficient, but it's a start.
>
> For example, this xml snippet:
>
> <ab cd="ef" gh="ijk">lmnop</a>
>
> Might be represented in J as:
> 'ab';<('cd';'ef'),('gh';'ijk'),:'';<<'lmnop'
>
> (The extra boxing on the text is because that might in the general
> case actually be a sequence of elements).
>
> Another approach might be:
> 'ab';(('cd';'ef'),:('gh';'ijk'));<<'lmnop'
>
> Here, the [textual, in this case] content of the element is stored in
> a separate box from the attributes, instead of treating it as a
> blank-named attribute.
>
> But perhaps there are good non-boxed ways of representing the structure?
>
> Has anyone else been working with xml in J?
>
> Thanks,
>
> --
> Raul
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm