Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
|
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 11th, 2003, 10:24 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 231
Thanks: 0
Thanked 0 Times in 0 Posts
Default Binary Data in XML

Is there any way of including raw binary data in an XML file?

I have looked at the CDATA section and was under the impression that this could be used to embed raw binary data (including characters that outside of the CDATA section would be seen as illegal characters, not just < and >).

However when I use the MSXML 4.0 component in VB with raw binary data embedded in a CDATA section an error is raised when loading the document. The Microsoft XMLDOM ActiveX component in a web page on the other hand has no problems loading and parsing the embedded raw binary data, nor does IE6 for that matter.

Can anyone tell me the definitive answer to whether raw binary data can be included in a CDATA section of an XML file?

Regards
Owain Williams
__________________
Regards
Owain Williams
 
Old November 11th, 2003, 11:22 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

In xml 1.0 you cannot have any character belo 0x20 except for 9 (tab), (10) newline and 13 (carriage return.
To embed others you can use (amongst other things) Base64 encoding. It's very easy if you are using msxml parser and here are two scripts to try it out, create a simple MS Word doc called hello1.doc or another file if you wish but change line two as appropriate:
Code:
//Save this as BinaryToXml.js
var WORD_PATH = "hello1.doc";
var XML_PATH = "word.xml"

function main()
{
  var oDoc = new ActiveXObject("MSXML2.DOMDocument.4.0");
  oDoc.async = false;
  var oStream = new ActiveXObject("ADODB.Stream");
  oStream.type = 1; //Binary
  oStream.open();
  oStream.loadFromFile(WORD_PATH);
  var oNode = oDoc.createProcessingInstruction("xml", "version='1.0' encoding='utf-8'")
  oDoc.insertBefore(oNode, oDoc.childNodes[0]);
  var oRoot = oDoc.createElement("Root")
  oDoc.documentElement = oRoot
  oRoot.setAttribute("xmlns:dt", "urn:schemas-microsoft-com:datatypes");
  oNode = oRoot.appendChild(oDoc.createElement("wordDoc"));
  oNode.dataType = "bin.base64";
  oNode.nodeTypedValue = oStream.read();
  oStream.close();
  oDoc.save(XML_PATH);
}

main();
Then this to read from first file:
Code:
//Save this as XmlToBinary.js
var WORD_PATH = "hello2.doc";
var XML_PATH = "word.xml"

function main()
{
  var oDoc = new ActiveXObject("MSXML2.DOMDocument.4.0");
  oDoc.async = false;
  oDoc.load(XML_PATH);
  var oStream = new ActiveXObject("ADODB.Stream");
  oStream.type = 1; //Binary
  oStream.open();
  oStream.write(oDoc.documentElement.selectSingleNode("wordDoc").nodeTypedValue);
  oStream.savetoFile(WORD_PATH);
  oStream.close();  
}

main();
Joe (MVP - xml)





Similar Threads
Thread Thread Starter Forum Replies Last Post
XML to/from binary format aliirfan84 .NET Framework 2.0 1 April 17th, 2007 06:34 AM
XML to/from binary format aliirfan84 XML 1 March 29th, 2007 12:37 PM
XML to/from binary format aliirfan84 C# 2005 1 March 29th, 2007 10:27 AM
XML to/from binary format aliirfan84 General .NET 0 March 29th, 2007 07:33 AM
Converting from binary to xml Morrislgn Pro VB.NET 2002/2003 0 February 2nd, 2006 12:46 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.