Last updated on Feb, 12th 2011

Xml Model Binding
News
Presentation
Use
License
Download
History
Evolutions
Contact

News

July, 16th 2010 Version 0.3. Improvements and bug corrections.
May, 17th 2010 Version 0.2. Still a preliminary release.
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 {
    public boolean xml2model(java.io.File J_XmlFile, TopClass top) ... {
        ...
        Xml2Model_T_TopNode(xml_CurrentNodeid4490640, top);
        ...
    }
    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 (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 (xml_CurrentNodeid4489376.getNodeName().equals("bar")) {
                top.setBar(new Integer(xml_CurrentNodeid4489376.getTextContent()));
            }
            xml_CurrentNodeid4489376 = xml_FocusNodeid4489376;
        }
    }
    ...

    public boolean model2xml(java.io.File J_XmlFile, TopClass top) ... {
        ...
        Model2Xml_T_TopNode(xml_CurrentNodeid4490640, top);
        ...
    }
    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
July, 16th 2010 xmb_0.3.tgz Improvements and bug corrections.
May, 17th 2010 xmb_0.2.tgz Still a preliminary release.
Feb, 15th 2010 xmb_0.1.tgz Preliminary release.

History

date version Description
July, 16th 2010 0.3 Improvements:
  • xmb2java.sh script options improvement. Deletion of the DOM and SAX script versions
Bug corrections:
  • Bug(major)! Null pointer exception when reading an XML file containing special <?nodes?>
  • Bug(major)! Xml2Model pre-code and post-code generation errors when dealing with choices or sequences instead of elements >> xml2model code generation simplification
May, 17th 2010 0.2 Still a preliminary release:
  • Exception not caught anymore to let the user know when something has gone wrong
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 .