KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Decoding BLOBs in XML elements
PRODUCT: 4D | VERSION: 11 | PLATFORM: Mac & Win
Published On: September 24, 2010

When creating XML documents with the 4D DOM XML commands it is possible to store not just the plain text data normally associated with XML, but also Pictures and BLOBs. With a BLOB, basically anything can be stored in your XML document. The DOM SET XML ELEMENT VALUE command can be used to handle this.

You can later use the DOM GET XML ELEMENT VALUE command to retrieve the data from your document. When you are later retrieving the BLOB it is important to note that you cannot just store it after retrieval. This is because when using DOM SET XML ELEMENT VALUE the BLOB is encoded in BASE64 encoding. You can use the DECODE command to access your BLOB again.

Here is a sample of code which creates an XML document storing a file of your choice:

doc:=Open document("")
CLOSE DOCUMENT(doc)

C_BLOB(myBlob)
C_TEXT(vRootRef;vElemRef)
vRootRef:=DOM Create XML Ref("MyRoot")
vElemRef:=DOM Create XML element(vRootRef;"MyBLOB")
DOM SET XML ELEMENT VALUE(vElemRef;myBlob)
DOM EXPORT TO FILE(vRootRef;"mydoc.xml")
DOM CLOSE XML(vRootRef)


As you can see the document will be stored in the element value.

The following code will retrieve the document:

C_BLOB(myBlob)
C_TEXT(vRootRef;vElemRef)
vRootRef:=DOM Parse XML source("mydoc.xml")
vElemRef:=DOM Find XML element(vRootRef;"/MyRoot/MyBLOB")
DOM GET XML ELEMENT VALUE(vElemRef;myBlob)
DECODE(myBlob)

doc:=Open document("")
CLOSE DOCUMENT(doc)
BLOB TO DOCUMENT(Document;myBlob;*)
DOM CLOSE XML(vRootRef)