# Xml\_write to a zipfile

**URL:** <https://swi-prolog.discourse.group/t/xml-write-to-a-zipfile/9819>\
**Category:** General\
**Tags:** how-to\
**Created:** [September 16, 2026, 3:10pm UTC](https://swi-prolog.discourse.group/t/xml-write-to-a-zipfile/9819 "2026-09-16T15:10:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mgondan1](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/mgondan1/32/803_2.png) [@mgondan1](https://swi-prolog.discourse.group/u/mgondan1)\
**Post date:** [September 16, 2026, 3:10pm UTC](https://swi-prolog.discourse.group/t/xml-write-to-a-zipfile/9819/1 "2026-09-16T15:10:48Z")

</div>

I want to write an xml to a zip file, this is what I tried:

```prolog
zip_open("new.zip", write, Z, []), 
zipper_open_new_file_in_zip(Z, "imsmanifest.xml", S, []), 
xml_write(S, element(elem, [], []), []), 
close(S), 
close(Z).

```

An error is raised on the encoding:

```prolog
Call: (15) sgml_write:xml_write(<stream>(000002284659f7e0), element(elem, [], []), []) ? skip
ERROR: Domain error: `xml_encoding' expected, found `octet'

```

Can I specify this somewhere?

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [September 16, 2026, 3:40pm UTC](https://swi-prolog.discourse.group/t/xml-write-to-a-zipfile/9819/2 "2026-09-16T15:40:09Z")

</div>

> [@mgondan1](#):
>
> An error is raised on the encoding:

xml\_write/3 wants a text stream. This works fine.

```prolog
t :-
    setup_call_cleanup(
        zip_open("new.zip", write, Z, []),
        setup_call_cleanup(
            zipper_open_new_file_in_zip(Z, "imsmanifest.xml", S, []),
            ( set_stream(S, encoding(utf8)),
              xml_write(S, element(elem, [], []), [])
            ),
            close(S)),
        zip_close(Z)).

```

You may like to use library(archive) though. library(zip) is a small public layer with a much more limited interface than library(archive) that exposes the built-in zip functionality (SWI-Prolog saved states are zip files).

---

<div class="post-metadata">

**Author:** ![mgondan1](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/mgondan1/32/803_2.png) [@mgondan1](https://swi-prolog.discourse.group/u/mgondan1)\
**Post date:** [September 17, 2026, 10:05am UTC](https://swi-prolog.discourse.group/t/xml-write-to-a-zipfile/9819/3 "2026-09-17T10:05:33Z")

</div>

Thank you! The problem has been solved.

…as tradition requires, a problem is only solved if one or more new problems are raised 😅 xml\_write does not seem to add “newlines” in the zip file (in normal textfiles, it does).

```prolog
setup_call_cleanup(
        zip_open("new.zip", write, Z, []),
        setup_call_cleanup(
            zipper_open_new_file_in_zip(Z, "imsmanifest.xml", S, []),
            ( set_stream(S, encoding(utf8)), set_stream(S, newline(posix)),
              xml_write(S, [element(elem, [], [element(inside, [], [])])], [layout(false), indent(2)])
            ),
            close(S)),
        zip_close(Z)).
Z = <zipper>(0x622877233340),
S = <stream>(0x62287723a500).

21 ?- halt.
matthias@PC105-C720:~$ unzip new.zip
Archive: new.zip
replace imsmanifest.xml? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
  inflating: imsmanifest.xml
matthias@PC105-C720:~$ cat imsmanifest.xml
<?xml version="1.0" encoding="UTF-8"?>

  <elem><inside/></elem>matthias@PC105-C720:~$

```

I tried newline(posix) and newline(dos\*), and I played around with layout and indent. It’s not vital, but I wonder if this points to a deeper issue.

\*did DOS exist at that time?

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [September 17, 2026, 11:24am UTC](https://swi-prolog.discourse.group/t/xml-write-to-a-zipfile/9819/4 "2026-09-17T11:24:45Z")

</div>

> [@mgondan1](#):
>
> xml\_write does not seem to add “newlines” in the zip file (in normal textfiles, it does).

Nope.

```prolog
106 ?- xml_write(user_output, element(elem, [], []),
                        [ layout(true)
                        ]), write(xxx).
<?xml version="1.0" encoding="UTF-8"?>

<elem/>xxx
true.

```

You surely need `layout(true)` as otherwise it adds no white space at all. There is no final newline. One might argue there should be when layout is used. It is not required by XML though. Simply call `nl(S)` after the xml\_write …

---

<div class="post-metadata">

**Author:** ![mgondan1](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/mgondan1/32/803_2.png) [@mgondan1](https://swi-prolog.discourse.group/u/mgondan1)\
**Post date:** [September 17, 2026, 11:27am UTC](https://swi-prolog.discourse.group/t/xml-write-to-a-zipfile/9819/5 "2026-09-17T11:27:46Z")

</div>

> [@mgondan1](#):
>
> `xml_write(S, [element(elem, [], [element(inside, [], [])])], [layout(true)])`

All right, though I was actually referring to newlines between xml elements (e.g., when nested), not just after the end of the file.

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [September 17, 2026, 11:36am UTC](https://swi-prolog.discourse.group/t/xml-write-to-a-zipfile/9819/6 "2026-09-17T11:36:16Z")

</div>

> [@mgondan1](#):
>
> All right, though I was actually referring to newlines between xml elements (e.g., when nested), not just after the end of the file.

Its too long ago. I think it has rules for that. Check the code (or ask AI to see how to do that 🙂 ) Note that you cannot add white space characters in an XML document except for in the tag, e.g., you can do something like this.

```prolog
<mytag
>Content</mytag
>

```

Inside an element, white space is part of the content. SWI-Prolog’s XML parser has options to avoid that, but these are not XML compliant. This is from my memory; I happily forgot most about XML’s details …

But, it could produce fair layout for RDF/XML, so it can do some of this 🙂 Who wants XML since we have JSON 🙂

---

<div class="post-metadata">

**Author:** ![mgondan1](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/mgondan1/32/803_2.png) [@mgondan1](https://swi-prolog.discourse.group/u/mgondan1)\
**Post date:** [September 17, 2026, 11:42am UTC](https://swi-prolog.discourse.group/t/xml-write-to-a-zipfile/9819/7 "2026-09-17T11:42:14Z")

</div>

> [@jan](#):
>
> Who wants XML since we have JSON 🙂

Response: E-learning-software 🙄
