将元素插入从现有内容派生的 XML 文档中
Insert elements into an XML document derived from existing content
我有一个这样的 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
最稳健的解决方案是使用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>
元素的值,并且只添加元素 video
和 marquee
如果它们尚不存在:
$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 '^.*/(.*)\..*$', ''
# 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()
我有一个这样的 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
最稳健的解决方案是使用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>
元素的值,并且只添加元素 video
和 marquee
如果它们尚不存在:
$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 '^.*/(.*)\..*$', ''
# 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()