The most primitive XML writer (just for fun)
Posted: 09 Dec 2019 20:50
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 .
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