2

I attempt to create a xml document to access a webservice. I'm having trouble getting the result xml beeing like i want it to. :-) This is my delphicode to create this document.

  xmlQuery.Active := true;
  xmlQuery.Version := '1.0';
  xmlQuery.Encoding := 'UTF-8';
  lEnvelope := xmlQuery.AddChild('soap:Envelope');
  lEnvelope.Attributes['xmlns:soap'] := 'http://schemas.xmlsoap.org/soap/envelope/';
  lHeader := lEnvelope.AddChild('soap:Header');
  lBruker := lHeader.AddChild('brukersesjon:Brukersesjon');
  lValue := lBruker.AddChild('distribusjonskanal');
  lValue.Text := 'PTP';
  lValue := lBruker.AddChild('systemnavn');
  lValue.Text := 'infotorgEG';

The resulting xml looks like this.

<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Header>
      <brukersesjon:Brukersesjon>
           <brukersesjon:distribusjonskanal>PTP</brukersesjon:distribusjonskanal>
           <brukersesjon:systemnavn>infotorgEG</brukersesjon:systemnavn>
      </brukersesjon:Brukersesjon>
    </soap:Header>
</soap:Envelope>

I want it to look like this.

<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Header>
       <brukersesjon:Brukersesjon>
           <distribusjonskanal>PTP</distribusjonskanal>
           <systemnavn>infotorgEG</systemnavn>
       </brukersesjon:Brukersesjon>
   </soap:Header>
</soap:Envelope>

I can't figure out what i'm doing wrong. Can anyone of you help me? Does anyone of you know of a sample/tutorial that creates a xml file complete with header and body?

Ken White
  • 123,280
  • 14
  • 225
  • 444
Sjubussen
  • 51
  • 1
  • 8
  • Hint: do not hardcode `soap:` - the colon-separated prefix is a namespace, and all XML document methods can insert this namespace for you if you use their version with an additional namespace argument. – mjn Dec 24 '14 at 07:51
  • Stop using TXMLDocument for consuming webservices. Use the WSDL in conjunction with THTTPRIO for these kind of tasks. – whosrdaddy Dec 24 '14 at 11:56
  • I have tried that but import of the wsdl failes. I have previously posted a question regarding that here on SO. – Sjubussen Dec 24 '14 at 14:14
  • Why don't you deal with that then? – David Heffernan Dec 24 '14 at 14:57

1 Answers1

1

Use the overloaded IXMLNode.AddChild that takes a second parameter for the NameSpaceURI:

xmlQuery.Active := true;
xmlQuery.Version := '1.0';
xmlQuery.Encoding := 'UTF-8';
lEnvelope := xmlQuery.AddChild('soap:Envelope');
lEnvelope.Attributes['xmlns:soap'] := 'http://schemas.xmlsoap.org/soap/envelope/';
lHeader := lEnvelope.AddChild('soap:Header');
lBruker := lHeader.AddChild('brukersesjon:Brukersesjon');
lValue := lBruker.AddChild('distribusjonskanal', '');  // <<<-- Here
lValue.Text := 'PTP';
lValue := lBruker.AddChild('systemnavn', '');          // <<<-- And here
lValue.Text := 'infotorgEG';

This produces the output you show as the desired output.

<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header><brukersesjon:Brukersesjon>
    <distribusjonskanal>PTP</distribusjonskanal>
    <systemnavn>infotorgEG</systemnavn>
  </brukersesjon:Brukersesjon>
  </soap:Header>
</soap:Envelope>
Ken White
  • 123,280
  • 14
  • 225
  • 444