0
votes

I am using Mule ESB to convert an input JSON to an XML to be sent to a SOAP webservice. I am able to build the structure and add namespace to the root element but my XML output that my SOAP needs is quite precise and asks for prefixes and namespaces in elements.

Here is my Groovy Script :

def xml = new StringWriter().with { w -> new groovy.xml.MarkupBuilder(w).with {
        SaveContact('http://tempuri.org/') {
            contact() {  
                Address('http://schemas.datacontract.org/2004/07/PivotalService.Entities')   
                {
                    Address1(payload.Address1)
                    Address2(payload.Address2)
                    Address3(payload.Address3)
                    City(payload.City)
                    Country(payload.Country)
                    Fax(payload.Fax)
                    Mobile(payload.Mobile)
                    Phone(payload.Phone)
                    PhoneExtension(payload.PhoneExtension)
                    State(payload.State)
                    Workphone(payload.Workphone)
                    WorkphoneExtension(payload.WorkphoneExtension)
                    Zip(payload.Zip)
                }
                ContactId(payload.ContactId)
                Email(payload.Email)
                Firstname(payload.Firstname)
                Gender(payload.Gender)
                Language(payload.Language)
                Lastname(payload.Lastname)
                PrestashopId(payload.PrestashopId)
            }
            Consent(payload.Consent)
        }
    }
    w.toString()
}

Here is an example of the output it gives me :

<SaveContact xmlns='http://tempuri.org/'>
  <contact>
    <Address xmlns='http://schemas.datacontract.org/2004/07/PivotalService.Entities'>
      <Address1>2, test street</Address1>
      <Address2>Appartement 10</Address2>
      <Address3 />
      <City>Montreal</City>
      <Country>Canada</Country>
      <Fax />
      <Mobile>+1(418)123-1234</Mobile>
      <Phone>+1(418)123-1234</Phone>
      <PhoneExtension />
      <State>Québec</State>
      <Workphone />
      <WorkphoneExtension />
      <Zip>A1A1A1</Zip>
    </Address>
    <ContactId />
    <Email></Email>
    <Firstname>fooo</Firstname>
    <Gender>1</Gender>
    <Language>3</Language>
    <Lastname>baaarrr</Lastname>
    <PrestashopId>172</PrestashopId>
  </contact>
  <Consent></Consent>
</SaveContact>

As you can see just the namespace doesn't even work when adding to an element.

Here is the XML I want (I am not sure if the prefixes (ns0..etc) are required):

<?xml version="1.0" encoding="ISO-8859-1"?>
<ns0:SaveContact xmlns:ns0="http://tempuri.org/">
  <ns0:contact>
    <ns1:Address xmlns:ns1="http://schemas.datacontract.org/2004/07/PivotalService.Entities">
      <ns1:Address1>2, test street</ns1:Address1>
      <ns1:Address2>Appartement 10</ns1:Address2>
      <ns1:City>Montreal</ns1:City>
      <ns1:Country>Canada</ns1:Country>
      <ns1:Mobile>+1(418)123-1234</ns1:Mobile>
      <ns1:Phone>+1(418)123-1234</ns1:Phone>
      <ns1:State>Quebec</ns1:State>
      <ns1:WorkPhone>+1(418)123-1234</ns1:WorkPhone>
      <ns1:Zip>A1A1A1</ns1:Zip>
    </ns1:Address>
    <ns1:Email xmlns:ns1="http://schemas.datacontract.org/2004/07/PivotalService.Entities">null</ns1:Email>
    <ns1:Firstname xmlns:ns1="http://schemas.datacontract.org/2004/07/PivotalService.Entities">fooo</ns1:Firstname>
    <ns1:Gender xmlns:ns1="http://schemas.datacontract.org/2004/07/PivotalService.Entities">1</ns1:Gender>
    <ns1:Language xmlns:ns1="http://schemas.datacontract.org/2004/07/PivotalService.Entities">3</ns1:Language>
    <ns1:Lastname xmlns:ns1="http://schemas.datacontract.org/2004/07/PivotalService.Entities">baaarrr</ns1:Lastname>
    <ns1:PrestashopId xmlns:ns1="http://schemas.datacontract.org/2004/07/PivotalService.Entities">172</ns1:PrestashopId>
  </ns0:contact>
</ns0:SaveContact>

Thank you for helping it is appreciated.

1

1 Answers

1
votes

Below implementation can be useful:

def entityNs = [
    'xmlns:ns1': "http://schemas.datacontract.org/2004/07/PivotalService.Entities"
]

Continue with what you have above, with changes like:

....
"ns1:Email"(entityNs, 'foo')
"ns1:Firstname"(entityNs, 'Bar')
....