在特定行插入新的xml文本

时间:2017-04-11 13:54:03

标签: xml powershell insert line

<?xml version="1.0" encoding="UTF-8"?>
<Tc_data_access_config>
<privileges>
<priv_name>READ</priv_name>
<priv_name>WRITE</priv_name>
<priv_name>COPY</priv_name>
<priv_name>CHANGE</priv_name>
<priv_name>DELETE</priv_name>
</privileges>
<named_acls>
<named_acl>
<acl_name>Working</acl_name>
<acl_name language="fr_FR">working_fr</acl_name>
<acl_name language="de_DE">working_de</acl_name>
<acl_name language="jp_JP">working_jp</acl_name>
<ace_entry>
<accessor_type>group</accessor_type>
<accessor>dba</accessor>
<grant>
<p>READ</p>
<p>WRITE</p>
<p>COPY</p>
</grant>
<revoke>
<p>DELETE</p>
<p>CHANGE</p>
</revoke>
</ace_entry>
<ace_entry>
<accessor_type>Owning Group</accessor_type>
<accessor> </accessor>
<grant>
<p>READ</p>
<p>WRITE</p>
<p>COPY</p>
</grant>
<revoke>
<p>DELETE</p>
<p>CHANGE</p>
</revoke>
</ace_entry>
</named_acl>
<named_acl>
<acl_name>In Project ACL</acl_name>
<acl_name language="fr_FR">In Project ACL fr</acl_name>
<acl_name language="de_DE">In Project ACL de</acl_name>
<acl_name language="jp_JP">In Project ACL jp</acl_name>
<ace_entry>
<accessor_type>group</accessor_type>
<accessor>dba</accessor>
<grant>
<p>READ</p>
<p>WRITE</p>
<p>COPY</p>
</grant>
<revoke>
<p>DELETE</p>
<p>CHANGE</p>
</revoke>
</ace_entry>
<ace_entry>
<accessor_type>Owning Group</accessor_type>
<accessor> </accessor>
<grant>
<p>READ</p>
<p>WRITE</p>
<p>COPY</p>
</grant>
<revoke>
<p>DELETE</p>
<p>CHANGE</p>
</revoke>
</ace_entry>
</named_acl>
</named_acls>
<rule_tree>
<tree_node>
<rule_name>Has Class</rule_name>
<rule_argument>POM_object</rule_argument>
<acl_name></acl_name>
<tree_node>
<rule_name>Has Bypass</rule_name>
<rule_argument>true</rule_argument>
<acl_name>Bypass</acl_name>
</tree_node>
<tree_node>
<rule_name>Has Status</rule_name>
<rule_argument></rule_argument>
<acl_name>Vault</acl_name>
</tree_node>
<tree_node>
<rule_name>Has Class</rule_name>
<rule_argument>POM_application_object</rule_argument>
<acl_name>Working</acl_name>
<tree_node>
<rule_name>Has Class</rule_name>
<rule_argument>Dataset</rule_argument>
<acl_name>Dataset ACL</acl_name>
</tree_node>
</tree_node>
</tree_node>
</rule_tree>
</Tc_data_access_config>

您好, 我上面有一个xml文件,我想在其中插入一些新数据 - 自动化 - 这个想法是在一个特定的行号(首选)之后或者在一组特定行之后(之后查找并插入一些多行标记文本)。

让我们说它应该找到这样的文字:

    <tree_node>
<rule_name>Has Class</rule_name>
<rule_argument>POM_object</rule_argument>
<acl_name></acl_name>
<tree_node>
<rule_name>Has Bypass</rule_name>
<rule_argument>true</rule_argument>
<acl_name>Bypass</acl_name>
</tree_node>
<tree_node>
<rule_name>Has Status</rule_name>
<rule_argument></rule_argument>
<acl_name>Vault</acl_name>
</tree_node>
<tree_node>
<rule_name>Has Class</rule_name>
<rule_argument>POM_application_object</rule_argument>
<acl_name>Working</acl_name>
<tree_node>
<rule_name>Has Class</rule_name>
<rule_argument>Dataset</rule_argument>
<acl_name>Dataset ACL</acl_name>
</tree_node>
</tree_node>
</tree_node>

然后添加:

    <tree_node>
<rule_name>Has BLA</rule_name>
<rule_argument>324234</rule_argument>
<acl_name></acl_name>
<tree_node>
<rule_name>Has TEST</rule_name>
<rule_argument>true</rule_argument>
<acl_name>NO</acl_name>
</tree_node>
<tree_node>
<rule_name>Has BLABLA</rule_name>
<rule_argument></rule_argument>
<acl_name>Vault</acl_name>
</tree_node>
<tree_node>
<rule_name>Has Class</rule_name>
<rule_argument>test</rule_argument>
<acl_name>Working</acl_name>
<tree_node>
<rule_name>Has test</rule_name>
<rule_argument>Dataset</rule_argument>
<acl_name>Dataset ACL</acl_name>
</tree_node>
</tree_node>
</tree_node>

可以用Powershell或其他一些脚本语言来完成任何事情吗? 谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

如果您正确使用对象,您应该能够“点”走到您需要的值。就像是     $ c.tree_node.rule_name

这是我使用的wifi脚本的一部分。您可以看到文件如何作为对象加载,然后您可以点到您要使用的XML中的属性。然后你可以做你的PSH foo来改变和保存新的XML。

Get-ChildItem -Path $path -Recurse |
Foreach-Object {
  [xml]$c = Get-Content -path $_.fullname

$connections += New-Object pscustomobject -Property @{
  ‘name’ = $c.WLANProfile.name;
  ‘mode’ = $c.WLANProfile.connectionMode;
  ‘ssid’ = $c.WLANProfile.SSIDConfig.SSID.hex

  }
}

$connections | Export-CliXml 
"$($PSScriptRoot)\$($env:COMPUTERNAME)_Backup.xml"