Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing xml file Delphi

Firstly, I'm new to coding and Delphi, been on and off with it for a few months.

Below is a sample xml file.

What I'm trying to achieve is to Parse all data in each 'Name' section of the .xml file.

I have never done this before. Some guidance would be appreciated. I have looked at other questions on here similar to this but I cant quite get to grips with it.

I have no code example to provide....this is how stuck I am, I do not know where to begin.

<ds>
<Customers>
<Name>
<address_name>test 1</address_name> 
<address_line_1>test 1</address_line_1> 
<address_line_2>test 1</address_line_2> 
<address_line_3>test 1</address_line_3>
<postcode>test 1</postcode> 
<tel_no>test 1</tel_no> 
<fax_no>test 1</fax_no> 
<email_address>test 1<email_address/> 
<website>test 1<website /> 
</Name>
<Name>
<address_name>test 2</address_name> 
<address_line_1>test 2</address_line_1> 
<address_line_2>test 2</address_line_2> 
<address_line_3>test 2</address_line_3>
<postcode>test 2</postcode> 
<tel_no>test 2</tel_no> 
<fax_no>test 2</fax_no> 
<email_address>test 2<email_address/> 
<website>test 2<website /> 
</Name>
<Name>
<address_name>test 3</address_name> 
<address_line_1>test 3</address_line_1> 
<address_line_2>test 3</address_line_2> 
<address_line_3>test 3</address_line_3>
<postcode>test 3</postcode> 
<tel_no>test 3</tel_no> 
<fax_no>test 3</fax_no> 
<email_address>test 3<email_address/> 
<website>test 3<website /> 
</Name>
<Customers>
</ds>

Thanks,

like image 266
Sharpie Avatar asked Dec 06 '22 21:12

Sharpie


2 Answers

A better aproach for your xml file would be:

<ds>
    <Customers>
        <Customer>
            <address_name>test 1</address_name> 
            <address_line_1>test 1</address_line_1> 
            <address_line_2>test 1</address_line_2> 
            <address_line_3>test 1</address_line_3>
            <postcode>test 1</postcode> 
            <tel_no>test 1</tel_no> 
            <fax_no>test 1</fax_no> 
            <email_address>test 1</email_address> 
            <website>test 1</website> 
        </Customer>
        <Customer>
            <address_name>test 2</address_name> 
            <address_line_1>test 2</address_line_1> 
            <address_line_2>test 2</address_line_2> 
            <address_line_3>test 2</address_line_3>
            <postcode>test 2</postcode> 
            <tel_no>test 2</tel_no> 
            <fax_no>test 2</fax_no> 
            <email_address>test 2</email_address> 
            <website>test 2</website> 
        </Customer>
        <Customer>
            <address_name>test 3</address_name> 
            <address_line_1>test 3</address_line_1> 
            <address_line_2>test 3</address_line_2> 
            <address_line_3>test 3</address_line_3>
            <postcode>test 3</postcode> 
            <tel_no>test 3</tel_no> 
            <fax_no>test 3</fax_no> 
            <email_address>test 3</email_address> 
            <website>test 3</website> 
        </Customer>
    </Customers>
</ds>

To read this file:

Insert this two uses: XMLDoc, XMLIntf;

Here is a procedure to read your XML file.

procedure TForm1.btnReadXmlFileClick(Sender: TObject);
var
  XmlFile : TXMLDocument;
  MainNode, CustomerNode : IXMLNode;
  i : Integer;
  XMLPath : string;
begin
  XMLPath := 'Z:\Temp\xmlToRead.xml'; //example of path
  XmlFile :=  TXMLDocument.Create(Application);
  try
    XmlFile.LoadFromFile(XMLPath);
    XmlFile.Active := True;
    MainNode := XmlFile.DocumentElement;

    for i:=0 to MainNode.ChildNodes['Customers'].ChildNodes.Count-1 do
    begin
      CustomerNode := MainNode.ChildNodes['Customers'].ChildNodes[i];
      //Here you can get any imformation
      ShowMessage(CustomerNode.ChildNodes['address_name'].Text);
      ShowMessage(CustomerNode.ChildNodes['address_line_1'].Text);
    end;
  finally
    FreeAndNil(XmlFile);
  end;
end;
like image 53
Leo Melo Avatar answered Dec 08 '22 10:12

Leo Melo


Depending on your Delphi SKU, you can do this using Delphi components very easily if it comes with the XMLMapper utility (in Delphi's Bin directory).

Create a new project containing

  • a TClientDataSet
  • a TDatasource
  • a TDbGrid

Connect the datasource to the CDS and the grid to the datasource;

  • add an XMLTransformerProvider

Set the ProviderName of the CDS to the name of the XMLTransformerProvider

In the FormCreate event, open the CDS.

Save the project.

Then, after correcting your XML file as I mentioned in my comment, load it into Delphi's XMLMapper.

In XML Mapper,

  • Select the DocumentView tab of the LH, Document pane

  • Double-click each of the nodes address_name .. website in turn

  • Click Create | DataPacket from XML in the menu

  • Click the Create and Test Transformation button on the Mapping tab of the central, Transformation pane.

  • From the menu, go to File | Save | Transformation and save your .Xtr file.

Back in your Delphi project, point the XMLDataFile property of the XMLTransformProvider at your XML file, & the TransformationFile of its TransformRead zub-component at your .Xtr file.

Compile & run your project.

The TFields created in your CDS have types and sizes determined by the .Xtr file generated by XMLMapper. You can fine-tune these as follows:

In XMLMapper:

  • Select the Node Properties tab of the Transformation pane.

  • In the Document View tab of the Document pane, click one of the data nodes-

  • You can then set its Data Type and Max Length on the Node Propertyies tab.

like image 21
MartynA Avatar answered Dec 08 '22 11:12

MartynA