简体   繁体   English

Windows批处理将一种XML复制到另一种XML

[英]Copy from one XML to another by Windows batch

I am trying to write a simple Windows batch ( .cmd ) script that copies some XML element from one XML file and insert it in the right place in another file. 我正在尝试编写一个简单的Windows批处理( .cmd )脚本,该脚本从一个XML文件复制一些XML元素,然后将其插入另一个文件的正确位置。

The first XML file has a structure like this: 第一个XML文件的结构如下:

<config>
 <jx></jx>
     <roul>
         <rect>
         </rect>
         <background>
         </background>
         <text>R</text>
         <color>123</color>
         <vcenter>true</vcenter>
         <textformat>center</textformat>
         <rl>
           <r1>
             <egmid>G</egmid>
             <number>1</number>
           </r1>
           <r2>
             <egmid>C</egmid>
             <number>2</number>
           </r2>
         </rl>
 </roul>
 <a></a>
 <b><b>
</config>

the second (destination) xml file before the merge: 合并之前的第二个(目标)xml文件:

<config>
 <jx></jx>
     <roul>
         <rect>
         </rect>
         <background>
         </background>
         <text>R</text>
         <color>123</color>
         <vcenter>true</vcenter>
         <textformat>center</textformat>
         <rl> </rl>
 </roul>
 <a></a>
 <b><b>
</config>

An after the merge it should look similar to the source xml file: 合并后的外观应类似于源xml文件:

<config>
 <jx></jx>
     <roul>
         <rect>
         </rect>
         <background>
         </background>
         <text>R</text>
         <color>123</color>
         <vcenter>true</vcenter>
         <textformat>center</textformat>
         <rl>
           <r1>
             <egmid>G</egmid>
             <number>1</number>
           </r1>
           <r2>
             <egmid>C</egmid>
             <number>2</number>
           </r2>
         </rl>
 </roul>
 <a></a>
 <b><b>
</config>

I need to copy all the tags that are between the <rl> / </rl> tags and copy them in the second XML file that looks almost the same but has no children elements between <rl> / <rl> . 我需要复制<rl> / </rl>标记之间的所有标记,并将它们复制到看起来几乎相同但在<rl> / <rl>之间没有子元素的第二个XML文件中。

The two files are in separate locations like: 这两个文件位于不同的位置,例如:

  • c:/andrej/tmp1/file1.xml
  • c:/andrej/tmp2/file2.xml

What I learned from other posts is how to loop trough XML lines, but not how to copy and then somehow echo it or paste it to another XML file... 我从其他文章中学到的是如何循环遍历XML行,而不是如何复制然后以某种方式将其回显或粘贴到另一个XML文件中。

The code I have for now is: 我现在的代码是:

(for /F "delims=" %%a in ("c:\andrej\tmp1\file1.xml") do (
    set "line=%%a"
    set "newLine=!line:roulettes>=!"
    if "!newLine!" eq "!line!" (
        rem ...
    )
))

Can anyone help me? 谁能帮我?

note: Your XML snippets aren't fully valid, by the way. 注意: 顺便说一下,您的XML代码段并不完全有效。 The closer of your <b> tag at the bottom of all your XML examples is missing a slash. 在所有XML示例底部的<b>标记结尾处缺少斜杠。 This solution assumes you fixed that. 该解决方案假定您已解决该问题。

As others have pointed out, it's better to parse and objectify XML and other such structured markup than to hack and scrape it as flat text. 正如其他人指出的那样,解析和对象化XML和其他此类结构化标记比将其砍成纯文本更好。 The batch language unfortunately doesn't offer much for parsing XML, but it's easy enough to borrow from other languages. 不幸的是,批处理语言不能为XML解析提供太多功能,但从其他语言中借用它很容易。

PowerShell is particularly well-suited to handle XML. PowerShell特别适合处理XML。 You can read an XML file as text, then cast that data as an XML object simply by prefacing it with [XML] . 您可以将XML文件读取为文本,然后只需以[XML]开头将其转换为XML对象即可。 Handy, right? 方便吧? From there, select the source node from XML1 and the destination node from XML2 using XPath expressions, then import from 1 into 2. 在此处,使用XPath表达式从XML1选择源节点,从XML2选择目标节点,然后从1导入到2。

Here's a hybrid Batch + PowerShell script demonstrating this. 这是一个演示批处理+ PowerShell的混合脚本。 Save this with a .bat extension and salt to taste. 用.bat扩展名保存并加盐调味。

<# : batch portion (begin multiline PowerShell comment)
@echo off & setlocal

set "xml1=c:\andrej\tmp1\file1.xml"
set "xml2=c:\andrej\tmp2\file2.xml"

if not exist "%xml1%" (
    echo XML1: %xml1% not found.
    exit /b 1
)

if not exist "%xml2%" (
    echo XML2: %xml2% not found.
    exit /b 1
)

rem // relaunch self with PowerShell
powershell -noprofile "iex (${%~f0} | out-string)"

rem // end of script
goto :EOF

: end batch / begin PowerShell hybrid code #>

$src = [xml](gc $env:xml1)
$dest = [xml](gc $env:xml2)

$src.SelectNodes("//roul/rl/*") | %{
    "copying {0} and its descendents" -f $_.Name
    [void]$dest.SelectSingleNode("//roul/rl").AppendChild($dest.ImportNode($_, $true))
}

$dest.Save($env:xml2)
write-host "Saved changes to ${env:xml2}" -f green
# // return execution back to Batch

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM