# ConTeXt: Read an XML file into a buffer for processing

I have a very (very) long XML file, and I am using ConTeXt to turn it into a book.

This works to read the file and output as-is:

\starttext

\startbuffer[test]
\stopbuffer

\getbuffer[test]

\stoptext


If I copy the contents of the XML file between the \startbuffer and \stopbuffer commands, it works fine:

\startbuffer[test]
<EveningPrayer>
<TitleSection>
<PsalterWeek>Psalter, Week I</PsalterWeek>
<Hour>Evening Prayer I</Hour>
</TitleSection>
<Hymn>
...
<CollectLine></CollectLine>
<CollectLine></CollectLine>
</Collect>
</DaytimePrayer>

\stopbuffer

(xmlsetups)

\xmlprocessbuffer{test}{test}{}


However, if I try to read the XML file into the buffer, it does not work (no errors, but no output at all):

\startbuffer[test]
\stopbuffer

(xmlsetups)

\xmlprocessbuffer{test}{test}{}


I don't want to have to copy 10,000+ pages of XML into my .tex file.

• There is \xmlprocessfile. Jan 12, 2019 at 19:27

You don't have to read the file into a buffer first. There is \xmlprocessfile to directly process an XML file.

If you want to read the file into a buffer for some reason, e.g. to preprocess it in Lua or so, you can easily implement such a macro yourself.

\startluacode
function buffers.fromfile(name,file)
if not buffers.exists(name) then
end
end

interfaces.implement {
actions   = buffers.fromfile,
arguments = { "string", "string" }
}
\stopluacode

\unprotect