Sorry if I kept replying to my own email, I would like to point out that the 
following support verbs are part of my code library and found it more 
convenient to just copy and paste them instead of making an optimized one from 
scratch just for these topic:
NB. =========================================================
NB. Support verbs
boxEMPTY=: <''
boxitems=: <"_1 ^: (0: = L.)
boxedlist2mat=: [: |: boxitems &>
blankout=: 3 : 'boxEMPTY (bx y = <,'' '') } y'
mat2boxedlist=: 3 : 0
dat=. <"1 |: ,each y
opn=. > each dat
emp=. isempty &> opn
num=. bx emp < 2 ~: 3!:0 &> opn
dat=. (, each num { opn) num } dat
ndx=. (i.#dat) -. num
(blankout each ndx { dat) ndx } dat
)
charsep=: 3 : 0
',' charsep y
:
if. L. y do.
  }. ; (x &, @ ":) each y
else.
  , ": y
end.
)

I guess you can figure out what they do. I am aware that some of these codes 
already exist in one form or the others in the AddOns but I'm sort of more 
familiar with these. Take for example boxEMPTY ... I should have replaced that 
years ago with the ACE primitive (a:).

I hope this is of use to someone. ;)

NOTE: You guys should play around with the verbs mat2boxedlist and 
boxedlist2mat ... these are cool verbs. ;) 
   [rawdata=: 2 3 $ 'Vilma';'Shenzen';'China';'Alex';'Makati';'Philippines'
+-----+-------+-----------+
|Vilma|Shenzen|China      |
+-----+-------+-----------+
|Alex |Makati |Philippines|
+-----+-------+-----------+

NB. mat2boxedlist - converts a 2 dimension matrix into a list of vectors by 
columns
   mat2boxedlist rawdata
+------------+----------------+-------------------+
|+-----+----+|+-------+------+|+-----+-----------+|
||Vilma|Alex|||Shenzen|Makati|||China|Philippines||
|+-----+----+|+-------+------+|+-----+-----------+|
+------------+----------------+-------------------+

NB. boxedlist2mat - converts a vector to a 2 dimension matrix
   [temp=. 1 2 3;(;: 'aa bb cc');<<'wired'
+-----+----------+-------+
|1 2 3|+--+--+--+|+-----+|
|     ||aa|bb|cc|||wired||
|     |+--+--+--+|+-----+|
+-----+----------+-------+
   boxedlist2mat temp
+-+--+-----+
|1|aa|wired|
+-+--+-----+
|2|bb|     |
+-+--+-----+
|3|cc|     |
+-+--+-----+

I should really put this in the JWIKI but I broke my left collarbone and it 
hasn't healed yet. :(

-----Original Message-----
From: [email protected] 
[mailto:[email protected]] On Behalf Of Alex Rufon
Sent: Wednesday, May 05, 2010 4:12 PM
To: Programming forum
Subject: Re: [Jprogramming] Poor mans way of creating XML document files in J



I forgot to add, "What do you guys think?", before pressing the SEND button. :P 
heheheh

-----Original Message-----
From: [email protected] 
[mailto:[email protected]] On Behalf Of Alex Rufon
Sent: Wednesday, May 05, 2010 4:08 PM
To: [email protected]
Subject: [Jprogramming] Poor mans way of creating XML document files in J

I actually have a book in my cubicle with the title "Professional Visual Basic 
6 XML" by Jame Britt, Teun Duynstee. Hmmm, I actually have more than 50 books 
in here in my cubicle which includes "A New Kind of Science", "The Mythical 
Man-Month", "Advance Dungeons and Dragons 2nd Edition" (both the Dungeon Master 
Guide and Players Handbook) ...

So going back, cracking open the book again on VB6 XML, I realized how many 
hoops an MS programmer has to jump through to write an XML document. 

Take for example the following XML document:
<?xml version="1.0"?>
<data>
  <user>
    <name>Alex</name>
    <city>Makati</city>
    <country>Philippines</country>
  </user>
  <user>
    <name>Vilma</name>
    <city>Shenzen</city>
    <country>China</country>
  </user>
</data>

So let's say that we have the following data that has to be transformed to XML 
in the format above:
   [rawdata=: 2 3 $ 'Vilma';'Shenzen';'China';'Alex';'Makati';'Philippines'
+-----+-------+-----------+
|Vilma|Shenzen|China      |
+-----+-------+-----------+
|Alex |Makati |Philippines|
+-----+-------+-----------+

Well, my idea is to create formatted nouns and just replace the values as shown 
with this code:
NB. =========================================================
NB. [email protected]   2010 5 5 02 59 58.529
NB. Poor man XML writer
load 'files validate'

NB. ---------------------------------------------------------
NB. XML definition tags
XML_DATA_OPEN=: 0 : 0
<?xml version="1.0"?>
<data>
)

XML_DATA_CLOSE=: '</data>'

XML_USER_OPEN=: '<user>'

XML_USER_CLOSE=: '</user>'

XML_USER_MAP=: > '*' cutopen each cutopen 0 : 0
 <name>*User Name*</name>
 <city>*User City*</city>
 <country>*User Country*</country>
)

NB. =========================================================
NB. Support verbs
boxEMPTY=: <''

boxitems=: <"_1 ^: (0: = L.)

boxedlist2mat=: [: |: boxitems &>

blankout=: 3 : 'boxEMPTY (bx y = <,'' '') } y'

mat2boxedlist=: 3 : 0
dat=. <"1 |: ,each y
opn=. > each dat
emp=. isempty &> opn
num=. bx emp < 2 ~: 3!:0 &> opn
dat=. (, each num { opn) num } dat
ndx=. (i.#dat) -. num
(blankout each ndx { dat) ndx } dat
)

charsep=: 3 : 0
',' charsep y
:
if. L. y do.
  }. ; (x &, @ ":) each y
else.
  , ": y
end.
)

parseUser=: 3 : 0
data=. y
temp=. '' charsep "1 boxedlist2mat (<data) 1 } mat2boxedlist XML_USER_MAP
XML_USER_OPEN,temp,XML_USER_CLOSE
)

NB. =========================================================
NB.*exportToXML a saves data to XML file
NB.
NB. x is: data that is 3 columns by N rows
NB. y is: filename 
exportToXML=: 4 : 0
data=. mat2boxedlist "1 x
xmlfile=. y
temp=. '' charsep parseUser each data
xmlfile fwrites~ XML_DATA_OPEN,temp,XML_DATA_CLOSE
)

NB. =========================================================
NB. End of J script


If you would review the code above, I defined some constants first to handle 
the tags like:
XML_DATA_OPEN
XML_DATA_CLOSE
XML_USER_OPEN
XML_USER_CLOSE

Then I defined a map with:
   XML_USER_MAP
+----------+------------+----------+
| <name>   |User Name   |</name>   |
+----------+------------+----------+
| <city>   |User City   |</city>   |
+----------+------------+----------+
| <country>|User Country|</country>|
+----------+------------+----------+

The idea is to use the 2nd column to as to where to put the actual data. I 
actually use the verb 'parseUser' to actually put the data into the 
XML_USER_MAP. 

So to run this, just execute:
   rawdata exportToXML 'c:\test.xml'
416

Then verify the result in 'C:\TEST.XML' by opening it using FireFox or IE. 

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to