Last updated on Feb, 15th 2010

Xml Model Binding
News
Presentation
Use
License
Download
History
Evolutions
Contact

News

Feb, 15th 2010 Version 0.1. Preliminary release.

Presentation

Considering that XML input/output code usually looks like an XSD schema, Xml Model Binding is an XSD extension proposition for easily developping XML input/output code.

The sample below highlights the Xml Model Binding syntax extension within the Xml Schema syntax:
<?xml version="1.0" encoding="iso-8859-1"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:xmb="http://alexis.royer.free.fr/XmlModelBinding"
            elementFormDefault="unqualified" attributeFormDefault="unqualified">

    <xsd:element name="top" type="T_TopNode">
        <xmb:param>TopClass top</xmb:param>
        <xmb:call><xmb:param>top</xmb:param></xmb:call>
    </xsd:element>

    <xsd:complexType name="T_TopNode">
        <xmb:param>TopClass top</xmb:param>

        <xsd:sequence>
            <xsd:element name="bar" type="xsd:int">
                <xmb:xml2model><xmb:code>top.setBar(new Integer(<xmb:value/>));</xmb:code></xmb:xml2model>
                <xmb:model2xml><xmb:value>top.getBar()</xmb:value></xmb:model2xml>
            </xsd:element>
        </xsd:sequence>

        <xsd:attribute name="foo" type="xsd:string">
            <xmb:xml2model><xmb:code>top.setFoo(<xmb:value/>);</xmb:code></xmb:xml2model>
            <xmb:model2xml><xmb:value>top.getFoo()</xmb:value></xmb:model2xml>
        </xsd:attribute>
    </xsd:complexType>
</xsd:schema>

And here is the java code automatically generated below. The code directly derived from the Xml Model Binding tags is highlighted:
class XmlTop {
    void xml2model(java.io.File J_XmlFile, TopClass top) ... {
        try {
            ...
            Xml2Model_T_TopNode(xml_CurrentNodeid4489872, top);
        } catch (Exception j_Exception) {
            ...
        }
    }
    void Xml2Model_T_TopNode(org.w3c.dom.Node xml_FocusNodeid4489376, TopClass top) {
        org.w3c.dom.Node xml_CurrentNodeid4489376 = xml_FocusNodeid4489376;

        for (   int i_Attrid4489376 = 0;
                i_Attrid4489376 < xml_FocusNodeid4489376.getAttributes().getLength();
                i_Attrid4489376 ++) {
            xml_CurrentNodeid4489376 = xml_FocusNodeid4489376.getAttributes().item(i_Attrid4489376);
            if (false) {
            } else if (xml_CurrentNodeid4489376.getNodeName().equals("foo")) {
                top.setFoo(xml_CurrentNodeid4489376.getTextContent());
            }
            xml_CurrentNodeid4489376 = xml_FocusNodeid4489376;
        }

        for (   int i_Childid4489376 = 0;
                i_Childid4489376 < xml_FocusNodeid4489376.getChildNodes().getLength();
                i_Childid4489376 ++) {
            xml_CurrentNodeid4489376 = xml_FocusNodeid4489376.getChildNodes().item(i_Childid4489376);
            if (xml_CurrentNodeid4489376.getNodeType() == org.w3c.dom.Node.TEXT_NODE) continue;
            if (false) {
            } else if (xml_CurrentNodeid4489376.getNodeName().equals("bar")) {
                top.setBar(new Integer(xml_CurrentNodeid4489376.getTextContent()));
            }
            xml_CurrentNodeid4489376 = xml_FocusNodeid4489376;
        }
    }
    ...

    void model2xml(java.io.File J_XmlFile, TopClass top) ... {
        try {
            ...
            Model2Xml_T_TopNode(xml_CurrentNodeid4489872, top);
            ...
        } catch (Exception j_Exception) {
            ...
        }
    }
    void Model2Xml_T_TopNode(org.w3c.dom.Node xml_FocusNodeid4489376, TopClass top) {
        org.w3c.dom.Node xml_CurrentNodeid4489376 = xml_FocusNodeid4489376;

        if (true) {
            xml_CurrentNodeid4489376 = createAttribute(xml_FocusNodeid4489376, "foo");
            xml_CurrentNodeid4489376.setTextContent(String.valueOf(top.getFoo()));
        }
        xml_CurrentNodeid4489376 = xml_FocusNodeid4489376;

        if (true) {
            xml_CurrentNodeid4489376 = createNode(xml_FocusNodeid4489376, "bar");
            xml_CurrentNodeid4489376.setTextContent(String.valueOf(top.getBar()));
        }
        xml_CurrentNodeid4489376 = xml_FocusNodeid4489376;
    }
    ...
}

Use

Only Java available for the moment:

  1. Write the XSD schema file describing your XML format
  2. Extend it with <xmb/> tags, giving the user code bindings with your data model (call getters and setters)
  3. Use the xmb2java tool set to produce java code

Sorry there is no user-guide nor XMB schema yet. Please check the Watch project for examples of it.

License

This software, written by Alexis Royer, is under BSD-like license.

This kind of license is compatible with commercial use, modification and redistribution, under the terms of the license.

Download

date archive Description
Feb, 15th 2010 xmb_0.1.tgz Preliminary release.

History

date version Description
Feb, 15th 2010 0.1 Preliminary release:
  • Only Java support
  • Only full DOM support, partial SAX support (XML reading)

Evolutions

Still many things to do... User-guide, XMB schema, full SAX support.

Don't hesitate to for any suggestion.

Contact

Any question? Found a bug (maybe many)? Feel free to .