Decode base64 with embedded XML

This would seem to be a simple problem, but I haven't been able to find out why I cannot
successfully decode a HTML stream with embedded XML.

This is the base64 string:

LS1ib3VuZGFyeQ0KQ29udGVudC1EaXNwb3NpdGlvbjogZm9ybS1kYXRhOyBuYW1lPSJNb3ZlRGV0ZWN0aW9uLnhtbCI7IGZpbGVuYW1lPSJNb3ZlRGV0ZWN0aW9uLnhtbCINCkNvbnRlbnQtVHlwZTogYXBwbGljYXRpb24veG1sDQpDb250ZW50LUxlbmd0aDogNTQ5DQoNCjw/eG1sIHZlcnNpb249IjEuMCIgZW5jb2Rpbmc9IlVURi04Ij8+DQo8RXZlbnROb3RpZmljYXRpb25BbGVydCB2ZXJzaW9uPSIyLjAiIHhtbG5zPSJodHRwOi8vd3d3Lmhpa3Zpc2lvbi5jb20vdmVyMjAvWE1MU2NoZW1hIj4NCjxpcEFkZHJlc3M+MTAuODguMS4xOTY8L2lwQWRkcmVzcz4NCjxwb3J0Tm8+Mzk1MDE8L3BvcnRObz4NCjxwcm90b2NvbD5IVFRQPC9wcm90b2NvbD4NCjxtYWNBZGRyZXNzPjY0OjMxOjM5OjMwOmNjOmU5PC9tYWNBZGRyZXNzPg0KPGNoYW5uZWxJRD4xPC9jaGFubmVsSUQ+DQo8ZGF0ZVRpbWU+MjAyNC0wMS0xNVQxODo0NDo1OS0wNjowMDwvZGF0ZVRpbWU+DQo8YWN0aXZlUG9zdENvdW50PjE8L2FjdGl2ZVBvc3RDb3VudD4NCjxldmVudFR5cGU+Vk1EPC9ldmVudFR5cGU+DQo8ZXZlbnRTdGF0ZT5hY3RpdmU8L2V2ZW50U3RhdGU+DQo8ZXZlbnREZXNjcmlwdGlvbj5Nb3Rpb24gYWxhcm08L2V2ZW50RGVzY3JpcHRpb24+DQo8Y2hhbm5lbE5hbWU+Q2FtZXJhIDAxPC9jaGFubmVsTmFtZT4NCjwvRXZlbnROb3RpZmljYXRpb25BbGVydD4NCg0KLS1ib3VuZGFyeS0tDQo=

This is the function I used:

String decode64(String raw) {
def decodedBytes = raw.decodeBase64()
def decoded = new String(decodedBytes, "UTF-8")
return decoded
}

The decoded base64 should be this:

--boundary
Content-Disposition: form-data; name="MoveDetection.xml"; filename="MoveDetection.xml"
Content-Type: application/xml
Content-Length: 549

<?xml version="1.0" encoding="UTF-8"?>

<EventNotificationAlert version="2.0" xlmlns="http://www.hikvision.com/ver20/XMLSchema">
<ipAddress>10.88.1.196</ipAddress>
<portNo>39501</portNo>
<protocol>HTTP</protocol>
... more of the same ...
--boundary--

But what I get is everything decoded but missing all text in < > brackets:

--boundary Content-Disposition: form-data; name="MoveDetection.xml"; filename="MoveDetection.xml" Content-Type: application/xml Content-Length: 549 10.88.1.196 39501 HTTP 64:31:39:30:cc:e9 1 2024-01-15T19:07:22-06:00 1 VMD active Motion alarm Camera 01 --boundary--

Decoding the base64 stream in another application results in the proper text.

Any ideas what is wrong here?
Any help would be greatly appreciated.

Regards,
Gene

Are you viewing this in the Logs window in Hubitat? The browser will render away the XML markup as if it is HTML tags.

Try this for displaying it:

groovy.xml.XmlUtil.escapeXml(decoded)

3 Likes

I guess I should add, the data is actually there even though it doesn't display in the Logs.

So you should be able to parse and interact with it directly after your decoded step.

Thanks for the quick reply. You are right, the XML is there, it's just not visible. Using your suggestion to escape the text before printing made it visible and revealed the true cause of my problem: The HikVision camera has a different responses depending on the event type. I was looking for "<?xml version="1.0" as the start of the XML data, but not every camera event sends this. I finally succeeded by just looking for "<EventNotificationAlert" to find the XML. And now xml.'eventType' properly has the camera event for all event types.

Working with embedded HTML data is quirky. Also, the camera sends an embedded JPEG image (about 150kByte) which causes the Hubitat to issue excessive load warnings. I simply trimmed the raw data to 3 kByte which is enough to get the XML data I needed.

Thanks again for your tip - it helped solve my problem. The driver is now functional. Other HikVision drivers I've tried all have failed, probably for the same reason.

Regards,
Gene

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.