How to generate sample XML documents from their DTD or XSD?

前端 未结 19 1469
梦毁少年i
梦毁少年i 2020-11-22 10:11

We are developing an application that involves a substantial amount of XML transformations. We do not have any proper input test data per se, only DTD or XSD files. We\'d li

相关标签:
19条回答
  • 2020-11-22 11:03

    Seems like nobody was able to answer the question so far :)

    I use EclipseLink's MOXy to dynamically generate binding classes and then recursively go through the bound types. It is somewhat heavy, but it allows XPath value injection once the object tree is instantiated:

    InputStream in = new FileInputStream(PATH_TO_XSD);
    DynamicJAXBContext jaxbContext = 
                DynamicJAXBContextFactory.createContextFromXSD(in, null, Thread.currentThread().getContextClassLoader(), null);
    DynamicType rootType = jaxbContext.getDynamicType(YOUR_ROOT_TYPE);
    DynamicEntity root = rootType.newDynamicEntity();
    traverseProps(jaxbContext, root, rootType, 0);
    

    TraverseProps is pretty simple recursive method:

    private void traverseProps(DynamicJAXBContext c, DynamicEntity e, DynamicType t, int level) throws DynamicException, InstantiationException, IllegalAccessException{
            if (t!=null) {
                logger.info(indent(level) + "type [" + t.getName() + "] of class [" + t.getClassName() + "] has " + t.getNumberOfProperties() + " props");
                for (String pName:t.getPropertiesNames()){
                    Class<?> clazz = t.getPropertyType(pName);
                    logger.info(indent(level) + "prop [" + pName + "] in type: " + clazz);
                    //logger.info("prop [" + pName + "] in entity: " + e.get(pName));
    
                    if (clazz==null){
                        // need to create an instance of object
                        String updatedClassName = pName.substring(0, 1).toUpperCase() + pName.substring(1);
                        logger.info(indent(level) + "Creating new type instance for " + pName + " using following class name: " + updatedClassName );
                        DynamicType child = c.getDynamicType("generated." + updatedClassName);
                        DynamicEntity childEntity = child.newDynamicEntity();
                        e.set(pName, childEntity);
                        traverseProps(c, childEntity, child, level+1);
                    } else {
                        // just set empty value
                        e.set(pName, clazz.newInstance());
                    }
                }
            } else {
                logger.warn("type is null");
            }
        }
    

    Converting everything to XML is pretty easy:

    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(root, System.out);
    
    0 讨论(0)
  • 2020-11-22 11:03

    XMLBlueprint 7.5 can do the following: - generate sample xml from dtd - generate sample xml from relax ng schema - generate sample xml from xml schema

    0 讨论(0)
  • 2020-11-22 11:04

    I think Oxygen (http://www.oxygenxml.com/) does it as well, but that's another commerical product. It's a nice one, though... I'd strongly recommend it for anyone doing a lot of XML work. It comes in a nice Eclipse plugin, too.

    I do believe there is a free, fully-featured 30 day trial.

    0 讨论(0)
  • 2020-11-22 11:08

    The camprocessor available on Sourceforge.net will do xml test case generation for any XSD. There is a tutorial available to show you how to generate your own test examples - including using content hints to ensure realistic examples, not just random junk ones.

    The tutorial is available here: http://www.oasis-open.org/committees/download.php/29661/XSD%20and%20jCAM%20tutorial.pdf

    And more information on the tool - which is using the OASIS Content Assembly Mechanism (CAM) standard to refactor your XSD into a more XSLT friendly structure - can be found from the resource website - http://www.jcam.org.uk

    Enjoy, DW

    0 讨论(0)
  • 2020-11-22 11:09

    For Intellij Idea users:

    Have a look at Tools -> XML Actions

    enter image description here

    Seems to work very well (as far as I have tested).

    Edit:

    As mentioned by @naXa, you can now also right-click on the XSD file and click "Generate XML Document from XSD Schema..."

    0 讨论(0)
  • 2020-11-22 11:14

    XMLSpy does that for you, although that's not free...

    I believe that Liquid Xml Studio does it for you and is free, but I have not personally used it to create test data.

    0 讨论(0)
提交回复
热议问题