Xml_write to a zipfile

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

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:

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

Can I specify this somewhere?

xml_write/3 wants a text stream. This works fine.

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).

Thank you! The problem has been solved.

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

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?

Nope.

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 …

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 :slight_smile: ) 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.

<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 :slight_smile: Who wants XML since we have JSON :slight_smile:

Response: E-learning-software :roll_eyes: