Reading in Delphi XML generated in Excel

Asked

Viewed 1,277 times

4

I am generating an XML by Excel itself (file type XML Sheet 2003).

The generated file has the following XML data standard:

<Row>
<Cell><Data ss:Type="String">Williams</Data></Cell>
<Cell ss:StyleID="s63"><Datass:Type="Number">10644</Data></Cell>
<Cell><Data ss:Type="String">UK</Data></Cell>
<Cell><Data ss:Type="String">Qtr 2</Data></Cell>
</Row>

Creating a project in Delphi, using a Dataset and a Datasource, and making the default configuration, Delphi cannot read the fields of this XML, as occurs when we have a file in the pattern below. What to do to convert the Excel spreadsheet into an XML standard that Delphi understands, or how to interpret this XML standard?

<ROW NOME="PAMELA" DT_NASCIMENTO="19521002" DT_CADASTRO="20031020" TELEFONE="2548874"
CELULAR="93712225" EMAIL="[email protected]"/>

3 answers

3

This model referred to

<ROW NOME="PAMELA" DT_NASCIMENTO="19521002" DT_CADASTRO="20031020" TELEFONE="2548874"
CELULAR="93712225" EMAIL="[email protected]"/>

is the Clientdataset data schema and differs from the above-mentioned model, which comes from Excel.

To read the Excel Schema, you must use a XMLDocument and not the TClientDataSet

As in the following example:

var
  XMLDocument: IXMLDocument;
  MainNode, RowNode: TXMLNode;
  I, J: Integer;
begin
  XMLDocument := TXMLDocument.Create(nil);
  try
    XMLDocument.FileName := 'C:\Caminho\Para\Seu\Arquivo.xml';
    XMLDocument.Active := True;

    MainNode := XMLDocument.DocumentElement;

    for I := 0 to MainNode.ChildNodes.Count - 1 do
    begin
      RowNode := MainNode.ChildNodes[I];

      for J := 0 to RowNode.ChildNodes.Count - 1 do
      begin
        //Cada item aqui é uma célula e vc pode obter o valor
      end;
    end;

    XMLDocument.Active := False;
  finally
    XMLDocument := nil; //Ele é uma interface e é liberado automaticamente
  end;
  • Incompatible types: 'Txmldocument' and 'Ixmlnode'

  • This appears when compiled the Mainnode... and Rownode line. I left the code exactly as you suggested, just changed the XML address and added Xmldocumet from the Internet tab.

  • @user3771516 I fixed the example. The type of nodes is Txmlnode or Ixmlnode. I am without Deplhi here to test the example. Any other mistake, tell me I see.

  • Ran with Ixmlnode type. Now there is another error that occurs when running the first For line. Access Violation at address 00000000. Read of address 000000. Note: The xml address is right, and it’s the default I put in the initial post. Does it have something different in the file header?

  • @user3771516 apparently Delphi is releasing Xmldocument ahead of time because it is no longer used directly. I will change now to use Ixmldocument and disable it before Finally

  • Same start fault. Incompatible types: 'Txmldocument' and 'Ixmlnode'. If you have any other suggestions, thank you. To break a branch, I’m using Excel + VBA to mount the screens I need.

Show 1 more comment

1

I was able to make Delphi read the xml generated by Excel correctly:

vXMLDoc := TXMLDocument.Create(self);
vXMLDoc.LoadFromFile('Exemplo.xml');

J := vXMLDoc.DocumentElement.ChildNodes.Count;
Memo1.Clear;

for K := 0 to j -1 do
begin
  NodeRec := vXMLDoc.DocumentElement.ChildNodes[K];  
  Memo1.Lines.Add(NodeRec.ChildNodes[0].text + ' -' + 
  NodeRec.ChildNodes[1].text  + '-' 
  + NodeRec.ChildNodes[2].text+ ' -' + NodeRec.ChildNodes[3].text);     
end;

1

You can use the utility Xmlmapper for map the data from the XML file to a Tclientdataset. From there you can import the data into Tclientdataset and manipulate.

  • Good morning.. @Embarbosa.. I’m having trouble generating the . xtr file and . xml by Delphi TOKYO... When generating the files and connecting in CLIENTDATASET the same gives the following error..: XML PARSE ERROR Reason: The system cannot locate the specified object. . And by DELPHI 7 i Gero and use in clientDataSet without problem.. Can you tell me the difference between the XMLMAPPER of Iphi 7 and TOKYO??

  • @Souza as far as I know, no difference that could cause this kind of mistake. It seems more like Xmlmapper is just returning an error reported by Windows. As if the file path you specified did not exist.

  • .Dude, I just figured out why the bug. in Delphi 7 I use Xmltransform and it works beauty.. In Tokyo I went to use the same component there appears this message.. On the net I saw an article that the guy uses the ai Xmltransformprovider WORKS PERFECTLY.. Man .. Thanks for your attention. :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.