From XML to array

mazza paolo mazzapaolo at libero.it
Wed Feb 24 08:44:33 EST 2010


I need to import the data of an XML file to an ARRAY having the nodes as keys.

For example:  I need to transform an XML file like this ...

<?xml version="1.0"?>
<records>
	<record IDnum="1">
		<firstName>Steve</firstName>
		<lastName>Jobs</lastName>
		<roomNums>
<roomNum1>001</roomNum1>
<roomNum2>054</roomNum2>
<roomNum3>545</roomNum1>
</roomNums>
		<phoneExt>345</phoneExt>
		<parkingSlot>100</parkingSlot>
	</employee>
	<record IDnum="2">
		<firstName>Bill</firstName>
		<lastName>Gates</lastName>
		<roomNums>
<roomNum1>454</roomNum1>
<roomNum2>656</roomNum2>
</roomNums>
		<phoneExt>666</phoneExt>
		<parkingSlot>987</parkingSlot>
	</employee>
	<record IDnum="3">
		<firstName>Linus</firstName>
		<lastName>Torvald</lastName>
	<roomNums>
<roomNum1>001</roomNum1>
<roomNum2>054</roomNum2>
<roomNum3>545</roomNum3>
<roomNum4>545</roomNum4>
</roomNums>
		<phoneExt>479</phoneExt>
		<parkingSlot>123</parkingSlot>
	</employee>
	<record IDnum="4">
		<firstName>Kevin</firstName>
		<lastName>Miller</lastName>
		<roomNum>100</roomNum>
		<phoneExt>421</phoneExt>
		<parkingSlot>987</parkingSlot>
	</employee>
</records>

to  an array like this (se the combined version of the array)

/records/record[1]record:
/records/record[1]record/firstName:Steve
/records/record[1]record/lastName:Jobs
/records/record[1]record/parkingSlot:100
/records/record[1]record/phoneExt:345
/records/record[1]record/roomNums:
/records/record[1]record/roomNums/roomNum1:001
/records/record[1]record/roomNums/roomNum2:054
/records/record[1]record/roomNums/roomNum3:545
/records/record[2]record:
/records/record[2]record/firstName:Bill
/records/record[2]record/lastName:Gates
/records/record[2]record/parkingSlot:987
/records/record[2]record/phoneExt:666
/records/record[2]record/roomNums:
/records/record[2]record/roomNums/roomNum1:454
/records/record[2]record/roomNums/roomNum2:656
/records/record[3]record:
/records/record[3]record/firstName:Linus
/records/record[3]record/lastName:Torvald
/records/record[3]record/parkingSlot:123
/records/record[3]record/phoneExt:479
/records/record[3]record/roomNums:
/records/record[3]record/roomNums/roomNum1:001
/records/record[3]record/roomNums/roomNum2:054
/records/record[3]record/roomNums/roomNum3:545
/records/record[3]record/roomNums/roomNum4:545
/records/record[4]record:
/records/record[4]record/firstName:Kevin
/records/record[4]record/lastName:Miller
/records/record[4]record/parkingSlot:987
/records/record[4]record/phoneExt:421
/records/record[4]record/roomNum:100


The following RevTalk  script does the job. I wonder if there is an easyer (and more efficient) way to accomplish this task with Revolution.

local STRUTTURA,NODO1,LISTAARRAY, tDocID

on mouseUp
   local J, tNUMERORECORDS, tParentNode
      put field "DocID" into tDocID
   --the number of records
   put revXMLRootNode(tDocID) into tParentNode
   put revXMLNumberOfChildren(tDocID,tParentNode,"record",0) into tNUMERORECORDS
   
   repeat with J=1 to tNUMERORECORDS
   put "/records/record[" & J & "]" into NODO1
   CreaListaValori
   combine LISTAARRAY with return and  ":"
   put LISTAARRAY & return after message
   end repeat
end mouseUp

on CreaListaValori
   local NOMECAMPO, tNUMERO, VALORECAMPO, LISTA

   put revXMLTree(tDocID, NODO1, cr, tab, false, -1) into STRUTTURA
    repeat with tNUMERO=1 to the number of lines of STRUTTURA
      put line tNUMERO of STRUTTURA into NOMECAMPO
      put CompletaCampo(NOMECAMPO,tNUMERO) into NOMECAMPO
      put revXMLNodeContents(tDocID,NOMECAMPO) into VALORECAMPO
      PUT VALORECAMPO into LISTAARRAY[NOMECAMPO]
   end repeat

end CreaListaValori


function CompletaCampo  NOMECAMPO tNUMERO
   local NUMEROX, LINEA
   set itemdelimiter to TAB
   put the number of items of NOMECAMPO into NUMEROX
   repeat with k=1 to NUMEROX
      if item K of LINEA is empty then
         put CercaNomeCampo(tNUMERO, k ) into item K of LINEA
      end if
   end repeat
   replace tab with "/" in LINEA
   put NODO1 & LINEA into LINEA
   return LINEA
end CompletaCampo

function CercaNomeCampo tNUMERO tPosizione
   local NUMEROX, LINEA, K, NOME
   set itemdelimiter to tab
   repeat with K=tNumero down to 1
      put item tPosizione of line K of STRUTTURA into NOME
      if NOME is not empty then
        return NOME
         exit repeat
      end if
   end repeat
end CercaNomeCampo



Thanks a lot,  Paolo Mazza




More information about the use-livecode mailing list