Read XML with XMLDocument in Delphi

3

I have this xml, I do not know how to read and get the values. Could someone 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>
    
asked by anonymous 16.10.2015 / 20:59

3 answers

3

Delphi, from the older versions, offers two units:

  

XMLIntf.pas and XMLDoc.pas

To use just declare an IXMLDocument variable and use its methods, they are quite 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've used a variable IXMLDocument instead of TXMLDocument for the compiler to automatically manage memory. So you do not need to destroy the component by calling the .Free

    
21.10.2015 / 12:31
2

The simplest way: 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-click 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 connected to it. In the XMLTransformProvider you have 2 properties, one you 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.

    
31.10.2015 / 22:17
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 very similar:

ClientDataSet1.SaveToFile(ClientDataSet1.FileName,dfXML);

PS: FileName is the property of the ClientDataSet where you inform the location of your XML, there in LoadFromFile you can also use FileName instead of writing the file name!

    
21.10.2015 / 00:43