2016-11-11 67 views
-1

衍生我有一个这样的XML文件:元素插入XML文档从现有的内容

<game id="748" source="ScreenScraper"> 
    <path>./Tecmo Super Bowl (U) (Sep 1993) [!].zip</path> 
    <name>Tecmo Super Bowl</name> 
    <desc>Tecmo returns to the gridiron with this new version of Tecmo Super Bowl for 16-bit console systems. Play with real National Football League players and teams in this 2D, side-scrolling arcade game.</desc> 
    <image>~/.emulationstation/downloaded_images/megadrive/Tecmo Super Bowl (U) (Sep 1993) [!]-image.png</image> 
    <rating>0.85</rating> 
    <releasedate>19930000T000000</releasedate> 
    <developer>Tecmo</developer> 
    <publisher>Tecmo</publisher> 
    <genre>Sports</genre> 
    <players>1-2</players> 
    <crc32>0F02FBFC</crc32> 
    <md5>FEC698E5387617B11C81431894B12EDC</md5> 
    <sha1>77AF672321947C3F8F80F7AE7ADFF8CDE0E2986A</sha1> 
</game> 
<game id="1014" source="ScreenScraper"> 
    <path>./Action 52 (Unl) [!].zip</path> 
    <name>Action 52</name> 
    <desc>Action 52 features 52 different games in a single cartridge! The games are selected from one of three menus and are mostly side scrolling, platform or shooter action games.</desc> 
    <image>~/.emulationstation/downloaded_images/megadrive/Action 52 (Unl) [!]-image.png</image> 
    <rating>0.1</rating> 
    <releasedate>19930000T000000</releasedate> 
    <developer>Active Enterprises Ltd.</developer> 
    <publisher>Active Enterprises</publisher> 
    <genre>Compilation</genre> 
    <players>1-2</players> 
    <crc32>7B544625</crc32> 
    <md5>D32F3F2825DB4AE40D51317BBFF7330E</md5> 
    <sha1>3D9C1984B5F1B385EBDFF34218FEA6BCE83C43B6</sha1> 
</game> 
<game id="103612" source="ScreenScraper"> 
    <path>./Bug's Life, A (Unl) [!].zip</path> 
    <name>A Bug's Life</name> 
    <developer>X BOY</developer> 
    <publisher>X BOY</publisher> 
    <genre>Action</genre> 
    <crc32>6EBECD9A</crc32> 
    <md5>27122254E417C44A35310E6D39B61D4A</md5> 
    <sha1>441A0E019EB32106D1AFD2ED97F117B007299F5B</sha1> 
</game> 

我需要一个批处理文件来为每个游戏读取名字的值,并添加两个新线这样的每场比赛:

<video>~/.emulationstation/roms/megadrive/Videos/%gamename%.mp4</video> 
<marquee>~/.emulationstation/roms/megadrive/Marquees/%gamename%.png</marquee> 

%gamename%必须<name></name>之间的游戏的名称。

我有此批处理脚本,但它不工作:

setlocal enableextensions disabledelayedexpansion 
set "gamename=" 
for /f "tokens=3 delims=<>" %%a in ('find /i "<name>" ^< "gamelist.xml"') do set "gamename=%%a" 
(for /F "delims=" %%a in (gamelist.xml) do (
    set "newLine=!line:"~/.emulationstation/roms/megadrive/Videos/%gamename%.mp4" 
    echo !newLine! 
)) > newFile.xml 

回答

2

最可靠的解决方案是使用XML解析器,并PowerShell的允许一个轻松访问:

# Read the XML file into an XML DOM ([System.Xml.XmlDocument] or [xml], for short). 
# Note: If your file truly has no single root XML element, 
#  use ('<xml>' + (Get-Content ...) + '</xml>') 
$doc = [xml] (Get-Content -raw gamelist.xml) 

# Loop over all <game> elements. 
foreach ($gameEl in $doc.DocumentElement.game) { 
    $gameName = $gameEl.name 
    # Append the new elements, using string interpolation to set the inner text 
    # (note the references to variable ${gameName} inside the double-quoted string). 
    $gameEl.AppendChild($doc.CreateElement('video')).InnerText = "~/.emulationstation/roms/megadrive/Videos/${gameName}.mp4" 
    $gameEl.AppendChild($doc.CreateElement('marquee')).InnerText = "~/.emulationstation/roms/megadrive/Marquees/${gameName}.png" 
} 

# Save the modified XML to a new file, using the .NET framework's default 
# UTF-8 encoding without a BOM. 
$writer = [System.IO.StreamWriter] "$PWD/newFile.xml" 
$doc.Save($writer) 
$writer.Close() 

由OP要求的变体,它使用包含在所述路径的扩展名的文件名在<path>元素,而不是<name>元素的值,只有增加要素videomarquee如果他们不已经存在:

$doc = [xml] (Get-Content -raw gamelist.xml) 

foreach ($gameEl in $doc.DocumentElement.game) { 
    # Use -replace to extract the filename without extension from the 
    # path contained in the <path> element. 
    $gameName = $gameEl.path -replace '^.*/(.*)\..*$', '$1' 
    # Append elements 'video' and 'marquee', but only if they don't already 
    # exist. 
    if ($null -eq $gameEl.video) { 
    $gameEl.AppendChild($doc.CreateElement('video')).InnerText = "~/.emulationstation/roms/megadrive/Videos/${gameName}.mp4" 
    } 
    if ($null -eq $gameEl.marquee) { 
    $gameEl.AppendChild($doc.CreateElement('marquee')).InnerText = "~/.emulationstation/roms/megadrive/Marquees/${gameName}.png" 
    } 
} 

$writer = [System.IO.StreamWriter] "$PWD/newFile.xml" 
$doc.Save($writer) 
$writer.Close()