Recent

Author Topic: [SOLVED] How To change the NodeValue of a TDOMNode :o  (Read 3117 times)

br4d

  • New Member
  • *
  • Posts: 26
[SOLVED] How To change the NodeValue of a TDOMNode :o
« on: May 23, 2017, 10:36:59 am »
  Hi guys,
I wanto to modify an XML file which contains  something like

<PRICE>28</PRICE>


I have created a working XMLParser which read my xml input files using Laz2_DOM, laz2_XMLRead, laz2_XMLWrite.

After parsing I got some TDOMNode and I want to change their NodeValues. But to my suprise the implementation of TDOMNode is empty

procedure TDOMNode.SetNodeValue(const AValue: DOMString);
begin
  // do nothing
end;


Any idea how to do it?
« Last Edit: May 24, 2017, 02:35:27 am by br4d »

br4d

  • New Member
  • *
  • Posts: 26
Re: How To change the NodeValue of a TDOMNode :o
« Reply #1 on: May 23, 2017, 11:12:41 am »
My Procedure is:

procedure TMainForm.FillAvgPrices;
var
  itemLineNodeList: TDOMNodeList;
  curNode: TDOMNode;
  i: Integer;
begin
  try
     //ShowMessage('Starting path: ' + GetCurrentDir());
     ReadXMLFile(XmlDoc, xmlInputFile, [xrfPreserveWhiteSpace]) ;

     itemLineNodeList:= XmlDoc.GetElementsByTagName('ITEMLINE');
     if not Assigned(itemLineNodeList) then exit;

     for i:=0 to itemLineNodeList.Length-1 do begin
       curNode:= itemLineNodeList;
       curNode.NodeValue:=' AAAAA '  ;
       showDebugDlg('ItemNodelist value before procedure call is ' + curNode.NodeValue);
       replaceAVGPrice(TDOMCharacterData(itemLineNodeList));
       showDebugDlg('Itemnode after setting average ' + itemLineNodeList.NodeValue);
     end;
     myWriteXMLFile('accExt\diana_invoice_selfMade.xml');
  finally
    XmlDoc.Free;
  end;

end;

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11351
  • FPC developer.
Re: How To change the NodeValue of a TDOMNode :o
« Reply #2 on: May 23, 2017, 11:49:33 am »
shouldn't

Code: Pascal  [Select][+][-]
  1.   for i:=0 to itemLineNodeList.Length-1 do begin
  2.        curNode:= itemLineNodeList;
  3.  

be
Code: Pascal  [Select][+][-]
  1.   for i:=0 to itemLineNodeList.Length-1 do begin
  2.        curNode:= itemLineNodeList[i];
  3.  

or something ?

br4d

  • New Member
  • *
  • Posts: 26
Re: How To change the NodeValue of a TDOMNode :o
« Reply #3 on: May 24, 2017, 02:34:41 am »
Yup, it's a a mistake coming from debugging. Usually I would not create an extra variable, but simply pass the node in the list.

But it seems the nodeValue can not be changed, fortunately in my case the node value is a leaf. I simply remove the leaf node and create a new one with the correct price.

But in case the node to be changed is a complex one and thus too expensive too recreate, there is some info in the source code should someone has the same problem:
 
{ NodeType, NodeName and NodeValue had been moved from fields to functions.
  This lowers memory usage and also obsoletes most constructors,
  at a slight performance penalty. However, NodeName and NodeValue are
  accessible via fields using specialized properties of descendant classes,
  e.g. TDOMElement.TagName, TDOMCharacterData.Data etc.}

So you can cast the node to TDOMCharacterData and change the DATA not the NodeValue. Internal, the data property will set the node value.

Hope it helps and thanks to the helpful people here.

 

TinyPortal © 2005-2018