Home > Software design >  Generating Form (and XML result) using XSD(s)
Generating Form (and XML result) using XSD(s)

Time:02-03

This post JSON shows how to create a JSON from JSON schema using Java Metawidget. I want to do exactly the same for an XSD (and if necessary XML).

The metawidget documentation says quite a bit about XMLInspectors, but I have not been able to get a result after some trial and error (and i did not find any examples). Do any of you have any idea if and how the whole thing works with Metawidget? If this should not go with Metawidget, I would also be open for alternatives to Metawidget. I want to create the form dynamically. This means that during runtime a form should be created from passed xsd(s), from which an XML is then generated (in java).

CodePudding user response:

Java Metawidget's internal inspection format is already XML (albeit a String of XML, to avoid dependencies). See section 2.2.6 in the documentation:

<inspection-result xmlns="http://metawidget.org/inspection-result" version="1.0">
    <entity type="com.myapp.Person">
        <property name="name" required="true"/>
        <property name="age" minimum-value="0"/>
    </entity>
</inspection-result>

So you would need to create your own Inspector, that read your own XML format, and manipulated it into the format above. Then return that as a String.

See section 2.2.7 in the documentation. For inspecting XML files, BaseXmlInspector assists in opening and traversing through the XML, as well as merging multiple XML files into one (e.g. merging multiple Hibernate mapping files).

It also lets you work with parsed XML as Maps, which is a bit easier, and does the conversion for you. See:

protected Map<String, String> inspectProperty( Element toInspect ) {

    if ( !"field".equals( toInspect.getNodeName() ) )
        return null;

    Map<String, String> attributes = CollectionUtils.newHashMap();
    attributes.put( NAME, toInspect.getAttribute( getNameAttribute() ) );
    attributes.put( TYPE, toInspect.getAttribute( getTypeAttribute() ) );
    return attributes;
}

CodePudding user response:

Processing raw XSD documents in the general case is not easy, because there are so many features like named model groups, substitution groups, and types derived by extension that make it complex. If you do need to extract information from an XSD schema, I would recommend using a schema processor that gives you access to the compiled information via some API. Saxon, for example, can output the compiled schema as a .SCM file, which is much easier to process than the raw form.

  • Related