# Parsing BinaryData XML documents

**URL:** <https://forum.juce.com/t/parsing-binarydata-xml-documents/27943>\
**Category:** General JUCE discussion\
**Created:** [May 31, 2018, 7:28am UTC](https://forum.juce.com/t/parsing-binarydata-xml-documents/27943 "2018-05-31T07:28:20Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![w\_ellis](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/w_ellis/32/176_2.png) [@w\_ellis](https://forum.juce.com/u/w_ellis)\
**Post date:** [May 31, 2018, 7:28am UTC](https://forum.juce.com/t/parsing-binarydata-xml-documents/27943/1 "2018-05-31T07:28:20Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![matkatmusic](https://avatars.discourse-cdn.com/v4/letter/m/dfb087/32.png) [@matkatmusic](https://forum.juce.com/u/matkatmusic)\
**Post date:** [May 31, 2018, 7:42am UTC](https://forum.juce.com/t/parsing-binarydata-xml-documents/27943/2 "2018-05-31T07:42:20Z")

</div>

pass your binary data to:

```auto
MemoryBlock (const void *dataToInitialiseFrom, size_t sizeInBytes)

```

and then use

```auto
String MemoryBlock::toString	( )	const

```

to feed

```auto
static XmlElement* XmlDocument::parse	(	const String & xmlData	)	

```

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [May 31, 2018, 9:01am UTC](https://forum.juce.com/t/parsing-binarydata-xml-documents/27943/3 "2018-05-31T09:01:11Z")

</div>

```auto
auto s = String::createStringFromData (data, size);
std::unique_ptr<XmlElement> xml (XmlDocument::parse (s));

```

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [May 31, 2018, 9:02am UTC](https://forum.juce.com/t/parsing-binarydata-xml-documents/27943/4 "2018-05-31T09:02:57Z")

</div>

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

```auto
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
auto xml = juce::parseXML (BinaryData::foobar_xml);

```

---

<div class="post-metadata">

**Author:** ![matkatmusic](https://avatars.discourse-cdn.com/v4/letter/m/dfb087/32.png) [@matkatmusic](https://forum.juce.com/u/matkatmusic)\
**Post date:** [May 31, 2018, 9:05am UTC](https://forum.juce.com/t/parsing-binarydata-xml-documents/27943/5 "2018-05-31T09:05:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [May 31, 2018, 9:07am UTC](https://forum.juce.com/t/parsing-binarydata-xml-documents/27943/6 "2018-05-31T09:07:43Z")

</div>

> [@matkatmusic](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![w\_ellis](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/w_ellis/32/176_2.png) [@w\_ellis](https://forum.juce.com/u/w_ellis)\
**Post date:** [May 31, 2018, 1:58pm UTC](https://forum.juce.com/t/parsing-binarydata-xml-documents/27943/7 "2018-05-31T13:58:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![w\_ellis](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/w_ellis/32/176_2.png) [@w\_ellis](https://forum.juce.com/u/w_ellis)\
**Post date:** [May 31, 2018, 7:42pm UTC](https://forum.juce.com/t/parsing-binarydata-xml-documents/27943/8 "2018-05-31T19:42:51Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![mjokipii](https://avatars.discourse-cdn.com/v4/letter/m/e47c2d/32.png) [@mjokipii](https://forum.juce.com/u/mjokipii)\
**Post date:** [November 9, 2022, 8:41pm UTC](https://forum.juce.com/t/parsing-binarydata-xml-documents/27943/9 "2022-11-09T20:41:57Z")

</div>

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

```auto
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.
