Recent

Author Topic: fcl-xml domstring to string ?  (Read 10338 times)

JC

  • Newbie
  • Posts: 2
fcl-xml domstring to string ?
« on: March 02, 2010, 11:50:35 am »
Hello,

is possible convert PassNode.FirstChild.NodeValue (or PassNode.TextContent) to string ?

Code: [Select]
var
  PassNode: TDOMNode;
  Doc:      TXMLDocument;
 begin
  // Read in xml file from disk
  ReadXMLFile(Doc, 'c:\xmlfiles\test.xml');
  // Retrieve the "password" node
  PassNode := Doc.DocumentElement.FindNode('password');
  // Write out value of the selected node
  WriteLn(PassNode.NodeValue); // will be blank
  // The text of the node is actually a separate child node
  WriteLn(PassNode.FirstChild.NodeValue); // correctly prints "abc"
  // alternatively
  WriteLn(PassNode.TextContent);
  // finally, free the document
  Doc.Free;
end;

from http://wiki.freepascal.org/XML_Tutorial

Thanks


marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11382
  • FPC developer.
Re: fcl-xml domstring to string ?
« Reply #1 on: March 02, 2010, 02:58:52 pm »
Convert how what where? If you try the above code, what goes wrong? :o

JC

  • Newbie
  • Posts: 2
Re: fcl-xml domstring to string ?
« Reply #2 on: March 02, 2010, 03:30:31 pm »
ah.. problem is here:

 var
  PassNode: TDOMNode;
  Doc:      TXMLDocument;
   s: string;

begin
     ReadXMLFile(Doc, 'test.xml');
     PassNode := Doc.DocumentElement.FindNode('name');
     s := PassNode.FirstChild.NodeValue;
     ..
     Doc.Free;
 end;


No problems with compiling but in program it make "access violation" error.

eny

  • Hero Member
  • *****
  • Posts: 1634
Re: fcl-xml domstring to string ?
« Reply #3 on: March 02, 2010, 03:33:24 pm »
You must check if PassNode.FirstChild is assigned:

Code: [Select]
if assigned(PassNode.FirstChild) then
  s := PassNode.FirstChild.NodeValue;
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

LuizAmérico

  • Sr. Member
  • ****
  • Posts: 457
Re: fcl-xml domstring to string ?
« Reply #4 on: March 02, 2010, 05:11:40 pm »
Also, if you are working with UTF8 strings will be necessary to do the proper conversion:

s := UTF8Encode(PassNode.FirstChild.NodeValue);

delphi887

  • New Member
  • *
  • Posts: 30
    • my personal website
Re: fcl-xml domstring to string ?
« Reply #5 on: August 28, 2012, 12:17:10 pm »
ah.. problem is here:

 var
  PassNode: TDOMNode;
  Doc:      TXMLDocument;
  s: string;

begin
     ReadXMLFile(Doc, 'test.xml');
     PassNode := Doc.DocumentElement.FindNode('name');
     s := PassNode.FirstChild.NodeValue;
     ..
     Doc.Free;
 end;


No problems with compiling but in program it make "access violation" error.
hi
I'm having the same problem and I did not find a solution
Please help me
Thanks

eny

  • Hero Member
  • *****
  • Posts: 1634
Re: fcl-xml domstring to string ?
« Reply #6 on: August 28, 2012, 08:24:50 pm »
hi
I'm having the same problem and I did not find a solution
Please help me
Thanks
What?
If you kick a 2.5 year old topic, then at least please read what was posted here.
Or open a new topic where you explain what the problem is you are experiencing.
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

 

TinyPortal © 2005-2018