Read XML String and load the data in Controls

We can read a XML string and parse the data based on the XML tags and load the data into our UI COntrols.

In the following example a Customer data is sent as XML String, We are going to retrieve the Customer Name, Address and City and load the same in the Edit Controls.


sXML is string ="<?xml version='1.0' encoding='utf-8'?><XMLHDR><RESULT>TRUE</RESULT><CUSTOMER><NAME>TESTNAME</NAME><ADDRESS>12 A BLOCK</ADDRESS><STREET>15TH STREET</STREET><CITY>CHENNAI</CITY></CUSTOMER></XMLHDR>"

XMLDocument("XMLOrd", sXML
XMLFirst("XMLOrd")
XMLChild("XMLOrd")

IF XMLElementName("XMLOrd") = "RESULT" THEN
       IF XMLData("XMLOrd") = "TRUE" THEN                
              WHILE XMLOut("XMLOrd")=False
                      XMLNext("XMLOrd")
                      IF XMLElementName("XMLOrd") = "CUSTOMER" THEN
                             XMLChild("XMLOrd")
                      END
                      IF XMLElementName("XMLOrd") = "NAME" THEN
                             Edt_Name = XMLData("XMLOrd")                     
                      END
                      IF XMLElementName("XMLOrd") = "ADDRESS" THEN
                             Edt_Address = XMLData("XMLOrd")                          
                      END
                      IF XMLElementName("XMLOrd") = "STREET" THEN
                             Edt_Street = XMLData("XMLOrd")                           
                      END                  
                      IF XMLElementName("XMLOrd") = "CITY" THEN
                             Edt_City = XMLData("XMLOrd")                     
                      END
                      //                    XMLNext("XMLOrd")
              END
       END
END
XMLClose("XMLOrd")


We can use this same logic to read multiple XML records and load in our HFSQL Database or any other database.

-----------------------------------------------
In case we have the XML in a .xml file.  we can use the following method to read the xml file and load it in a string variable and then process the same.


MyDoc is string = fLoadText("D:\\XMLDoc\example.xml")
XMLDocument("DOM", MyDoc)

Comments

Popular posts from this blog

Word Processing Control

Track Directory