< Day Day Up > |
The program in Listing 7.6 shows how an XML structure can be built that sends only the data absolutely needed to reconstruct the two tables. Listing 7.6. FoxPro Program to Create Hierarchical XML* Program-ID...: BuildXMLHierarchy.PRG CLOSE ALL CLEAR ALL CLEAR USE InvoiceS IN A USE INVDETLS IN B lcXML = "" Add2XML ( [<?xml version = "1.0" encoding="Windows-1252" standalone="yes"?>] ) SELECT Invoices Add2XML ( [<Invoices>] ) NoFac = 0 SCAN NoFac = NoFac + 1 Add2XML ( [<Invoice>], 1 ) FOR I = 1 TO FCOUNT() fld = LOWER(FIELD(I)) Add2XML ( [<] + fld + [>] + ALLTRIM(TRANSFORM(EVALUATE(fld))) ; + [</] + fld + [>], 2) ENDFOR SELECT INVDETLS SET FILTER TO INVNUM = InvoiceS.INVNUM NoLine = 0 SCAN NoLine = NoLine + 1 Add2XML ( [<Line>], 2 ) FOR I = 1 TO FCOUNT() fld = LOWER(FIELD(I)) Add2XML ([<] + fld + [>] + ALLTRIM(TRANSFORM(EVALUATE(fld))) ; + [</] + fld + [>], 3) ENDFOR Add2XML ( [</Line>], 2 ) ENDSCAN SELECT Invoices Add2XML ( [</Invoice>], 1 ) ENDSCAN Add2XML ( [</Invoices>] ) STRTOFILE( lcXML, "TEST.XML" ) MODIFY FILE TEST.XML NOMODIFY FUNCTION Add2XML PARAMETERS Texto, Indent Indent = IIF ( EMPTY ( Indent ), 0, Indent ) lcXML = lcXML + IIF(EMPTY(lcXML),"",CHR(13)) + REPLICATE(CHR(9),Indent) + Texto RETURN The resulting XML produced from two invoice headers and four detail lines, two for each invoice, appears in Listing 7.7. Listing 7.7. Hierarchical XML<?xml version = "1.0" encoding="Windows-1252" standalone="yes"?> <Invoices> <Invoice> <invnum>141</invnum> <clientid>41</clientid> <date>04/01/2004</date> <total>192</total> <Line> <invnum>141</invnum> <linenum>1</linenum> <quantity>3</quantity> <productid>303142-A</productid> <unitprice>12.95</unitprice> <extended>38.85</extended> </Line> <Line> <invnum>141</invnum> <linenum>2</linenum> <quantity>4</quantity> <productid>1041202</productid> <unitprice>3.15</unitprice> <extended>12.60</extended> </Line> </Invoice> <Invoice> <invnum>142</invnum> <clientid>43</clientid> <date>04/03/2004</date> <total>225</total> <Line> <invnum>142</invnum> <linenum>1</linenum> <quantity>1</quantity> <productid>2022201</productid> <unitprice>25</unitprice> <extended>25</extended> </Line> <Line> <invnum>142</invnum> <linenum>2</linenum> <quantity>2</quantity> <productid>2016615</productid> <unitprice>89.95</unitprice> <extended>179.90</extended> </Line> </Invoice> </Invoices> Open Visual Studio and select File, Open, File from the menu to open Hierarchical.XML . You'll see the screen shown in Figure 7.3. Figure 7.3. Data table view of a hierarchical XML file.It's true that hierarchical XML reduces file size a little, compared to sending the same amount of data as two XML tables. However, the programming required to traverse a hierarchy and load the data into a treeview is more complicated than simply loading the hierarchy from a parent table and a child table (although some day I expect that it will become easier). Until then, broadband is cheaper than programming, so I'd recommend that you just send the two tables and don't bother representing their contents as a hierarchy. |
< Day Day Up > |