如何在文本中添加/插入xml节点 [英] How to add/insert a xml node into text

查看:90
本文介绍了如何在文本中添加/插入xml节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在生成基于XML的开放文档格式(odt)的文本文档.但是在文本段落中添加制表符时出现问题.当我在Open Office应用程序中创建它并保存文档时,该段落具有以下内部XML表示形式:

I am generating a text document in the open document format (odt) which is based on XML. But I have a problem when adding a tabulator in a text passage. When I create it in the Open Office application and save the document the passage has the internal XML representation as:

<text:p text:style-name="P5">Prepared by: <text:tab/>Tim Test</text:p>

自动生成时,我将在源代码中介绍这一部分:

When generating it automatically I come to this part in my source code:

   Node, Node1: IXMLNode;
   ...
   Node := Node1.AddChild('text:p');
   Node.Attributes['text:style-name'] := 'P5';
   Node.Text := 'Prepared by: Tim Test';

但是我找不到任何方法可以将节点添加到"Tim"之前的文本中,或者至少没有使用内部Delphi XML库.

But I can't find any method to add the node into the text before "Tim", or at least not with the internal Delphi XML library.

有没有一种方法可以实现它,或者是否有其他的Delphi XML库可以做到这一点?

Is there a way to achieve it or is there any other Delphi XML library which can do that?

推荐答案

请记住,XML是节点的层次结构,包括文本片段.您显示的XML在树中看起来像这样:

Keep in mind that XML is a hierarchy of nodes, including text snippets. The XML you showed looks like this in a tree:

[element] 'text:p'
  │
  ├─[attributes]
  │   │
  │   └─[attribute] 'text:style-name'
  │       │
  │       └─[text] 'PS'
  │
  └─[children]
      |
      ├─[text] 'Prepared by: '
      │
      ├─[element] 'text:tab'
      │ 
      └─[text] 'Tim Test'

这应该可以帮助您可视化如何将节点添加到文档中以获得所需的输出,例如:

That should help you visualize how you have to add nodes to your document to get the desired output, eg:

Node, Node1, Node2: IXMLNode;
...
Node := Node1.AddChild('text:p');
Node.Attributes['text:style-name'] := 'P5';

Node2 := Node.OwnerDocument.CreateNode('Prepared by: ', ntText);
Node.ChildNodes.Add(Node2);

Node2 := Node.OwnerDocument.CreateElement('text:tab', '');
Node.ChildNodes.Add(Node2);

Node2 := Node.OwnerDocument.CreateNode('Tim Test', ntText);
Node.ChildNodes.Add(Node2);

这篇关于如何在文本中添加/插入xml节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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