Menu

#21 Section name with multiple words

New
None
Medium
Review
2016-04-18
2016-04-18
Haykal HM
No

Hallo, I use TNativeXML v4.09 to create XML like this :

procedure TForm1.Button1Click(Sender: TObject);
var aaa: TNativeXml;
vSectionName : string;
begin
vSectionName := 'word1 word2';
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;

and then the XML file result :

<Root> <word1 word2="">this is value</word1> </Root>

Its is look normal, but when I want to get the section with name as vSectionName like this :

procedure TForm1.Button2Click(Sender: TObject);
var aaa : TNativeXml;
vSectionName : string;
vNode : TXmlNode;
begin
vSectionName := 'word1 word2';
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;

I always get message : 'Section not found'.

Then to track this error with this in NodeByName function:

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;

I got the section name with multiple word name ("word1 word2") change to single word name ("word1") when we load from file XML.
Why this happen?

Thanks in advance.

Discussion

  • Haykal HM

    Haykal HM - 2016-04-18

    Sorry the XML file like this :

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

    Haykal HM - 2016-04-18

    And code above :

    procedure TForm1.Button1Click(Sender: TObject);
    var aaa: TNativeXml;
        vSectionName : string;
    begin
      vSectionName := 'word1 word2';
      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.Button2Click(Sender: TObject);
    var aaa : TNativeXml;
        vSectionName : string;
        vNode : TXmlNode;
    begin
      vSectionName := 'word1 word2';
      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;
    

    and in NativeXML.TXMLNode.NodeByName function :

    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;
    
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.