Lazarus

Programming => General => Topic started by: JC on March 02, 2010, 11:50:35 am

Title: fcl-xml domstring to string ?
Post by: JC 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

Title: Re: fcl-xml domstring to string ?
Post by: marcov on March 02, 2010, 02:58:52 pm
Convert how what where? If you try the above code, what goes wrong? :o
Title: Re: fcl-xml domstring to string ?
Post by: JC 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.
Title: Re: fcl-xml domstring to string ?
Post by: eny 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;
Title: Re: fcl-xml domstring to string ?
Post by: LuizAmérico 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);
Title: Re: fcl-xml domstring to string ?
Post by: delphi887 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
Title: Re: fcl-xml domstring to string ?
Post by: eny 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.
TinyPortal © 2005-2018