Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用powershell仅导入父节点,而保留xml文件中的所有子节点?_Xml_Powershell_Xml Parsing - Fatal编程技术网

如何使用powershell仅导入父节点,而保留xml文件中的所有子节点?

如何使用powershell仅导入父节点,而保留xml文件中的所有子节点?,xml,powershell,xml-parsing,Xml,Powershell,Xml Parsing,我有一个类似这样的xml文件 <parent> <child1> <subchild11/> <subchild12/> </child1> <child2> <subchild21/> <subchild22/> </child2> </parent> 我只想导入父节点(如果有参数),而不导入子节点。然后我想

我有一个类似这样的xml文件

  <parent>
  <child1>
    <subchild11/>
    <subchild12/>
   </child1>
   <child2>
    <subchild21/>
    <subchild22/>
   </child2>
  </parent>

我只想导入父节点(如果有参数),而不导入子节点。然后我想用这个父节点创建一个新的xml,并将子节点添加到我的愿望中。 我不想使用CreateNode()创建父节点


可能吗?我在任何地方都找不到它。

您可以导入XML,然后从父节点删除所有子节点

$xmlDocument=[Xml](获取内容'file.Xml')
$parent=$xmlDocument.ChildNodes[0]
#删除所有子项
而($parent.FirstChild-ne$null){
$parent.RemoveChild($parent.FirstChild)
}
然后,您可以只向其中添加子项:

#添加新的子节点
$parent.AppendChild(…)
$parent.AppendChild(…)

编辑
$parent.RemoveAll()
也会删除父对象的属性(我必须承认,这非常混乱),因此我用一个循环替换它以删除所有子对象。

另一种方法使用ImportNode的第三个参数,称为“deep”。设置为$true将复制所有子代节点,但设置为$false将仅复制属性。请参阅Microsoft开发人员网络页面

当输入为DocumentElement且“deep”为$true时

源元素及其指定属性的后代 递归导入节点,并将生成的节点重新组装到 形成相应的子树

注意:不会复制默认属性。如果该文档 导入到定义此元素名称的默认属性 被分配

当“deep”为$false时

将导入源元素的指定属性节点,并且 生成的XmlAttribute节点附加到生成的XmlElement

注意:不会复制默认属性。如果该文档 导入到定义此元素名称的默认属性 被分配

下面的代码显示了此方法的作用:

$inxml = [xml] @"
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="myfile.xsl" ?>
<bookstore specialty="novel">
  <book style="autobiography">
    <author>
      <first-name>Joe</first-name>
      <last-name>Bob</last-name>
      <award>Trenton Literary Review Honorable Mention</award>
    </author>
    <price>12</price>
  </book>
  <book style="textbook">
    <author>
      <first-name>Mary</first-name>
      <last-name>Bob</last-name>
      <publication>Selected Short Stories of
        <first-name>Mary</first-name>
        <last-name>Bob</last-name>
      </publication>
    </author>
    <editor>
      <first-name>Britney</first-name>
      <last-name>Bob</last-name>
    </editor>
    <price>55</price>
  </book>
</bookstore>
"@

$outxml1 = New-Object -TypeName System.Xml.XmlDocument
$outxml2 = New-Object -TypeName System.Xml.XmlDocument

$inxml.bookstore

# third parameter of ImportNode is $true for 'deep copy'
# where descendants are copied fully
# see https://msdn.microsoft.com/en-us/library/system.xml.xmldocument.importnode(v=vs.110).aspx
# The descendants of the source element and its specified
# attribute nodes are recursively imported and the resulting
# nodes reassembled to form the corresponding subtree.
$outnode1 = $outxml1.ImportNode($inxml.bookstore, $true)
$outxml1.AppendChild($outnode1)
$outxml1
$outxml1.Save("N:\temp\xml_desc.xml")

# third parameter of ImportNode is $false for "not a 'deep copy'"
# from the above reference:
# Specified attribute nodes of the source element are imported, 
# and the generated XmlAttribute nodes are attached to the 
# generated XmlElement.
$outnode2 = $outxml2.ImportNode($inxml.bookstore, $false)
$outxml2.AppendChild($outnode2)
$outxml2
$outxml2.Save("N:\temp\xml_nodesc.xml")

Write-Host "Ends"
$inxml=[xml]@”
乔
上下快速移动
特伦顿文学评论荣誉奖
12
玛丽
上下快速移动
中国短篇小说选集
玛丽
上下快速移动
布兰妮
上下快速移动
55
"@
$outxml1=新对象-TypeName System.Xml.XmlDocument
$outxml2=新对象-TypeName System.Xml.XmlDocument
$inxml.bookstore
#对于“深度复制”,ImportNode的第三个参数是$true
#完全复制子体的位置
#看https://msdn.microsoft.com/en-us/library/system.xml.xmldocument.importnode(v=vs.110).aspx
#源元素及其指定元素的后代
#递归导入属性节点,并生成
#节点重新组合以形成相应的子树。
$outnode1=$outxml1.ImportNode($inxml.bookstore,$true)
$outxml1.AppendChild($outnode1)
$outxml1
$outxml1.Save(“N:\temp\xml\u desc.xml”)
#ImportNode的第三个参数为$false,表示“不是“深度副本”
#根据上述参考资料:
#导入源元素的指定属性节点,
#生成的XmlAttribute节点附加到
#生成的XmlElement。
$outnode2=$outxml2.ImportNode($inxml.bookstore,$false)
$outxml2.AppendChild($outnode2)
$outxml2
$outxml2.Save(“N:\temp\xml\u nodesc.xml”)
写入主机“结束”
输出文件xml_desc.xml为:

<bookstore specialty="novel">
  <book style="autobiography">
    <author>
      <first-name>Joe</first-name>
      <last-name>Bob</last-name>
      <award>Trenton Literary Review Honorable Mention</award>
    </author>
    <price>12</price>
  </book>
  <book style="textbook">
    <author>
      <first-name>Mary</first-name>
      <last-name>Bob</last-name>
      <publication>Selected Short Stories of
    <first-name>Mary</first-name><last-name>Bob</last-name></publication>
    </author>
    <editor>
      <first-name>Britney</first-name>
      <last-name>Bob</last-name>
    </editor>
    <price>55</price>
  </book>
</bookstore>
<bookstore specialty="novel" />

乔
上下快速移动
特伦顿文学评论荣誉奖
12
玛丽
上下快速移动
中国短篇小说选集
Maryb
布兰妮
上下快速移动
55
而xml_nodesc.xml是:

<bookstore specialty="novel">
  <book style="autobiography">
    <author>
      <first-name>Joe</first-name>
      <last-name>Bob</last-name>
      <award>Trenton Literary Review Honorable Mention</award>
    </author>
    <price>12</price>
  </book>
  <book style="textbook">
    <author>
      <first-name>Mary</first-name>
      <last-name>Bob</last-name>
      <publication>Selected Short Stories of
    <first-name>Mary</first-name><last-name>Bob</last-name></publication>
    </author>
    <editor>
      <first-name>Britney</first-name>
      <last-name>Bob</last-name>
    </editor>
    <price>55</price>
  </book>
</bookstore>
<bookstore specialty="novel" />

这似乎符合OP的要求


我希望这能帮助一些人,尽管现在帮助OP已经太晚了。

谢谢你的快速回答。这确实解决了我刚刚解决的问题。我在寻找更高效的方法。@srikanthpeetha你是说它不应该解析所有的XML,还是说“更高效”是什么意思?如果是这种情况,那么您可能需要使用正则表达式或类似的东西。我认为没有任何XML解析器支持部分解析。是的,我也找不到任何XML解析器。顺便说一句,$parent=$xmlDocument.ChildNodes[0]抛出错误,表示ChildNodes不支持索引