Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
siberia-man
- Posts: 208
- Joined: 26 Dec 2013 09:28
-
Contact:
#1
Post
by siberia-man » 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
.
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
-
aGerman
- Expert
- Posts: 4654
- Joined: 22 Jan 2010 18:01
- Location: Germany
#2
Post
by aGerman » 10 Dec 2019 14:04
Nice little exercise
Could you explain the reason for the double check in lines like that? :
Steffen
-
siberia-man
- Posts: 208
- Joined: 26 Dec 2013 09:28
-
Contact:
#3
Post
by siberia-man » 11 Dec 2019 03:49
aGerman wrote: ↑10 Dec 2019 14:04
Could you explain the reason for the double check in lines like that? :
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.
-
aGerman
- Expert
- Posts: 4654
- Joined: 22 Jan 2010 18:01
- Location: Germany
#4
Post
by aGerman » 11 Dec 2019 07:45
I think this is what I already understood.
My question is why
and why not just
So, what problems do you expect that you need the double check (the two IF statements in a row)?
Steffen
-
siberia-man
- Posts: 208
- Joined: 26 Dec 2013 09:28
-
Contact:
#5
Post
by siberia-man » 11 Dec 2019 07:52
Ahh... Sorry for misuderstanding
quite stupid implementation
-
aGerman
- Expert
- Posts: 4654
- Joined: 22 Jan 2010 18:01
- Location: Germany
#6
Post
by aGerman » 11 Dec 2019 08:03
Oh, now I see. Thanks
-
jfl
- Posts: 226
- Joined: 26 Oct 2012 06:40
- Location: Saint Hilaire du Touvet, France
-
Contact:
#7
Post
by jfl » 12 Dec 2019 11:21
Nice script indeed.
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 -
-
Aacini
- Expert
- Posts: 1885
- Joined: 06 Dec 2011 22:15
- Location: México City, México
-
Contact:
#8
Post
by Aacini » 12 Dec 2019 11:45
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