user2306408
user2306408

Reputation:

IXMLDocument unable to read data because IXMLNode.Text property always empty

I'm trying to read XML file with Delphi TXMLDocument parser but I'm unable to reach data in the nodes and I'm not able to figure out why this is happening. The XML is:

<?xml version="1.0" encoding="UTF-8"?>
<types:NotificaScarto xmlns:types="http://www.fatturapa.gov.it/sdi/messaggi/v1.0" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" versione="1.0" xsi:schemaLocation="http://www.fatturapa.gov.it/sdi/messaggi/v1.0 MessaggiTypes_v1.0.xsd ">
  <IdentificativoSdI>111</IdentificativoSdI>
  <NomeFile>IT01234567890_11111.xml.p7m</NomeFile>
  <DataOraRicezione>2013-06-06T12:00:00Z</DataOraRicezione>
  <RiferimentoArchivio>
    <IdentificativoSdI>100</IdentificativoSdI>
    <NomeFile>IT01234567890_11111.zip</NomeFile>
  </RiferimentoArchivio>
  <ListaErrori>
    <Errore>
      <Codice>00100</Codice>
      <Descrizione>Certificato di firma scaduto</Descrizione>
    </Errore>
  </ListaErrori>
  <MessageId>123456</MessageId>
  <Note>Note</Note>
</types:NotificaScarto>

I need to read the "DataOraRicezione" node so I'm using this code:

procedure TForm1.Button1Click(Sender: TObject);
var
  XMLD : IXMLDocument;
  N0,N1 : IXMLNode;
begin
  XMLD:=TXMLDocument.Create(Application);
  XMLD.LoadFromFile('d:\IT01131820936_00175_NS_001.xml');  // the XML is on a file
  XMLD.Active:=TRUE;
  N0:=XMLD.ChildNodes['types:NotificaScarto'];
  N1:=N0.ChildNodes['DataOraRicezione'];
  ShowMessage(N1.Text);  // Empty ?????
end;

I suppose that the problem could be related to the schema. I never read till now xml files like these I'm a newbie about it so I'm not capable to read that value. I search for similar answers but I couldn't reach my goal. Am I missing something?

Thank you Davide

Upvotes: 3

Views: 776

Answers (1)

RRUZ
RRUZ

Reputation: 136441

In order to read the node value you must use the FindNode method.

function FindNode(NodeName, NamespaceURI: DOMString): IXMLNode; overload;

Like so

  N0:=XMLD.ChildNodes['types:NotificaScarto'];
  N1:=N0.ChildNodes.FindNode('DataOraRicezione', '');
  ShowMessage(N1.Text); 

The NamespaceURI must be blank for this particular case because the namespace is not included in the node itself.

This will return

2013-06-06T12:00:00Z

Upvotes: 3

Related Questions