XML output with dynamic subparts

Post any questions you have about using the Verj.io Studio, including client and server-side programming with Javascript or FPL, and integration with databases, web services etc.

Moderators: Jon, Steve, Ian, Dave

vin@haarlem
Ebase User
Posts: 8
Joined: Wed Aug 14, 2013 9:18 am

XML output with dynamic subparts

#1

Postby vin@haarlem » Wed Nov 27, 2013 12:01 pm

I need to create a XML output with nested files within subparts (see example). What would be the best way to achieve this given the fact that the subparts are comming from a table and the files must be added to some subparts but not all? So subpart 1 doesn't have any files and subpart 2 got 4 files and subpart 3 got 1 one and so on. I hope the example below clarifies what I need.

Thanks, Vincent

Code: Select all

<root>
    <generic_data1>string</generic_data1>
    <generic_data2>string</generic_data2>
    <generic_data3>string</generic_data3>
    <sub>
        <sub_data1>string</sub_data1>
        <sub_data2>string</sub_data2>
        <sub_data3>string</sub_data3>
    </sub>
    <sub>
        <sub_data1>string</sub_data1>
        <sub_data2>string</sub_data2>
        <sub_data3>string</sub_data3>
        <files>
            <file>
                <filename>string</filename>
                <filedata>string</filedata>
            </file>
            <file>
                <filename>string</filename>
                <filedata>string</filedata>
            </file>
            <file>
                <filename>string</filename>
                <filedata>string</filedata>
            </file>
            <file>
                <filename>string</filename>
                <filedata>string</filedata>
            </file>
        </files>
    </sub>
    <sub>
        <sub_data1>string</sub_data1>
        <sub_data2>string</sub_data2>
        <sub_data3>string</sub_data3>
        <files>
            <file>
                <filename>string</filename>
                <filedata>string</filedata>
            </file>
        </files>
    </sub>
    <sub>
        <sub_data1>string</sub_data1>
        <sub_data2>string</sub_data2>
        <sub_data3>string</sub_data3>
    </sub>
</root>
0 x

Jon
Moderator
Moderator
Posts: 1342
Joined: Wed Sep 12, 2007 12:49 pm

#2

Postby Jon » Wed Nov 27, 2013 3:46 pm

There are two ways to do this. The first is to just construct the XML document line by line using a Javascript script - example is shown below. The second way is to use two tables within an XML Resource configured with the correct XPath - this requires almost no script code, but it's harder to set up - no example shown of this.

For the first option, you can construct an XML document something like this. This example is pasted from the online doc : Index > Javascript > JavaScript E4X Support (API for XML) - last paragraph, and shows creating a purchase order from a table (PURCHASE_ORDERS) and uses the built-in XML API (called E4X):

Code: Select all

//create a new purchase order
var purchaseOrder = <purchaseOrder/>;
//iterate rows
var rows = tables.PURCHASE_ORDERS.rows;
//create row index
var index = 0;
while&#40;rows.next&#40;&#41;&#41;
&#123;
     //append new item to purchaseOrder
      var item = purchaseOrder.appendChild&#40;<item/>&#41;.item;
 
      var name = tables.PURCHASE_ORDERS.ITEM.value;
      var price = tables.PURCHASE_ORDERS.PRICE.value;
      var quantity = tables.PURCHASE_ORDERS.QUANTITY.value;

      item&#91;index&#93;.appendChild&#40;<name>&#123;name&#125;</name>&#41;;
      item&#91;index&#93;.appendChild&#40;<price>&#123;price&#125;</price>&#41;;
      item&#91;index&#93;.appendChild&#40;<quantity>&#123;quantity&#125;</quantity>&#41;;
      index++; //increment row index
&#125;
log&#40;purchaseOrder.toString&#40;&#41;&#41;;
 
In your case, you need to check whether your subparts exist and only add them if they do.

The final section of the example script loads the document into an XML Resource. This is very handy as you can then use the XML Resource adapters to save it to file, send it to a URL etc. You can also use this technique to build documents in a Web Services Resource.

Code: Select all

//set the purchase order document
resources.XML_EXAMPLE.setDocument&#40;"MY_DOC", purchaseOrder&#41;;
//write the document
resources.XML_EXAMPLE.write&#40;&#41;;
Hopefully this is enough to get you going.

Regards
Jon
0 x


Who is online

Users browsing this forum: No registered users and 6 guests