Read XML with XMLDocument in Delphi

Question:

I have this xml, I don't know how to read and get the values. can anybody 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>

Answer:

Delphi, since ancient versions, provides two units:

XMLIntf.pas and XMLDoc.pas

To use it, just declare an IXMLDocument variable and use its methods, they 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 an IXMLDocument variable instead of TXMLDocument for the compiler to manage memory automatically. That way you don't need to destroy the component by calling the .Free method

Scroll to Top