Page 1 of 1

The most primitive XML writer (just for fun)

Posted: 09 Dec 2019 20:50
by siberia-man
Hello,

I've just written the most primitive XML writer in the world. Please don't take it seriously. I made it just for fun :).

Code: Select all

@echo off

setlocal

call :xml-new-doc
call :xml html
call :xml head ""
call :xml body
call :xml h1 "Greeting..."
call :xml p  "Hello, world!"
call :xml hr /
call :xml p "I am primitive xml writer"
call :xml-end-doc

goto :EOF

:: Open new XML document
:xml-new-doc
set /a "xml_lvl=0"
goto :EOF

:: Finalize the XML document and close all tags
:xml-end-doc
if %xml_lvl% == 0 goto :EOF
set /a "xml_lvl-=1"
setlocal enabledelayedexpansion
echo:^</!xml_tag_%xml_lvl%!^>
endlocal
goto :xml-end-doc

:: Write a XML tag and its content if the last one is specified.
:: Possible options are:
::
:: call :xml tag /
:: Prints the self-closing tag: <tag />
::
:: call :xml tag ""
:: Prints the tag with an empty content: <tag></tag>
::
:: call :xml tag "some content"
:: Prints the tag with its content: <tag>some content</tag>
::
:: call :xml tag
:: Opens the tag container: <tag>
:: Closing tag will be printed at the document end
:xml
if "%~2" == "/" if [%2] == [/] (
	echo:^<%~1/^>
	goto :EOF
)

if "%~2" == "" if [%2] == [""] (
	echo:^<%~1^>^</%~1^>
	goto :EOF
)

if not "%~2" == "" (
	echo:^<%~1^>%~2^</%~1^>
	goto :EOF
)

echo:^<%~1^>
set "xml_tag_%xml_lvl%=%~1"
set /a "xml_lvl+=1"
goto :EOF

Re: The most primitive XML writer (just for fun)

Posted: 10 Dec 2019 14:04
by aGerman
Nice little exercise :)

Could you explain the reason for the double check in lines like that? :

Code: Select all

if "%~2" == "/" if [%2] == [/] (
Steffen

Re: The most primitive XML writer (just for fun)

Posted: 11 Dec 2019 03:49
by siberia-man
aGerman wrote:
10 Dec 2019 14:04
Could you explain the reason for the double check in lines like that? :

Code: Select all

if "%~2" == "/" if [%2] == [/] (
Steffen
This is dirty check for the two cases:

tag as container

Code: Select all

:: this produces <tag>
call :xml tag
empty tag

Code: Select all

:: this produces <tag></tag>
call :xml tag ""
It is dirty, actually. The better way would be to implement separately similar to xml-tag-open, xml-tag-close, xml-tag (these names are just examples). I presume there could be a lot of complexity to be never developed because of batch syntax limitation.

Re: The most primitive XML writer (just for fun)

Posted: 11 Dec 2019 07:45
by aGerman
I think this is what I already understood.
My question is why

Code: Select all

if "%~2" == "/" if [%2] == [/] (
and why not just

Code: Select all

if "%~2" == "/" (
So, what problems do you expect that you need the double check (the two IF statements in a row)?

Steffen

Re: The most primitive XML writer (just for fun)

Posted: 11 Dec 2019 07:52
by siberia-man
Ahh... Sorry for misuderstanding

Code: Select all

xml tag /
for
<tag/>

Code: Select all

xml tag "/"
for
<tag>/</tag>
quite stupid implementation :)

Re: The most primitive XML writer (just for fun)

Posted: 11 Dec 2019 08:03
by aGerman
Oh, now I see. Thanks 👍

Re: The most primitive XML writer (just for fun)

Posted: 12 Dec 2019 11:21
by jfl
Nice script indeed. :D
Actually, the only thing that's really missing is another option to close an open block. (The - sign maybe?)
With that, we'd be able to generate tables, etc.

Code: Select all

call :xml table
call :xml row
call :xml td "Row 1 column 1"
...
call :xml row -
call :xml table -

Re: The most primitive XML writer (just for fun)

Posted: 12 Dec 2019 11:45
by Aacini
siberia-man wrote:
11 Dec 2019 03:49
.....
I presume there could be a lot of complexity to be never developed because of batch syntax limitation.
Perhaps you could be interested in my TextToHtml.bat conversion program with a lot of features...

Antonio