views:

39

answers:

1

Hi

We have an requirement in our project to browse & upload an XML file and show it on the datagrid, edit the grid and then save the contents to the database. I'm able to see examples that take a XML from a specific folder and show on the datagrid, but not able to see any examples that browse for a XML and then upload it. It would be great if someone can point me to the examples or some sample code. Our XML looks like:

<VisitImportList>
    <Visit>
        <AuditDetails>
            <UpdateUser>ADMIN</UpdateUser>
            <UpdateTimestamp>2010-10-22T16:25:26.593Z</UpdateTimestamp>
        </AuditDetails>
        <VisitId>3</VisitId>
        <MeasurementCollectionId>4</MeasurementCollectionId>
        <WeightConfirmationCode>5</WeightConfirmationCode>
        <PrefilledIndicator>true</PrefilledIndicator>
        <VisitDate>2010-10-22T16:25:26.593Z</VisitDate>     
    </Visit>
    <Visit>
        <AuditDetails>
            <UpdateUser>ADMIN</UpdateUser>
            <UpdateTimestamp>2010-10-22T16:25:26.593Z</UpdateTimestamp>
        </AuditDetails>
        <VisitId>3</VisitId>
        <MeasurementCollectionId>3</MeasurementCollectionId>
        <BloodPressureConfirmationCode>4</BloodPressureConfirmationCode>
        <PrefilledIndicator>true</PrefilledIndicator>
        <VisitInvalidCode>1</VisitInvalidCode>
        <VisitInvalidReasonText>No Dates</VisitInvalidReasonText>
    </Visit>
</VisitImportList>

Ok I have been able to show the data on the grid using FileReference for upload and XML/XMLListCollection. Now the issue is when I try to save to the database. I did not want to create a new thread, so I have added my question to here:

private function saveVisit(event:MouseEvent): void
            {
var decoder:SimpleXMLDecoder = new SimpleXMLDecoder();
                var data:Object = decoder.decodeXML(xmlDoc);

                var array:Array = ArrayUtil.toArray(data.VisitImportList.Visit);
                tempCollection = new ArrayCollection(array);

Now I have the data in my arraycollection (tempCollection). But it holds generic objects and I need to convert them to Visit Object. So I want to loop through the ArrayCollection, convert the object in to specific custom Visit Objects and then add them to another Collection (I'm sure this is not the right way to do, but I'm not able to come up with an alternate):

     for (var i:int = 0; i < tempCollection.length; ++i) 
                        {               
                            model.visit = new Visit();  
                            model.visit = Visit(tempCollection.getItemAt(i, 0)); // This line gives the error Type Coercion failed: cannot convert Object@1d4e4719 to com.model.Visit.
                            model.visit = tempCollection.getItemAt(i) as Visit; // This line always has Visit as null eventhough the tempCollection has 2 objects

model.pvList.visits.addItemAt(Visit, i);
}

So can someone help on how to loop through the ArraCollection and convert the AS Object to custom Visit object and then add to another ArrayCollection OR an easier way to do this

Thanks

Harish

+1  A: 

If you are working with AIR application, you can easily access resources in your computer using File and FileStream objects in Flex.

http://livedocs.adobe.com/flex/3/html/help.html?content=Filesystem_16.html

Get the XML object and then convert it to XMLListCollection from its item command i.e.

var xmlProvider:XMLListCollection = new XMLListCollection(xml.Visit);

Set this provider to your data grid and write script to upload and save this data the way you want.

But instead if you are writing web application. You can't access resources that are not in your flex temp files path on your client. Or you have to give flex access to the folder/file on client machine using "Global Security Settings". In this case same AIR api should work.

However, if you don't want that either then obviously you have to upload the file on server, read that object using HTTPService back at the client, create XMLListCollection object and load the data in DataGrid. This would help

  1. Upload File
  2. HTTP Service + DataGrid Sample for XML file on server
Faheem
Thanks Faheem. I was able to use the FileReference for browse/upload and XML/XMLListCollection to show on the datagrid. But now I have another issue when saving to the DB. I have appended this to the main question. Please help anyone
Harish
This is the simpler way to do it... http://cookbooks.adobe.com/post_Using_a_Factory_Method_to_convert_raw_XML_into_cus-12764.html
Faheem
Awesome Faheem, almost solved except 1 issue :). visit.visitDate = VisitNode.VisitDate; Here I'm getting an error: Type Coercion failed: cannot convert XMLList@1d76eee9 to Date. Do I need to format the date? How to please.
Harish
Sorry my bad....new Date(string) fixed it. Thanks Faheem a lot. appreciate it. I will mark your answer as right. Thanks so much :). Will get back here if any more issues :)
Harish