Menu

#17 Attribute value changes by Node value

New
None
Medium
Defect
2015-08-07
2015-04-24
oam333
No

NativeXML's version 4.09.

If set value to one Node, then add to this Node an attribute and set value to this attribute - it's ok. But if after that update a value of Node then updates Attribute value, but not Node.

procedure TForm1.Button1Click(Sender: TObject);
var aaa: TNativeXml;
begin
  aaa:= TNativeXml.Create(Self);
  aaa.CreateName('Root');
  aaa.Root.NodeNew('bbb');

  aaa.Root.NodeByName('bbb').Value:='abc';

  aaa.Root.NodeByName('bbb').AttributeAdd('p', '123');

  aaa.Root.NodeByName('bbb').Value:='cba';

  aaa.XmlFormat := xfReadable;
  aaa.SaveToFile('d:\123.xml');
end;

Result:

<?xml version="1.0" encoding="utf-8"?>
<Root>
    <bbb p="cba">abc</bbb>
</Root>

Discussion

  • oam333

    oam333 - 2015-04-24

    I don't know am i right or not, but after few hours i found a solution. This solution I was completely satisfied (for me).
    I have replaced in source code 1 function and 1 procedure (TsdElement.GetValue and TsdElement.SetValue) with these:

    function TsdElement.GetValue: Utf8String;
    var i: smallint;
    begin
      // Return the value of the CharData subnode designated by the parser
      if (FValueIndex >= 0) and (FValueIndex < FNodes.Count) then
      begin
        // chardata value at FValueIndex
        // This calls TsdCharData.GetValue(),
        // then TsdCharData.GetCoreValue().
    
        // ----------------------------------------------
        // this block i add
        // ----------------------------------------------
        for i:=0 to FNodes.Count-1 do
           begin
             if FNodes[i].Name = 'CharData' then
               begin
                 Result := FNodes[i].Value;
                 exit;
               end;
           end;
        // ----------------------------------------------
    
        Result := FNodes[FValueIndex].Value;
    
        // BUGFIX: no longer allowed, value is already unnormalized in chardata
    {    // do un-normalisation if mac/windows
        if GetEolStyle <> esLF then
          Result := sdUnNormaliseEol(Result, GetEolStyle);}
    
      end else
        // default value
        Result := '';
    end;
    
    procedure TsdElement.SetValue(const Value: Utf8String);
    var
      Res: Utf8String;
      Node: TXmlNode;
      i: smallint;
    begin
      if Length(Value) > 0 then
      begin
        // value that will be set.
        Res := Value;
    
        // add or update a value
        if FValueIndex < 0 then
        begin
    
          // we do not have a value node, so we will add it after FDirectNodeCount
          Node := TsdCharData.Create(TNativeXml(FOwner));
          Node.Value := Res;
          NodeInsert(FDirectNodeCount, Node);
          FValueIndex := FDirectNodeCount;
    
        end else
        begin
    
          // just update the value
    
          // ----------------------------------------------
          // this block i add
          // ----------------------------------------------
          for i:=0 to FNodes.Count-1 do
             begin
               if FNodes[i].Name = 'CharData' then
                 begin
                   FNodes[i].Value := Res;
                   exit;
                 end;
             end;
          // ----------------------------------------------
    
          FNodes[FValueIndex].Value := Res;
    
        end;
      end else
      begin
        // remove the value
        if FValueIndex >= 0 then
          NodeDelete(FValueIndex); // this sets it to -1 as well
      end;
    end;
    

    When a Node has an attributes the variable "FValueIndex" must not be always "0", because "FNodes" has several elements like (Node1attr1, Node1attr2, ... , Node1Value, Node2attr1, Node2attr2, Node2attr3, ... , Node2Value, etc). And when "FValueIndex" is always equal "0" - FNodes[FValueIndex].Value always get a value of the first element in FNodes, which in current situation is attribute of Node, but not Node.

     
  • simdesign

    simdesign - 2015-04-24

    Hello Oam333,

    Thanks for your effort and time in trying to fix NativeXml. I am not sure if I can implement your proposed changes in a timely manner but I will try. I am disabled since nov 2008 due to a horseriding acct and subsequent cerebral infarction. So I can not respond immediately but will try.

    One suggestion is that I also still have an old "nativexml" version online here, called RelaxXml now. This old version has less problems, but also is less efficient and performant. However, you can test if it works for you.

    Kind regards and nice to see you @ sourceforge :)

    Nils Haeck M.Sc. aka mellobot

     

    Last edit: simdesign 2015-04-24

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.