Service Virtualization

Using DevTest to create XML sample documents from XSD specifications

  • 1.  Using DevTest to create XML sample documents from XSD specifications

    Broadcom Employee
    Posted Feb 23, 2015 05:08 AM

    Hi all --

     

    I stumbled across a project that claims to generate sample XML files from an XSD specification.

    https://code.google.com/p/jlibs/wiki/XSInstance

     

    I downloaded the jlibs zip file, extracted it and copied all the JAR files from the lib folder into LISA_HOME/hotDeploy.

     

    I then hacked the programmatic sample a little to get it working in a beanshell step in DevTest Workstation:

    ——

    import jlibs.xml.xsd.XSParser;

     

    import org.apache.xerces.xs.*;

    //beanshell needs these two imports

    import javax.xml.namespace.QName;

    import javax.xml.transform.stream.StreamResult;

     

    //my variables

    projRoot = testExec.getStateValue("LISA_PROJ_ROOT");

    myXSD = projRoot + "/data/xsds/myXSD.xsd”; // This is where my XSD file lives

    myRootElement = “rootElement"; //Change this to the rootElement inside the XSD file that I want to use

    XSModel xsModel = new XSParser().parse(myXSD);

     

    //Create an instanceof XSInstance and configure various options

    import jlibs.xml.xsd.XSInstance;

    XSInstance xsInstance = new XSInstance();

    xsInstance.minimumElementsGenerated = 2;

    xsInstance.maximumElementsGenerated = 4;

    xsInstance.generateOptionalElements = Boolean.TRUE; // null means random

     

    //now genreate the sample xml as follows:

    import jlibs.xml.sax.XMLDocument;

    QName rootElement = new QName("", myRootElement);

    XMLDocument sampleXml = new XMLDocument(new StreamResult(System.out), true, 4, null);

    xsInstance.generate(xsModel, rootElement, sampleXml);

    ——

    If you open the “System Messages” panel at the bottom of the Workstation window (or tail the workstation.log file), you will see a sample XML message generated.

     

    This has the standard problem when generating sample documents that we have no idea which elements need to be combined for a business transaction, so the generated sample is probably useless … but DevTest performs the action fine

    Therefore, I haven't put any time into writing the XML document into a string, or saving it as a file. If you want to use it, you probably want to do that by changing StreamResult.

     

    If anyone finds a real use for this, where they can identify how to make actual usable sample XML messages from XSD specifications, please respond here.

     

    Rick