# Creating an XML Document

**URL:** <https://forum.crystal-lang.org/t/creating-an-xml-document/8668>\
**Category:** Help & Support\
**Created:** [January 26, 2026, 10:25am UTC](https://forum.crystal-lang.org/t/creating-an-xml-document/8668 "2026-01-26T10:25:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Carlos](https://avatars.discourse-cdn.com/v4/letter/c/ecae2f/32.png) [@Carlos](https://forum.crystal-lang.org/u/Carlos)\
**Post date:** [January 26, 2026, 10:25am UTC](https://forum.crystal-lang.org/t/creating-an-xml-document/8668/1 "2026-01-26T10:25:29Z")

</div>

How can I programmatically build an XML document? (the object, not the text)

There’s `XML.parse` that creates one from a string, but I can’t find anything else. `XML.build` (what I thought I would use) returns a string. Am I supposed to build it, get the string and then parse it? 😬

And once I have it, how can I modify it?

---

<div class="post-metadata">

**Author:** ![initd](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/initd/32/3182_2.png) [@initd](https://forum.crystal-lang.org/u/initd)\
**Post date:** [January 26, 2026, 11:28am UTC](https://forum.crystal-lang.org/t/creating-an-xml-document/8668/2 "2026-01-26T11:28:00Z")

</div>

[nokogiri](https://github.com/sparklemotion/nokogiri)

[https://github.com/sparklemotion/nokogiri/wiki/Cheat-sheet#creating-and-working-with-documents](https://github.com/sparklemotion/nokogiri/wiki/Cheat-sheet#creating-and-working-with-documents)

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [January 26, 2026, 12:40pm UTC](https://forum.crystal-lang.org/t/creating-an-xml-document/8668/3 "2026-01-26T12:40:49Z")

</div>

Nokogiri is a Ruby gem. It doesn’t work in Crystal.  
Looks like Nokogiri requires creating a document by parsing an XML source (which may be empty), and then you can mutate that document.

Unfortunately, Crystal’s stdlib doesn’t really provide any APIs for mutating an XML document.  
See [XML: improve the DOM interface · Issue #15915 · crystal-lang/crystal · GitHub](https://github.com/crystal-lang/crystal/issues/15915)
