Forum Discussion

Jens_Riis's avatar
Jens_Riis
Occasional Contributor
16 years ago

Creating a XML file based on a Schema

I have a need to create an XML file based on a schema (xsd) as the first step of a testcase. As there is no means of creating a DataSource Sink of type XML  I have absolutely no idea on how to get around doing this in SOAPUI, so any help would be appreciated.

 

3 Replies

  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    Hi Jens,


    What you're looking for is not trivial but not impossible either. 

    You can write a Groovy script that gradually creates an XML document out of an XSD file and uses SampleXmlUtil support class to create a sample for the desired element that is defined in your Xml Schema.

    Here's how it would look like:

    import org.apache.xmlbeans.*
    import javax.xml.namespace.*


    // provide file path to XSD file
    def xsdObject = XmlObject.Factory.parse(new File(xsdFilename))
    def xmlBeans = new XmlBeans()
    def loader = xmlBeans.loadXsd( xsdObject )

    namespace = 'urn:yahoo:api' // replace
    elementName = 'Error'  // replace
    def qName = new QName(namespace, elementName)
    def el = loader.findElement(qName)

    if (el != null) {
    log.info("Created an element with qualified name: " + qName)
    def xml = SampleXmlUtil.createSampleForElement( el )
    log.info("xml document: " + xml)
    } else {
    log.warn("Could not create a non-existing element with qualified name: " + qName);
    }


    I hope this will push you in the right direction.


    Cheers!
    /Nenad Nikolic a.k.a. Shonzilla
  • Jens_Riis's avatar
    Jens_Riis
    Occasional Contributor
    Hi Nenad

    Yes i get the idea and would be able to get ahead based on your example, if I just could make it run.

    The invokation of  SampleXmlUtil.createSampleForElement fails  with

    Tue Mar 10 12:46:24 CET 2009:ERROR:groovy.lang.MissingMethodException: No signature of method: static com.eviware.soapui.impl.wsdl.support.xsd.SampleXmlUtil.createSampleForElement() is applicable for argument types: (org.apache.xmlbeans.impl.schema.SchemaGlobalElementImpl) values: {org.apache.xmlbeans.impl.schema.SchemaGlobalElementImpl@12b96ea}




    I have tried to cast the el (being of class type org.apache.xmlbeans.impl.schema.SchemaGlobalElementImpl, while the createSampleForElement method requires an argument of type org.apache.xmlbeans.SchemaGlobalElement)

    variable in various ways but it does not change anything unfortunately
  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    Hello!

    Which version of SoapUI are you running?

    If possible, please attach your .xsd file and the full script you are using. If you do not wish to make it public, you can email it to us at support@eviware.com

    Regards,
    Dain
    eviware.com