Listing 1

// Sample arguments
String xsd = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
	+ "<schema xmlns=\"http://www.w3.org/2001/XMLSchema\" "
	+ "  xmlns:test=\"http://mytestnamespace\" "
	+ "  targetNamespace=\"http://mytestnamespace\">"
	+ "  <element name=\"TestElementX\" type=\"test:TestElementXType\"/>"
	+ "  <complexType name=\"TestElementXType\">"
	+ "    <sequence>"
	+ "      <element name=\"SubElement1\" type=\"double\"/>"
	+ "      <element name=\"SubElement2\" type=\"string\"/>"
	+ "    </sequence>"
	+ "  </complexType>"
	+ "</schema>";

String rootType = "TestElementXType";
String namespace = "http://mytestnamespace";

// Create helper context with extensible namespaces
// With extensible namespaces you can define two different schemas with same namespace
HelperContext context = SDOUtil.createHelperContext(true);

// Define and cache the types of the xsd
context.getXSDHelper().define(xsd);

// Let us generate our SDO data object representing our root type
Type type = context.getTypeHelper().getType(namespace, rootType);
DataObject rootDataObject = context.getDataFactory().create(type);

Listing 2

Type type = rootDataObject.getType();
List containedProps = type.getProperties();
for (Iterator i = containedProps.iterator(); i.hasNext();) {
  Property property = (Property) i.next();
  handleProperty(helperContext, rootXmlElement, rootDataObject, property);
}


Handling Each Property
private void handleProperty(HelperContext helperContext,
  final CompositeXmlElement parent, final DataObject dataObject,
  final Property property) {

  if (property.isContainment() && !property.getType().isDataType()) {

    List<DataObject> childObjects;
    if (dataObject.isSet(property)) {
      childObjects = new ArrayList<DataObject>(1);
      childObjects.add(dataObject.getDataObject(property));
    } else {
      // if we are traversing hierarchy without an xml data instance
      // we create an empty placeholder
      childObjects = new ArrayList<DataObject>(1);
      childObjects.add(dataObject.createDataObject(property));
    }
    for (DataObject childObject : childObjects) {
      CompositeXmlElement child = new CompositeXmlElement(parent, childObject, property);
      parent.add(child);
      Type type = property.getType();
      List containedProps = type.getProperties();
      for (Iterator i = containedProps.iterator(); i.hasNext();) {
        Property childProperty = (Property) i.next();
        handleProperty(helperContext, child, childObject,
            childProperty);
      }
    }
  } else {
    // Leaf xml element
    LeafXmlElement leafRoot = null;
    Object value = null;
    if (dataObject.isSet(property)) {
      value = dataObject.get(property);
    }
    leafRoot = generateLeaf(helperContext, parent, dataObject, property, value);
    parent.add(leafRoot);
  }
}

Generating the leafs:
private LeafXmlElement generateLeaf(
     final HelperContext helperContext, CompositeXmlElement parent,
     final DataObject dataObject, final Property property, Object value) {

  LeafXmlElement newLeaf = new LeafXmlElement(parent, dataObject, property);
  UIInput input = generateUIInput(dataObject, property, value, newLeaf);
  newLeaf.setUiInput(input);
  if (input != null) {
    UIOutput output = generateUIOutput(property.getName());
    newLeaf.setLabel(output);
  }
  return newLeaf;
}

Generating UIInput (Input Field on the Web Form):
private UIInput generateUIInput(final DataObject dataObject,
     final Property property, final Object value, LeafXmlElement leaf) {

  UIInput input;

  String instanceClass = property.getType().getInstanceClass().getSimpleName();

  // Numbers
  if (instanceClass.equals("BigInteger") || instanceClass.equals("int") ||     
      instanceClass.equals("Double") || instanceClass.equals("double") ||
      instanceClass.equals("Float") || instanceClass.equals("float") ||
     instanceClass.equals("Long") || instanceClass.equals("long")) {

    input = (UIInput) FacesContext.getCurrentInstance().getApplication()
.createComponent(HtmlInputText.COMPONENT_TYPE);
    ((HtmlInputText) input).setMaxlength(10);
    ((HtmlInputText) input).setConverter(
          FacesContext.getCurrentInstance().getApplication()
          .createConverter(NumberConverter.CONVERTER_ID));
  } else if (instanceClass.equals("Boolean") || instanceClass.equals("boolean")) {
    input = (UIInput) FacesContext.getCurrentInstance().getApplication()
	      .createComponent(HtmlSelectBooleanCheckbox.COMPONENT_TYPE);
  } else if (instanceClass.equals("String")) {
    input = (UIInput) FacesContext.getCurrentInstance().getApplication()
	      .createComponent(HtmlInputTextarea.COMPONENT_TYPE);
  } else {
    throw new RuntimeException("Unknown Xml type: " + instanceClass
        + ". Unable to generate ui");
  }

  input.setId("_" + UUID.randomUUID().toString());
  input.setRequired(SDOUtil.isRequired(property));
  input.setValue(value);

  return input;
}

Generating UIOutput (Label on the Web Form):
private UIOutput generateUIOutput(String label) {

  HtmlOutputText output = new HtmlOutputText();
  output.setId("_" + UUID.randomUUID().toString());
  output.setValue(label);

  return output;
}