Read XML with Xmldocument in Delphi

Asked

Viewed 7,876 times

3

I have this xml, I don’t know how to read and get the values. Someone could help me?

<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <soapenv:Body>
  <ns1:WsobterdadoscolaboradorResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://DefaultNamespace">
   <WsobterdadoscolaboradorReturn href="#id0"/>
  </ns1:WsobterdadoscolaboradorResponse>
  <multiRef id="id0" soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns2:Map" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns2="http://xml.apache.org/xml-soap">
    <item>
     <key xsi:type="soapenc:string">empresas</key>
     <value xsi:type="soapenc:string">1,2</value>
    </item>
    <item>
     <key xsi:type="soapenc:string">id</key>
     <value xsi:type="soapenc:string">2</value>
    </item>
    <item>
     <key xsi:type="soapenc:string">login</key>
     <value xsi:type="soapenc:string">andre</value>
    </item>
    <item>
     <key xsi:type="soapenc:string">nome</key>
     <value xsi:type="soapenc:string">Andr&#xE9; Fernando Rodrigues Lima</value>
    </item>
    <item>
     <key xsi:type="soapenc:string">senha</key>
     <value xsi:type="soapenc:string">376255bd8b21e8a66734694f5907a1d5</value>
     </item>
  </multiRef>
 </soapenv:Body>
</soapenv:Envelope>

3 answers

5

Delphi, since older versions, offers two Units:

Xmlintf.pas and Xmldoc.pas

To use just declare an Ixmldocument variable and use its methods, are very intuitive:

var
  XML: IXMLDocument;
begin
  XML:= TXMLDocument.Create(''); //Ou nome de um arquivo, se você for alterá-lo posteriormente
  XML.LoadFromFile('Arquivo.xml');
  XML.SaveToFile('Arquivo.xml'); //Ou '' se você especificar esse mesmo nome na construção do objeto
end; 

I used a variable IXMLDocument instead of TXMLDocument for the compiler to manage the memory automatically. So you do not need to destroy the component by calling the method .Free

2

The simplest way is: Inside Delphi has a tool (tools/ XML Mapper), basically you will open your xml that will be loaded in the Document window, after that, right mouse side on it, Select All is after Create Datapacket from XML. It will generate a final Todp.xtr. file in your application you will use the Xmltransformprovider + Clientdataset linked to it. In Xmltransformprovider you have 2 properties, one will point to your xml file and the other to the Todp.xtr file. After that you can load the fields in your Clientdataset as if it were a normal table.

1

Hello, I suggest using a Clientdataset.

To load the data try:

ClientDataSet1.Close;
ClientDataSet1.LoadFromFile(´data.xml´);
ClientDataSet1.Open;

To save the data is quite similar:

ClientDataSet1.SaveToFile(ClientDataSet1.FileName,dfXML);

PS: Filename is the Clientdataset property where you enter the location of your XML, there in Loadfromfile you can also use Filename instead of writing the file name!

Browser other questions tagged

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