将元素添加到 XML [英] Add Element to XML

查看:53
本文介绍了将元素添加到 XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向现有 XML 添加一个元素(连接器),这是成功的,但我需要删除 xmlns= 并想向它添加一个值.连接器 blaat 是用我的代码添加的.

I want to add a element (connector) to a existing XML, this was succesfull but I need to remove the xmlns= and want to add an value to it. The connector blaat is added with my code.

XML:

<?xml version="1.0"?>
<configuration xmlns="urn:activemq" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xi="http://www.w3.org/2001/XInclude" xsi:schemaLocation="urn:activemq /schema/artemis-configuration.xsd">
  <core xmlns="urn:activemq:core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:activemq:core ">
    <connectors>
      <!-- Connector used to be announced through cluster connections and notifications -->
      <connector name="artemis">tcp://xxxxxxx:61616</connector>
      <connector name="blaat" xmlns="" />
    </connectors>
  </core>
</configuration>

[xml]$xml = Get-Content d:\data\test-broker\etc\broker.xml

$xml.configuration.core.connectors.connector.ChildNodes.Item(0).value

$Node = $xml.CreateElement("connector");
$Node.SetAttribute("name", "blaat");
$xml.configuration.core.connectors.AppendChild($node)
$xml.configuration.core.connectors.connector.SetValue("tcp://");
$xml.Save("d:\data\test-broker\etc\broker.xml")

我希望 XML 是这样的:

I want the XML to be like this:

<?xml version="1.0"?>
<configuration xmlns="urn:activemq" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xi="http://www.w3.org/2001/XInclude" xsi:schemaLocation="urn:activemq /schema/artemis-configuration.xsd">
  <core xmlns="urn:activemq:core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:activemq:core ">
    <connectors>
      <!-- Connector used to be announced through cluster connections and notifications -->
      <connector name="artemis">tcp://xxxx1:61616</connector>
      <connector name="blaat">tcp://xxxxx2:61616</connector>
    </connectors>
  </core>
</configuration>

推荐答案

您的 XML 数据使用命名空间,因此您需要注意这一点.<core> 节点定义了一个默认命名空间(xmlns="urn:activemq:core"),它适用于它的所有子节点.创建一个命名空间管理器并将该命名空间添加到其中:

Your XML data uses namespaces, so you need to take care of that. The <core> node defines a default namespace (xmlns="urn:activemq:core") that applies to all of its child nodes. Create a namespace manager and add that namespace to it:

$nm = New-Object Xml.XmlNamespaceManager $xml.NameTable
$nm.AddNamespace('foo', 'urn:activemq:core')

选择要将新节点附加到的节点:

Select the node to which you want to append your new node:

$cn = $xml.SelectSingleNode('//foo:connectors', $nm)

创建新节点时指定其默认命名空间,然后设置节点的属性和值:

When creating the new node specify its default namespace, then set the node's attribute(s) and value:

$node = $xml.CreateElement('connector', $cn.NamespaceURI)
$node.SetAttribute('name', 'blaat')
$node.InnerText = 'tcp://xxxxx2:61616'

现在您可以将新节点附加到预期的父节点,而不会获得虚假的 xmlns 属性:

Now you can append the new node to the intended parent without getting a spurious xmlns attribute:

[void]$cn.AppendChild($node)

这篇关于将元素添加到 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆