Reputation: 1
I need to create a special purpose file format which should have an XML "header" and a large chunk of binary data after this header. I would like to use the tinyxml2 library to parse the xml. After that, I shall pull the binary data into a single large array of bytes. The xml header's size is not fixed. The xml header is not going to be very large, and the binary data would be typically around 10-20 MB. I would read-in many files, so efficiency is valuable.
tinyxml2 provides two methods to read from a file LoadFile( const char* filename ) and LoadFile( FILE* ) but these won't work if non-xml binary data is appended after the last element.
There is a concept of CDATA but I am not sure if that is the most efficient way since the binary data would be part of tinyxml2's parsing effort.
What is your advice to formatting this file (i.e. special tags, size-of-header info etc..) and reading the header with tinyxml2? Thank you!
Upvotes: 0
Views: 235
Reputation: 1
I realized that tinyxml2 also provides a XMLDocument.Parse() function to read-in from a string. It will also accept the number of bytes to read as a parameter. So I place a special header tag as the first line of my file format which indicates the number of bytes before binary data begins:
<!-- MY_FILE_FORMAT HeaderBytes="956" -->
<?xml version="1.0" encoding="UTF-8"?>
<Element> ... </Element>
binary-data-starts-here
This way I can extract the xml part only from the file opened in binary mode, and use the Parse() function on it. I created the tag to be in xml-comment format so it is properly displayed and highlighted in my editor.
Still interested to hear if there is a more "elegant" way.
Upvotes: 0