【问题标题】:Powershell and XML subnodesPowershell 和 XML 子节点
【发布时间】:2014-04-08 17:33:03
【问题描述】:

我有这个 XML:

<?xml version="1.0" encoding="utf-8"?>
<Man schemaVersion="1">
    <version>2.2</version>
    <file>
        <properties>
        …
        </properties>
        <group> 
            <properties>
        ...
            </properties>
        </group>                     
        <group>
            <properties>
        …
            </properties>       
            <group>
                <properties>
                    <items>
                        <name>test</name>
                        <description>A test</description>
                    </items>
                </properties>
            </group>
            <group>
                <properties>
                    <items>
                        <name>test2</name>
                        <description>A test field again</description>
                    </items>
                </properties>
            </group>
        </group>
    </file>
</Man>

我想用powershell添加一个子节点(?)包含以下内容:

    <group>
        <properties>
            <items>
                <name>test3</name>
                <description>one more field</description>
            </items>
        </properties>
    </group>

正好在 test2 节点下,看起来像这样:

    <group>
        <properties>
            <items>
                <name>test</name>
                <description>A test</description>
            </items>
        </properties>
    </group>
    <group>
        <properties>
            <items>
                <name>test2</name>
                <description>A test field again</description>
            </items>
        </properties>
    </group>
    <group>
        <properties>
            <items>
                <name>test3</name>
                <description>one more field</description>
            </items>
        </properties>
    </group>

问题是“组”被识别为数组,我无法向数组添加元素。我尝试了很多技巧来添加元素但不在数组中

代码(正如我所说,它处于非常初级的阶段,我首先关心的是获得一些测试值。

$strXMLfile="c:\blabla\bla.xml";
$xml=get-content $strXMLfile;
$xmlRoot=$xml.get_DocumentElement();
$xmlnode=$xmlRoot.file.group;
$group=0;
while (!($xmlnode[$group].properties.name -eq "test node")) {
    $group++
}
$nodesGroups=$xmlnode[$group].group;
if ($nodesGroups.count -eq $null) {
    $intNewGroup=2
}   
else {
    $intNewGroup=$nodesGroups.count+1
}

#Here should be the code for the group,properties,items line creation. Partially

$newline=$xml.CreateElement("items")

#Here should be the code for the <name>. Not ready yet

【问题讨论】:

  • 我从 XML 中添加了一些内容,以注意外部 不止一个。

标签: xml powershell


【解决方案1】:

您可以使用AppendChild 方法将新的最后一个子元素添加到现有元素(类型XmlElement)。因此,与其操纵 &lt;group&gt; 元素的数组,不如操纵它们的共同父元素(&lt;file&gt; 元素)。

【讨论】:

  • 你能说得更具体一点吗,因为每次我尝试将一个孩子附加到 时,它都在 下。
  • @Alex:对不起,我误读了 XML 示例(file 下的所有 group 元素)。对于这种操作,您需要使用某种方法(例如 XPath)来查找单个节点。在这种情况下,PSH 的包装器/助手可能会妨碍您。直接使用 System.Xml 类型更容易。是否有机会将示例剥离为更易于使用而无需重新构建您的应用程序......您当前的代码是什么?
  • 我没有任何代码,因为我正在测试和测试,直到我能够指向我想要的节点。
  • @Alex 所以你有 测试代码 正在进行中:这就是我们需要看到的。
  • 我提出了一些问题。这是我的主要工作,因为我一步一步地工作。如果能够达到添加到 我相信其余的会很容易。
【解决方案2】:

希望这可以帮助您入门:

 > $xml = [XML]"<Man>...</Man>"
 > $newGrp = $xml.CreateElement("group")
 > $newGrp.InnerText = "group 3"
 > $xml.Man.file.group.AppendChild($newGrp)

【讨论】:

  • 你能清除我的第一行吗?这三个点是这样的: $xml = [XML]"" ?因为我尝试过,我得到:方法调用失败,因为 [System.String] 不包含名为“AppendChild”的方法。
  • ... 代表您的内部 XML。我实际上将您发布的内容复制到 c:\temp.xml 中,并做了一个$xml = [XML](Get-Content c:\temp.xml)。然后只需找到您想要的节点即可。然后,您可以通过执行$xml.Man$xml.Man.file 之类的操作来导航节点树。这将返回一个 XmlElements 数组。您可以对它们执行任何操作,就像对相同类型的常规 .Net 对象执行操作一样。
  • 看来我对我想要的东西很封闭。我的第一次测试有点成功。当然,我不仅需要添加 ,还需要将里面的 添加到里面的 和最后一个 的值。对于此示例,“组 3”将转到 节点。所以我还需要创建内部节点。我不知道是否可以用一个命令(这将是超级)来做到这一点
  • 可以像这样组合其中一些命令:$xml.Man.file.group.AppendChild($xml.CreateElement("group").AppendChild($xml.CreateElement("properties")))。只是玩,直到你得到正确的排序。但是用几个命令来构建所需的节点结构可能会更易于管理。
  • 有些东西不工作。使用上述命令或使用 split,它只会创建 this -> 和 this -> 。结果,当我将命令扩展为具有某些值的 时, 作为父节点作为
猜你喜欢
  • 2020-07-14
  • 1970-01-01
  • 2023-03-03
  • 2021-12-28
  • 2022-01-01
  • 1970-01-01
  • 2011-07-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多