# How to modify the environment and command possible write the content in a file through the \write command?

I want to "generate/convert" my .tex files into xml format while compiling LaTeX. I don't want to modify the tex files, I want to generate the xml file trough the \write command based on the DTD while compiling in LaTeX I can only build a document class through commands and environment. Is possible to generate the xml file through the \write command? So far, I tried the following code:

\documentclass{book}
\usepackage{amsmath}

\newwrite\xmlwrite
\immediate\openout\xmlwrite \jobname.xml

\renewenvironment{quote}{\immediate\write\xmlwrite{<bq id="">}%
\leftskip24pt\rightskip24pt\noindent
}
{\immediate\write\xmlwrite{</sample>}%
\everypar{\immediate\write\xmlwrite{<p>}}

\begin{document}

We note that we cannot fully eliminate a possibility, however small, that
the results of our alternative tests are falsely rejecting the true hypothesis that
splitting activity responds to investor preferences.

\begin{quote}
Given that the spurious regression bias may result in finding empirical
support for the catering hypothesis even if firms do not actually respond to
catering incentives, we revisit the results using five alternative techniques that
reduce the bias concerns.
\end{quote}
We note that we cannot fully eliminate a possibility, however small, that
the results of our alternative tests are falsely rejecting the true hypothesis that
splitting activity responds to investor preferences.

\immediate\closeout\xmlwrite
\end{document}

• I think this will prove to be far harder than you might think. If the need to also produce XML is of paramount importance, then you might find that ConTeXt is the better option; see, e.g., its page about XML output. – jon Dec 19 '16 at 6:58
• there is a lot of existing solutions for LaTeX to XML conversion, for example tex4ht – michal.h21 Dec 19 '16 at 7:32