Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Preserve the sequence of xml elements in the generated SOAP Envelope

by chanakya (Friar)
on Apr 13, 2005 at 11:29 UTC ( [id://447329]=perlquestion: print w/replies, xml ) Need Help??

chanakya has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
I'm generating and sending a SOAP envelope to the server, but when the client
is executed I see that the XML elements in the generated SOAP envelope are not
in order as I expect.

I was expecting the XML elements something in this order:
<?xml version="1.0" encoding="utf-8"?> <MessageHeader> <AgreementId>urn:xxx-xxx:::</AgreementId> <ConversationId>20050904-13:30:03.469-8572</ConversationId> <Sequence> <Id>What id is this?</Id> <Number>123</Number> <Total>6</Total> </Sequence> <Service> <type>service.type. what servicetype is this?</type> </Service> <Action>Problem Submit</Action> <MessageData> <MessageId>20050904-13:30:03.469-8572@...</MessageId> <Timestamp>2005-0409T12:10:42</Timestamp> <RefToMessageId>Which reference is this?</RefToMessageId> <TimeToLive>2005-0409T12:10:42</TimeToLive> </MessageData> <Description>This is the message header</Description> <ErrorList> <Error> <Description> Error Description</Description> <codeContent>code content</codeContent> <errorCode>Error Code</errorCode> <severity>severity.type</severity> <location>error location</location> </Error> <highestSeverity>severity.type</highestSeverity> </ErrorList> <id>which id is this?</id> </MessageHeader>
But the generated SOAP envelope comes out as below:
<?xml version="1.0" encoding="UTF-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <GetbyName xmlns="urn:Delivery"> <MessageHeader xmlns="urn:MsgHeader"> <MessageData> <Timestamp>2005-04-08T14:29:37</Timestamp> <RefToMessageId>Which reference is this</RefToMessageId> <TimeToLive>2005-04-08T14:29:37</TimeToLive> <MessageId>20050904-13:30:03.469-8572@...</MessageId> </MessageData> <ErrorList> <Error> <location>error location</location> <errorCode>Error Code</errorCode> <codeContent>code content</codeContent> <severity>severity.type</severity> <Description>Error Description</Description> </Error> </ErrorList> <Service> <type>service.type. What servicetype is this?</type> </Service> <highestSeverity>severity.type</highestSeverity> <AgreementId>urn:xxx-xxxx:::</AgreementId> <Action>Problem Submittal</Action> <id>Which id is this</id> <Sequence> <Total>6</Total> <Number>123</Number> <Id>What id is this</Id> </Sequence> </MessageHeader> </GetbyName> </soap:Body> </soap:Envelope>
How do I preserve the order of XML elements in the generated SOAP envelope (both on client/server side). like
<MessageHeader> <AgreementId>... <ConversationId>... <Sequence> <Id>... <Number> ...
Many thanks

Replies are listed 'Best First'.
Re: Preserve the sequence of xml elements in the generated SOAP Envelope
by Joost (Canon) on Apr 13, 2005 at 12:17 UTC
      Joost,
      Here's the client code, where the messages are generated.
      #!/usr/local/bin/perl #CLIENT use strict; use warnings; use SOAP::Lite +trace => qw(debug); use MIME::Base64; my $HOST = "http://localhost/perl/soap-dbi.pl"; my $NS = "urn:Delivery"; my $MsgHdr = "urn:MsgHeader"; #Data to be added to the request my (@data) = ( #Common MessageHeader SOAP::Data->name(MessageHeader => { ( AgreementId => "urn +:xxx-xxx:::" ), ( ConversationId => " +20050904-13:30:03.469-857" ), ( Sequence => { Id => +"What id is this", Number + => "123", Total +=> "6" } ), ( Service => { type => + "service.type. What servicetype is this?" } ), ( Action => "Problem +Submit" ), ( MessageData => { Mes +sageId => "20050904-13:30:03.469-8572\@xxx.xxx.xxx", Tim +estamp => SOAP::Utils::format_datetime(localtime), Ref +ToMessageId => "Which reference is this", Tim +eToLive => SOAP::Utils::format_datetime(localtime) } ), ( Description => "Thi +s is the message header" ), ( ErrorList => { Error => { + Description => "Error Description", + codeContent => "code + content", + errorCode => "Error Code", + severity => "seve +rity.type", + location => "error location" + } } ), ( highestSeverity => +"severity.type" ), ( id => "Which id is +this" ) } )->uri($MsgHdr)->prefix(''), ); #Enable fault management globally use SOAP::Lite on_fault => sub { my($soap, $res) = @_; eval { die ref $res ? $res->faultstring : $soap->transport->stat +us }; return ref $res ? $res : new SOAP::SOM; }; #print $soap->fault ? $soap->faultstring. "\n" : $soap->result; my $soap = SOAP::Lite -> proxy($HOST) -> uri($NS); #my $res = $soap->byName(@headers,@data); my $res = $soap->GetbyName(@data); #print $soap->retrieveDocument->result; print $res->fault ? $res->faultstring. "\n" : $res->result;
      Many thanks

        The reason that the elements are not coming out in the order that you think you put them it, is that you are passing a hash reference as the second argument to SOAP::Data->name(), the elements in a hash are effectively in a random order which you cannot control. If you want more control over the structure of the generated XML you will need to explicitly create every element with SOAP::Data - you can find an example in Re: SOAP Beginner ... I hate to impose.

        As to the question of why the elements need to be in a particular order, in general with XML this shouldn't matter but I have seen some XML parsers get upset about this when validating against a DTD, but you generally aren't doing this in a Web Service. If you (or the owner of the web service) care at all about interoperability, you will fix the server so that it behaves in an interoperable manner rather than hacking the client to work around it's quirks.

        /J\

Re: Preserve the sequence of xml elements in the generated SOAP Envelope
by Anonymous Monk on Apr 13, 2005 at 13:41 UTC
    order doesn't matter

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://447329]
Approved by jbrugger
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-03-29 00:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found