Parsing BinaryData XML documents

Apologies if this is a really stupid question… I’m trying to find the best way to embed an XML file into an executable build and then parse the contents. Previously I had been doing this with relatively small files and just a straight conversion to String and then using static XmlElement* XmlDocument::parse ( const String & xmlData ) to parse was working fine.

However, now I have a couple of larger files, which the Projucer converts to binary representations due to the MS compiler string literal limitation. My first attempt was to use the String::fromUTF8() static method to create a String, but the contents are being truncated. When the XML file is outside the executable I just parse it using the static XmlElement* XmlDocument::parse ( const File & file ) method.

I couldn’t find an obvious way to get the contents of a BinaryData element into a File without writing out to disk first. Am I missing something, or is there a better way to think about this?

pass your binary data to:

MemoryBlock (const void *dataToInitialiseFrom, size_t sizeInBytes)

and then use

String MemoryBlock::toString	(		)	const

to feed

static XmlElement* XmlDocument::parse	(	const String & 	xmlData	)	
auto s = String::createStringFromData (data, size);
std::unique_ptr<XmlElement> xml (XmlDocument::parse (s));
1 Like

oh, but I just noticed you said it was in BinaryData. That’s even easier:

std::unique_ptr<XmlElement> xml (XmlDocument::parse (BinaryData::foobar_xml));

EDIT: I just got notified that people are 'heart’ing this post for some reason, but the code here is out of date. A much better way to write it is now just

auto xml = juce::parseXML (BinaryData::foobar_xml);
4 Likes

what happens internally with that last one? is the BinaryData:: converted to a File or String behind the scenes?

A string. It’s a const char*, there’s no implicit conversion from that to a File.

Awesome, thanks to both of you for the pointers, I’ll try that out tonight

Thanks again for the help. It turned out to be a problem with the XML file in the end. For whatever reason it was encoded as UTF-8 with BOM and I ran into the same problems with the ideas above as I did with my previous attempts. Just re-saved the file with UTF-8 encoding and all is now well!

If your XML contains UTF, you most likely need to say

auto xml = juce::parseXML (juce::CharPointer_UTF8 (BinaryData::foobar_xml));

Sorry to bump an old thread, but for anyone else watching JUCE blow up with assertions, while trying to import an XML resource.