如何在Delphi中设置文档元素的前缀 [英] How to set the prefix of a document element in Delphi

查看:167
本文介绍了如何在Delphi中设置文档元素的前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Delphi 2009,我试图获得一个声明的命名空间前缀,以应用到我正在创建的IXMLDocument中的文档元素。一旦创建了文档元素,我可以声明具有前缀的命名空间,但它不会应用于文档元素,我似乎不能更改文档元素的前缀。如果我使用doc.CreateElement(nodename,namespaceURI)创建文档元素,它将指定的URI添加为文档的默认命名空间,这不是我想要做的。我正在创建的这个文档将被添加到已经有一个默认命名空间的另一个文档中。

Using Delphi 2009, I'm trying to get a declared namespace prefix to apply to the document element in an IXMLDocument that I'm creating. Once the document element is created I can declare a namespace with a prefix, but it does not get applied to the document element and I can't seem to change the prefix of the document element. If I use doc.CreateElement(nodename, namespaceURI) to create the document element it add the specified URI as the default namespace for the doc, which is not what I want to do. This document that I'm creating is going to be added to another document that already had a default namespace.

  Result := NewXMLDocument;
  eleDoc := Result.CreateElement(TAG_IH_IMPORT, NS_HISTORIAN);
  eleDoc.DeclareNamespace(FNamespacePrefix, NS_HISTORIAN);

其中TAG_IH_IMPORT和NS_HISTORIAN是字符串常量,eleDoc:IXMLNode和FNamespacePrefix:String。

where TAG_IH_IMPORT and NS_HISTORIAN are string constants, eleDoc: IXMLNode and FNamespacePrefix: String.

输出结果如下所示:

<Import xmlns="uri" xmlns:h="uri" />

我真的想得到h:应用于导入标签。任何建议?

I really want to get that "h:" applied to the Import tag. Any suggestions?

谢谢。

推荐答案

您可以指定命名空间在您调用CreateElement()时的前缀,即:

You can specify the namespace prefix at the time you call CreateElement(), ie:

Result := NewXMLDocument;
eleDoc := Result.CreateElement(FNamespacePrefix + ':' + TAG_IH_IMPORT, NS_HISTORIAN);
eleDoc.DeclareNamespace(FNamespacePrefix, NS_HISTORIAN);
Result.DocumentElement := eleDoc;

或者,您可以创建一个临时文档节点,为其子节点声明前缀,添加一个小孩节点,然后将其分配为新的文档节点。例如:

Alternatively, you can create a temp document node, declare the prefix for its child nodes, add a child node to it, and then assign that as the new document node. For example:

Result := NewXMLDocument;
eleTemp := Result.CreateElement('temp', '');
eleTemp.DeclareNamespace(FNamespacePrefix, NS_HISTORIAN);
eleDoc := eleTemp.AddChild(TAG_IH_IMPORT, NS_HISTORIAN);
Result.DocumentElement := eleDoc;

这篇关于如何在Delphi中设置文档元素的前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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