[fpc-pascal] getting XML element's value using dom.pp unit

Sebastian Günther sguenther at gmx.de
Wed Dec 7 17:14:42 CET 2005


Bisma Jayadi schrieb:
> Hi all...
> 
> I'm currently developing a Lazarus application which need to read an XML
> file. In order to do such a task, FPC has provided DOM unit (dom.pp).
> But, it seems that dom.pp is unable to read XML element's (text) value.
> It succesfully read element's name (through NodeName property) and
> attributes (name and value) though. Getting element's name (through
> NodeValue property) always return an empty string.
> 
> What did I do wrong? Or am I missing something about the dom.pp usage?
> Here are the codes I used...
> 
> procedure TForm1.Button1Click(Sender: TObject);
> var
>   doc: TXMLDocument;
> begin
>   ReadXMLFile(doc, 'sample.xml');
> 
>   // this returns correct first attribute's name
>  
> Memo1.Lines.Add(doc.DocumentElement.FirstChild.Attributes.Item[0].NodeName);

btw, you can leave out the ".Item" part, as it's a default property. So
"Attributes[0]" would be sufficient.


>   // this returns correct first attribute's value
>  
> Memo1.Lines.Add(doc.DocumentElement.FirstChild.Attributes.Item[0].NodeValue);
> 
>   // this returns correct first element's name
>   Memo1.Lines.Add(doc.DocumentElement.FirstChild.FirstChild.NodeName);
>   // this returns nothing !!!
>   Memo1.Lines.Add(doc.DocumentElement.FirstChild.FirstChild.NodeValue);

Perhaps it contains whitespace? It's possible that the XML parser
creates more than one text node.
Do you have an excerpt of your XML file? This would be very helpful.


- Sebastian



More information about the fpc-pascal mailing list