如何接受来自用户的值,更改XML文件中的特定值并启动浏览器? @echo关闭 SETLOCAL ENABLEDELAYEDEXPANSION 设置自由港= 设置新港= REM检查位于路径的XML文件中的默认值 对于/F令牌^=4^ delims^=^“%%A( 'type%~dp0\wlp\usr\servers\defaultServer\server.xml^查找“httpEndpoint host=”' )做( echo:XML中的默认端口=%%A 设置/a开始端口=%%a ) 回显我的当前端口%startPort% :搜索端口 netstat-o-n-a |查找“侦听”|查找”:%startPort%>NUL 如果“%ERRORLEVEL%”等于“0”( 回显“端口不可用%startPort%” REM这里我想要求用户输入端口号而不是硬编码值??? set/a startPort=9080 回显我的新端口%startPort% 转到:搜索端口 )否则( 回显“端口可用%startPort%” 设置自由端口=%startPort% 转到:FOUNDPORT ) :FOUNDPORT 无回声%freePort% REM此处我想更改httpPort的XML值并保存XML文件,然后使用https:\\localhost:MyApp???启动默认浏览器??? ) @停顿

如何接受来自用户的值,更改XML文件中的特定值并启动浏览器? @echo关闭 SETLOCAL ENABLEDELAYEDEXPANSION 设置自由港= 设置新港= REM检查位于路径的XML文件中的默认值 对于/F令牌^=4^ delims^=^“%%A( 'type%~dp0\wlp\usr\servers\defaultServer\server.xml^查找“httpEndpoint host=”' )做( echo:XML中的默认端口=%%A 设置/a开始端口=%%a ) 回显我的当前端口%startPort% :搜索端口 netstat-o-n-a |查找“侦听”|查找”:%startPort%>NUL 如果“%ERRORLEVEL%”等于“0”( 回显“端口不可用%startPort%” REM这里我想要求用户输入端口号而不是硬编码值??? set/a startPort=9080 回显我的新端口%startPort% 转到:搜索端口 )否则( 回显“端口可用%startPort%” 设置自由端口=%startPort% 转到:FOUNDPORT ) :FOUNDPORT 无回声%freePort% REM此处我想更改httpPort的XML值并保存XML文件,然后使用https:\\localhost:MyApp???启动默认浏览器??? ) @停顿,xml,batch-file,xml-parsing,Xml,Batch File,Xml Parsing,server.xml内容是: @echo off SETLOCAL ENABLEDELAYEDEXPANSION set freePort= set newPort= REM Checking the default value in XML file located at the path FOR /F tokens^=4^ delims^=^" %%A in ( 'type %~dp0\wlp\usr\servers\defaultServer\server.xml ^| find "ht

server.xml内容是:

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set freePort=
set newPort=

REM Checking the default value in XML file located at the path
FOR /F tokens^=4^ delims^=^" %%A in (
'type %~dp0\wlp\usr\servers\defaultServer\server.xml ^| find "httpEndpoint host="'
) do (
     echo: Default Port in XML = %%A
     set /a startPort=%%A

)
echo myCurrent port %startPort%

:SEARCHPORT
netstat -o -n -a | find "LISTENING" | find ":%startPort% " > NUL
if "%ERRORLEVEL%" equ "0" (
  echo "port unavailable %startPort%"

 REM Here I want to ask user to enter a port number rather than hard-coded value ???
  set /a startPort=9080
  echo myNew port %startPort%
  GOTO :SEARCHPORT

) ELSE (
  echo "port available %startPort%"
  set freePort=%startPort%
  GOTO :FOUNDPORT
)

:FOUNDPORT
echo free %freePort%

REM here I want to change the value of the httpPort in XML and save the xml file, and then launch the default browser with https:\\localhost:<freePort>MyApp ???


)
@pause

webProfile-7.0

< /P> < P>用Windows命令解释器处理XML文件是可怕的。<代码> CMD.exe < /C>是为执行命令和应用而设计的,而不是解析XML文件和修改它们。对于这个任务,用C、C++或C语言编写一个可执行文件肯定要好得多,而不是用批处理文件来写。 但是,这里有一个带注释的批处理文件,用于将属性

httpPort
的当前端口号替换为XML文件
server.XML
中分配给环境变量
NewPort
的新端口号。该文件必须存储在与批处理文件相同的目录中。请阅读注释,因为此代码仍有一些限制属性
httpsPort
的like编号必须与
httpPort
的编号不同,否则两个编号将同时替换

<server description="new server">

<!-- Enable features -->
<featureManager>
    <feature>webProfile-7.0</feature>
    <!-- <feature>localConnector-1.0</feature> -->
</featureManager>

<!-- To access this server from a remote client add a host attribute to the following element, e.g. host="*" -->
<httpEndpoint host="27" httpPort="5357" httpsPort="9443" id="defaultHttpEndpoint"/>

<!-- Automatically expand WAR files and EAR files -->
<applicationManager autoExpand="true"/>


<applicationMonitor updateTrigger="mbean"/>
更好的方法是使用Dave Benham编写的示例,它是批处理文件/JScript的混合体,用于使用正则表达式替换XML文件中的值

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem First check if the file to modify exists in directory of batch file.
set "XmlFile=%~dp0server.xml"
if not exist "%XmlFile%" goto EndBatch

rem Define some environment variables which are needed later.
set "NewPort=47680"
set "LineNumber="
set "LineCount=0"
set "TmpFile=%TEMP%\%~n0.tmp"

rem Search for the line containing attribute httpPort and get its
rem line number and the line itself loaded into environment variables.

for /F "tokens=1* delims=:" %%I in ('%SystemRoot%\System32\findstr.exe /L /N /C:httpPort= "%XmlFile%" 2^>nul') do (
    set "LineNumber=%%I"
    set "PortLine=%%J"
)

rem If no line with attribute httpPort found, exit this batch file.
if not defined LineNumber goto EndBatch

rem Determine current number of attribute httpPort independent on where
rem this attribute is specified in the XML line and replace this number
rem in the line with the new port number as defined before.

rem It is required for this type of number replace that the other port
rem number for httpsPort is not the same number as current number for
rem httpPort as in this case both numbers would be replaced by the new
rem number. The attribute name and the equal sign cannot be included in
rem the string substitution as used here.

setlocal EnableDelayedExpansion
set "PortNumber=!PortLine:*httpPort=!"
for /F %%I in ("!PortNumber:~1!") do set "PortNumber=%%~I"
set "PortLine=!PortLine:"%PortNumber%"="%NewPort%"!"
endlocal & set "PortLine=%PortLine%"

rem Make sure the temporary file used next does not already exist.
del "%TmpFile%" 2>nul

rem Copy all lines from XML file to a temporary file including empty
rem lines with the exception of the line containing attribute httpPort
rem which is copied to temporary file with the modified port number.
for /F "tokens=1* delims=:" %%I in ('%SystemRoot%\System32\findstr.exe /R /N "^" "%XmlFile%" 2^>nul') do (
    set "XmlLine=%%J"
    set /A LineCount+=1
    setlocal EnableDelayedExpansion
    if not !LineCount! == %LineNumber% (
        echo/!XmlLine!>>"%TmpFile%"
    ) else (
        echo/!PortLine!>>"%TmpFile%"
    )
    endlocal
)

rem Overwrite original file with temporary file automatically deleted on success.
move /Y "%TmpFile%" "%XmlFile%" >nul

:EndBatch
endlocal
通过使用
jrepl.bat
属性
httpPort
的当前值是什么并不重要,它甚至可以是一个空字符串。这里使用的不区分大小写的正则表达式replace甚至可以处理属性
httpPort
,其值用单引号括起来,而不是用双引号括起来,或者根本不用q括起来uotes,但这对XML文件无效


\x22
是这里使用的
的十六进制表示法,用于避免Windows命令解释器出现问题,因为搜索参数字符串本身包含在双引号中。

我强烈建议您使用能够本机读写XML文件的脚本语言。Powershell、Vbscript或Jscript是一些选项。为了让批处理文件完成这项工作,它必须使用蛮力来完成。几乎完成了。我只想在批处理脚本的xml文件中为httpPort=“<>”添加一个新值。任何人都可以给出简单的几行代码来尝试使用Xidel,cmdline工具来解析XML文档。
@echo off
if not exist "%~dp0server.xml" goto :EOF
set "NewPort=47681"

call "%~dp0jrepl.bat" "(httpPort=[\x22']?)\d*([\x22']?)" "$1%NewPort%$2" /I /F "%~dp0server.xml" /O -