使用PowerShell,我想在XML树中添加几个子元素.
我知道添加一个元素,我知道添加一个或几个属性,但我不明白如何添加几个元素.
一种方法是对子级来写一个子XML树作为文本
但是因为元素没有立即加入,我不能用这种方法.
要添加一个元素,我这样做:
[xml]$xml = get-content $nomfichier
$newEl = $xml.CreateElement('my_element')
[void]$xml.root.AppendChild($newEl)
Run Code Online (Sandbox Code Playgroud)
工作良好.这给了我这个XML树:
$xml | fc
class XmlDocument
{
root =
class XmlElement
{
datas =
class XmlElement
{
array1 =
[
value1
value2
value3
]
}
my_element = <-- the element I just added
}
}
Run Code Online (Sandbox Code Playgroud)
现在我想在'my_element'中添加一个子元素.我使用类似的方法:
$anotherEl = $xml.CreateElement('my_sub_element')
[void]$xml.root.my_element.AppendChild($anotherEl) <-- error because $xml.root.my_element is a string
[void]$newEl.AppendChild($anotherEl) <-- ok
$again = $xml.CreateElement('another_one')
[void]$newEl.AppendChild($again)
Run Code Online (Sandbox Code Playgroud)
这给了这个XML树(部分显示):
my_element =
class XmlElement
{
my_sub_element =
another_one =
}
Run Code Online (Sandbox Code Playgroud)
这些是属性,而不是子元素.
子元素将显示为:
my_element =
[
my_sub_element
another_one
]
Run Code Online (Sandbox Code Playgroud)
问题:如何一次添加多个子元素?
JPB*_*anc 41
看看下面的例子:
# Document creation
[xml]$xmlDoc = New-Object system.Xml.XmlDocument
$xmlDoc.LoadXml("<?xml version=`"1.0`" encoding=`"utf-8`"?><Racine></Racine>")
# Creation of a node and its text
$xmlElt = $xmlDoc.CreateElement("Machine")
$xmlText = $xmlDoc.CreateTextNode("Mach1")
$xmlElt.AppendChild($xmlText)
# Creation of a sub node
$xmlSubElt = $xmlDoc.CreateElement("Adapters")
$xmlSubText = $xmlDoc.CreateTextNode("Network")
$xmlSubElt.AppendChild($xmlSubText)
$xmlElt.AppendChild($xmlSubElt)
# Creation of an attribute in the principal node
$xmlAtt = $xmlDoc.CreateAttribute("IP")
$xmlAtt.Value = "128.200.1.1"
$xmlElt.Attributes.Append($xmlAtt)
# Add the node to the document
$xmlDoc.LastChild.AppendChild($xmlElt);
# Store to a file
$xmlDoc.Save("c:\Temp\Temp\Fic.xml")
Run Code Online (Sandbox Code Playgroud)
编辑
Ert*_*maa 32
我更喜欢手工创建xml,而不是使用API逐节点地构建它,因为imho手工将更易读,更易于维护.
这是一个例子:
$pathToConfig = $env:windir + "\Microsoft.NET\Framework64\v4.0.30319\Config\web.config"
$xml = [xml] (type $pathToConfig)
[xml]$appSettingsXml = @"
<appSettings>
<add key="WebMachineIdentifier" value="$webIdentifier" />
</appSettings>
"@
$xml.configuration.AppendChild($xml.ImportNode($appSettingsXml.appSettings, $true))
$xml.Save($pathToConfig)
Run Code Online (Sandbox Code Playgroud)
检查此代码示例。它拥有从头开始创建 XML 所需的一切:
function addElement($e1, $name2, $value2, $attr2)
{
if ($e1.gettype().name -eq "XmlDocument") {$e2 = $e1.CreateElement($name2)}
else {$e2 = $e1.ownerDocument.CreateElement($name2)}
if ($attr2) {$e2.setAttribute($value2,$attr2)}
elseif ($value2) {$e2.InnerText = "$value2"}
return $e1.AppendChild($e2)
}
function formatXML([xml]$xml)
{
$sb = New-Object System.Text.StringBuilder
$sw = New-Object System.IO.StringWriter($sb)
$wr = New-Object System.Xml.XmlTextWriter($sw)
$wr.Formatting = [System.Xml.Formatting]::Indented
$xml.Save($wr)
return $sb.ToString()
}
Run Code Online (Sandbox Code Playgroud)
...现在让我们使用这两个函数来创建和显示一个新的 XML 对象:
$xml = New-Object system.Xml.XmlDocument
$xml1 = addElement $xml "a"
$xml2 = addElement $xml1 "b"
$xml3 = addElement $xml2 "c" "value"
$xml3 = addElement $xml2 "d" "attrib" "attrib_value"
write-host `nFormatted XML:`r`n`n(formatXML $xml.OuterXml)
Run Code Online (Sandbox Code Playgroud)
结果如下所示:
Formatted XML:
<?xml version="1.0" encoding="utf-16"?>
<a>
<b>
<c>value</c>
<d attrib="attrib_value" />
</b>
</a>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
64449 次 |
| 最近记录: |