【问题标题】:Need to edit the XML file using Powershell需要使用 Powershell 编辑 XML 文件
【发布时间】:2019-08-31 23:10:09
【问题描述】:

我有 XML 内容,我需要从节点的特定元素中删除整行。

XML 文件:

需要删除整行。

  <Host name="login.net">
    <Path name="Block">
      <Path name="webservices/login" authType="qwerty" requireSession="true" />
      <Path name="login" authType="qwerty" requireSession="true" />
      <Path name="weblogin" requireSession="true" authType="shibboleth" />
    </Path>
    <Path name="retailpdf" applicationId="retailpdf" authType="qwerty" requireSession="true" requireSessionWith="retailpdf" />
    <Path name="managemyaccount" applicationId="managemyaccount" authType="qwerty" requireSession="true" requireSessionWith="managemyaccount" />
    <Path name="approve" applicationId="approve" authType="qwerty" requireSession="true" requireSessionWith="approve" />
    <Path name="latesignin" applicationId="latesignin" authType="qwerty" requireSession="true" requireSessionWith="latesignin" />
    <Path name="fblogin" applicationId="fblogin" authType="qwerty" requireSession="true" requireSessionWith="fblogin" />
  </Host>

</RequestMap>

#需要删除id为retailpdf的完整节点

试图通过powershell脚本删除行

$XMLfilepath = 'C:\Test\MYfile.xml' 
psedit $XMLfilepath
$Pathxml = [xml] (Get-Content $XMLfilepath)

$nodes = $xml.SelectNodes("/RequestMapper/Path");

{Remove-Item "<Path name="fblogin" applicationId="fblogin" authType="shibboleth" requireSession="true" requireSessionWith="fblogin" />"}

$XMLfilepathNew = 'C:\Test\Myfiledone.xml' 
$Pathxml.Save($XMLfilepathNew)
psedit $XMLfilepathNew

需要删除 XML 内容中提到的行和节点,如下所示:

预期输出:

  <Host name="login.net">
    <Path name="Block">
      <Path name="webservices/login" authType="qwerty" requireSession="true" />
      <Path name="login" authType="qwerty" requireSession="true" />
      <Path name="weblogin" requireSession="true" authType="shibboleth" />
    </Path>

    <Path name="managemyaccount" applicationId="managemyaccount" authType="qwerty" requireSession="true" requireSessionWith="managemyaccount" />
    <Path name="approve" applicationId="approve" authType="qwerty" requireSession="true" requireSessionWith="approve" />
    <Path name="latesignin" applicationId="latesignin" authType="qwerty" requireSession="true" requireSessionWith="latesignin" />
    <Path name="fblogin" applicationId="fblogin" authType="qwerty" requireSession="true" requireSessionWith="fblogin" />
  </Host>

</RequestMap>

 <ApplicationOverride id="retailpdf" entityID="https://login.tcxqa.hrblock.net/retailpdf/shibboleth">
  <Sessions lifetime="60" timeout="20" checkAddress="false" consistentAddress="false" handlerURL="/retailpdf/Shibboleth.sso" handlerSSL="true">
    <SessionInitiator type="Chaining" Location="/retailpdf" isDefault="false" id="retailpdf" forceAuthn="true" entityID="https://qaidp.hrblock.net/idp/shibboleth" authnContextClassRef="urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport">
      <SessionInitiator type="SAML2" template="bindingTemplate.html" />
    </SessionInitiator>
  </Sessions>

【问题讨论】:

  • #需要删除 整行。
  • #需要删除id为retailpdf的完整节点

标签: xml powershell


【解决方案1】:

你有没有尝试过这样的事情:

$original = Get-Content 'C:\Test\MYfile.xml'
$new = $original.replace('<Path name="retailpdf" applicationId="retailpdf" authType="qwerty" requireSession="true" requireSessionWith="retailpdf" />', '')
$new | Set-Content 'C:\Test\MYfile.xml'

【讨论】:

  • 太棒了!非常感谢@Panomosh
  • 同时需要删除整个需要,因为属性名称=retailpdf 也与此节点相关。是否可以像您建议的那样删除上面的节点。
  • 只需编辑$original.replace() 方法的第一个参数以匹配您的节点
  • 否 那没有按预期工作。 这是我的节点,我需要删除整个节点。正如你所建议的,我已经更新了 $original.replace() 但输出没有变化..
  • $original = Get-Content 'C:\Test\shibboleth.xml' #$new = $original.replace('', '') $new = $original.replace('', ' ') $new | Set-Content 'C:\Test\shibboleth.xml'
【解决方案2】:

如果您可以创建一个未损坏的 XML 文件,那么您应该能够使用 XmlDocument 类中的方法删除不需要的节点。

$XMLfilepath = 'C:\Test\MYfile.xml' 
$Pathxml = [xml](Get-Content $XMLfilepath)
$nodeToRemove = $Pathxml.SelectSingleNode("//Host/Path[@name = 'retailpdf']")
$nodeToRemove.ParentNode.RemoveChild($nodeToRemove)
$nodeToRemove = $Pathxml.SelectSingleNode("//ApplicationOverride[@id = 'retailpdf']")
$nodeToRemove.ParentNode.RemoveChild($nodeToRemove)
$XMLfilepathNew = 'C:\Test\Myfiledone.xml' 
$Pathxml.Save($XMLfilepathNew)

请注意,您可以在 XPath 中使用通配符。这将有助于更简洁地删除包含特定值的所有属性。但是,您确实需要了解 XML 数据的上下文级别。

【讨论】:

  • 同时使用这个得到一些错误,比如你不能在空值表达式上调用方法。在 line:4 char:1 + $Pathxml.SelectSingleNode('//RequestMapper/RequestMap/Host/Path[@name ... + ~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull ....您对此有任何意见提前谢谢..
  • 这是脚本。 $XMLfilepath = 'C:\Test\shibboleth.xml' $Pathxml = [xml] (Get-Content $XMLfilepath) $Pathxml.SelectSingleNode('//RequestMapper/RequestMap/Host/Path[@name = "retailpdf"]' ).RemoveAll() $Pathxml.SelectSingleNode("//SPConfig/ApplicationOverride[@id = 'retailpdf']").RemoveAll() $XMLfilepathNew = 'C:\Test\shibboleth.xml' $Pathxml.Save($XMLfilepathNew )
  • 缺少的主要部分是 xml 结构。没有准确的 xml 数据很难做 Xpath
  • 谢谢你,我可以知道变化吗,我找不到确切的变化。
  • 我将第二个SelectSingleNode() 中的XPath 从"//RequestMap/ApplicationOverride[@id = 'retailpdf']" 更改为。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-28
  • 2015-01-12
  • 2011-08-28
  • 2014-09-12
  • 2014-01-15
相关资源
最近更新 更多