如何将子节点结构从一个XML文件复制到另一个XML文件(合并两个XML文件)?
我有以下两个XML文件:
I have the following two XML files:
File1
<?xml version="1.0"?>
<main>
<node1>
<subnode1>
<value1>101</value1>
<value2>102</value2>
<value3>103</value3>
</subnode1>
<subnode2>
<value1>501</value1>
<value2>502</value2>
<value3>503</value3>
</subnode2>
</node1>
</main>
File2
<?xml version="1.0"?>
<main>
<node1>
<subnode1>
<value1>454</value1>
<value2>471</value2>
<value3>498</value3>
</subnode1>
<subnode2>
<value1>723</value1>
<value2>645</value2>
<value3>823</value3>
</subnode2>
</node1>
</main>
在Delphi中,我想添加完整的< node1> ...< / File2到File1的node1>
结构重命名为< node2> ...< / node2>
。因此结果应如下所示:
In Delphi I would like to add the complete <node1>...</node1>
structure of File2 to File1 renamed as <node2>...</node2>
. So the result should look like this:
<?xml version="1.0"?>
<main>
<node1>
<subnode1>
<value1>101</value1>
<value2>102</value2>
<value3>103</value3>
</subnode1>
<subnode2>
<value1>501</value1>
<value2>502</value2>
<value3>503</value3>
</subnode2>
</node1>
<node2>
<subnode1>
<value1>454</value1>
<value2>471</value2>
<value3>498</value3>
</subnode1>
<subnode2>
<value1>723</value1>
<value2>645</value2>
<value3>823</value3>
</subnode2>
</node2>
</main>
我已经问过如何提取< node1> ...< / node1>
中的问题
如何将节点的内部文本和XML提取为字符串?
(这确实是一个XY问题,很抱歉),我可以找到一种解决方案
I asked already how to extract the <node1>...</node1>
block in the question
How to extract the inner text and XML of node as string?
(which was indeed a XY-problem, sorry about that) and I could work out a solution with manipulating the XML as strings.
但是我想可能有一个更好的解决方案可以直接使用XML功能。那么如何在Delphi 10中实现呢?
But I suppose there might be a better solution working directly with the XML functionality. So how could I implement this in Delphi 10?
将XML文件解析为 TXMLDocument
/ IXMLDocument
对象,这相当简单克隆/移动 IXMLNode
对象从一个文档到另一个文档(请参见 IXMLNode.CloneNode()
或 IXMLNodeList.Add()
和 IXMLNodeList.Remove()
)。尽管您不能重命名一个节点,但是您可以创建具有所需名称的新节点(请参见 IXMLNode.AddChild()
),然后克隆/移动旧的
Once you have parsed the XML files into TXMLDocument
/IXMLDocument
objects, it is fairly trivial to clone/move IXMLNode
objects from one document to the other (see IXMLNode.CloneNode()
, or IXMLNodeList.Add()
and IXMLNodeList.Remove()
). Although you can't rename a node, you can create a new node with the desired name (see IXMLNode.AddChild()
), and then clone/move the old node's children underneath the new node.
您不应在这些任务中使用XML字符串。而是操纵DOM树。因此,您将在File2中获得< node1>
的 IXMLNode
,并添加一个新的在File1中为
,然后克隆/移动< node2>
的IXMLNode < subnode1>
和< subnode2>
从File2中的 IXMLNode
到 IXMLNode $ c
You should not be using XML strings for these tasks. Manipulate the DOM tree instead. So, you would get the IXMLNode
for <node1>
in File2, add a new IXMLNode
for <node2>
in File1, and then clone/move <subnode1>
and <subnode2>
from the IXMLNode
in File2 to the IXMLNode
in File1.
话虽这么说,为什么要按顺序命名元素? < node>
,< subnode>
,< value>
等就足够了。您可以有多个< node>
s,多个< subnode>
s,多个根据需要在一个文档中< value>
。 XML非常适合使用重复的元素名称。您实际上并不需要在元素名称中使用序号,例如:
That being said, why are your elements named sequentially to begin with? <node>
, <subnode>
, <value>
, etc would suffice just fine. You can have multiple <node>
s, multiple <subnode>
s, multiple <value>
's in one document as needed. XML is good about using repeated element names. You don't really need to use sequential numbers in element names, eg:
<?xml version="1.0"?>
<main>
<node>
<subnode>
<value>101</value>
<value>102</value>
<value>103</value>
</subnode>
<subnode>
<value>501</value>
<value>502</value>
<value>503</value>
</subnode>
</node>
<node>
<subnode>
<value>454</value>
<value>471</value>
<value>498</value>
</subnode>
<subnode>
<value>723</value>
<value>645</value>
<value>823</value>
</subnode>
</node>
</main>
如果您确实需要按数字进行区分,则可以改用属性,例如:
If you really needed to differentiate by numbers, you could use attributes instead, eg:
<?xml version="1.0"?>
<main>
<node id="1">
<subnode id="1">
<value id="1">101</value>
<value id="2">102</value>
<value id="3">103</value>
</subnode>
<subnode id="2">
<value id="1">501</value>
<value id="2">502</value>
<value id="3">503</value>
</subnode>
</node>
<node id="2">
<subnode id="1">
<value id="1">454</value>
<value id="2">471</value>
<value id="3">498</value>
</subnode>
<subnode id="2">
<value id="1">723</value>
<value id="2">645</value>
<value id="3">823</value>
</subnode>
</node>
</main>