Y.XmlFragment deserialization

Good day,

I am sorry if this topic is very similar to this.
I am not able to get my head around on how to deserialize a string into Y.XmlFragment.

I have a bunch of strings received this way:

yDoc.getXmlFragment('default').toString();

And now I want to transform it back to XmlFragment instance, however, I do not see such function in docs.

String example is this:

<paragraph>This text:</paragraph>
<bulletlist><listitem><paragraph>is without</paragraph></listitem></bulletlist>

(I understand it should / could be wrapped with additional element.)

I tried parsing xml from string and gather the xml elements and xml texts into xml fragment manually, but I am not able to collect it correctly. So I was thinking it must be some easier way, since serialization is available, maybe there is deserialization available as well?

Tried reading source code, but still could not spot anything.

Would appreciate some insights.

Unfortunately I don’t believe a function like this exists. You would need to parse the XML string and traverse it to build up the Y.XmlFragment, as suggested in the linked discussion. It could make for a useful hocuspocus transformer.

Note: You may already be aware of this, but as soon as you do .toString() you lose the change history of the fragment, and thus lose the ability to sync without conflicts. Thus it’s not really correct to call it serialization/deserialization. The YJS shared types contain much more information than a single snapshot of the data, including the client id and clock information that is used by the CRDT. Transforming raw data into YJS shared types may be useful as a one-time event when initializing the app or importing data, but after that you’ll need to modify the YJS instance directly rather than modifying strings or JSON.

3 Likes