Tuesday, December 05, 2006, 6:27:36 PM, Lee wrote:
L> Thanks for the great feedback everyone. I noticed that the wiki does in
L> fact show the "Delphi way" of accessing a node's text which kind of
L> threw me off. I propose to add the following to the wiki under
L> "Networking" under the Basic Example section:
L> For Delphi Programmers:
L> Note that when working with TXMLDocument, the text within a Node is
L> considered a separate TEXT Node. As a result, you must access a node's
L> text value as a separate node. For instance, consider the following XML:
L> <code>
L> <?xml version="1.0" encoding="utf-8"?>
L> <request>
L> <request_type>PUT_FILE</request_type>
L> <username>123</username>
L> <password>abc</password>
L> </request>
L> </code>
L> Consider also the following code example:
L> <code>
L> var
L> PassNode: TDOMNode;
L> Doc: TXMLDocument;
L> begin
L> Doc := TXMLDocument.Create;
L> // Read in xml file from disk
L> ReadXMLFile(Doc, 'c:\xmlfiles\test.xml');
L> // Retrieve the "password" node
L> PassNode := Doc.DocumentElement.FindNode('password');
L> // Write out value of the selected node
L> WriteLn(PassNode.NodeValue); // will be blank
L> // The text of the node is actually a separate child node
L> WriteLn(PassNode.FirstChild.NodeValue); // correctly prints "abc"
L> </code>
L> I figured this might save new Laz/FPC users a bit of consternation.
L> Any suggestions or objections to me adding the above to the
L> wiki/networking/Basic Example section?
L> (http://wiki.lazarus.freepascal.org/Networking#Basic_Example)
Sorry for getting in too late, I was offline in recent days...
NodeValue property is really not the most convenient way to get the
text contained in XML. It had become obvious to guys in w3.org and they
added the TextContent property to DOM level 3 specification. However,
this happened not long ago, and every implementation that existed
before was already using its own way to accomplish the same - all
these Text, InnerText, etc.
I had implemented the TextContent property in FCL, just make sure you
use the latest version. On reading, TextContent retrieves all text
nodes beneath the given node concatenated together (beware, it's
currently not speed optimized in any way and thus can be pretty slow
if you try it on the root of large node tree). On setting, it
will delete all child nodes and append a single text node with the
given value instead. That is what is specified by DOM level 3, except
element content whitespace is not distinguished from regular text.
--
Best regards,
Sergei
_________________________________________________________________
To unsubscribe: mail [EMAIL PROTECTED] with
"unsubscribe" as the Subject
archives at http://www.lazarus.freepascal.org/mailarchives