【问题标题】:Section name with multiple words in TNativeXMLTNativeXML 中包含多个单词的部分名称
【发布时间】:2016-04-19 04:24:39
【问题描述】:

我正在尝试使用 NativeXML v4.09 制作 XML 文件,并以如下结构使用它:

<?xml version="1.0" encoding="utf-8"?>
<Root>
   <word1 word2>this is value</word1 word2>
</Root>

我这样写简单的代码:

procedure TForm1.ButtonWriteClick(Sender: TObject);
var aaa: TNativeXml;
    vSectionName : string;
begin
    vSectionName := 'word1 word2';//Name of Section with two words sparated with space
    aaa:= TNativeXml.Create(Self);
    aaa.CreateName('Root');
    aaa.Root.NodeNew(vSectionName);

    aaa.Root.NodeByName('word1 word2').Value:='this is value';

    aaa.XmlFormat := xfReadable;

    aaa.SaveToFile('test.xml');
end;

为了读取节的节点,我编写如下代码:

procedure TForm1.ButtonReadClick(Sender: TObject);
var aaa : TNativeXml;
    vSectionName : string;
    vNode : TXmlNode;
begin
    vSectionName := 'word1 word2';//Name of Section with two words sparated with space
    try
       aaa := TNativeXml.Create(Self);
       aaa.LoadFromFile('test.xml');
       vNode := aaa.Root.NodeByName(vSectionName);
       if vNode=nil then
          ShowMessage('Section not found')
       else
          ShowMessage('Section found');
    finally
       FreeAndNil(aaa);
    end;
end;

我可以创建上面我想要的结构的 XML 文件“test.xml”。但是当我想读取带有两个单词名称(如“word1 word2”名称)的节点部分时,总是收到消息“未找到部分”,因为 vNode 总是来自 NodeByName 函数的 NIL .

然后为了跟踪错误,我在 NativeXML.TXMLNode.NodeByName 函数中添加了一些代码,如下所示:

function TXmlNode.NodeByName(const AName: Utf8String): TXmlNode;
var i: integer;
    vF : boolean;
begin
    result := nil;
    for i := 0 to GetNodeCount - 1 do begin
       vF := (Utf8CompareText(GetNodes(i).Name, AName) = 0);
       if vF then
          Application.MessageBox(PWideChar('TXmlNode.NodeByName-> Found AName='+AName+'|GetNodes('+IntToStr(i)+').Name='+GetNodes(i).Name+'|GetNodes('+IntToStr(i)+').NameUnicode='+GetNodes(i).NameUnicode),'TXmlNode.NodeByName',0)
       else
          Application.MessageBox(PWideChar('TXmlNode.NodeByName-> Not Found AName='+AName+'|GetNodes('+IntToStr(i)+').Name='+GetNodes(i).Name+'|GetNodes('+IntToStr(i)+').NameUnicode='+GetNodes(i).NameUnicode),'TXmlNode.NodeByName',0);
       //if (Utf8CompareText(GetNodes(i).Name, AName) = 0) then
       if vF then begin
         Result := GetNodes(i);
         exit;
       end;
    end;
end;

并得到这个消息框:

MessageBox display

我看到部分名称从两个单词(“word1 word2”名称)变为单个单词(“word1”名称)。

这是包含多个单词的部分名称中的错误,名称带有空格还是部分名称必须是一个没有空格的单词?如果节的名称必须是一个或多个单词并且没有空格,为什么我们可以创建带有两个或多个带空格的单词的节名的 XML,但是当我们使用 NodeByName 读取它时却无法获取节节点?

【问题讨论】:

    标签: xml delphi nativexml


    【解决方案1】:

    您的文档不是有效的 XML。标记名称不得包含空格字符。

    XML 规范:XML Start-Tags, End-Tags, and Empty-Element Tags

    【讨论】:

    • 我从一开始就怀疑 XML 元素不能有空格,但我很奇怪为什么我们可以编写带有部分名称的 XML Doc 包含空格。好的,我想我添加了一些代码来修复它。谢谢大家。案件结案。
    • 我们不能,因为它不是有效的 XML;你刚刚发现了。
    【解决方案2】:

    XML 元素的名称中不能有空格。如果 XML 库在创建 XML 文档时允许使用间隔名称,那么该库是错误的。您应该将错误报告给供应商。 (不过,错误报告可能会因为使用错误而被忽略,因为没有正确的程序应该首先尝试使用多词名称。)

    您需要想出一些其他方式来表示您的数据。也许您可以将部分名称存储在属性值或元素内容中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-19
      • 2016-10-07
      • 1970-01-01
      • 2019-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多