The basis for what I'm doing is:

WebService <-> xml <-> xslt <-> xml <-> domain

I replaced the last part with iBatis. Xslt could still be applied to the xml from the webservice for further abstraction. The lazy part comes from being able to the load the object or arrays of objects from xml. Instead of doing it by hand, I used iBatis.

I was planning on doing what you suggested with XSLT into a domain object, but I couldn't find anything on how to do that. Can you point me towards an explanation?

Since the the XML is being passed to the map from the application, you could pass it through a validator first. I omitted that for the sake of brevity.

I also looked at DataSets, but that doesn't solve the problem of getting it into an object, or at least I couldn't anything that did it.

Is there any interest in creating a XML-mapper for iBatis? What I've done uses SQL Server to do the conversion but that wouldn't be needed if there was parser built in.

Brian

Nguyen, Tom wrote:
Interesting implementation; though I think it's a too much work.  It
seems more or equivalent to the amount of work for developing XSLT
transformation.
Not all XML are the same.  I would prefer to dynamically validate mine
through XML Schema; and use XSLT to transform it into a format that I
can deserialize into a domain object.
If I'm really lazy, I can load the data into the DataSet, if the data
schema works with a DataSet ...  ;)

But of course, with ibatis, one can re-use the mapping for different
datasources.  But the implementation would probably be easier with
something like XmlDbCommand like example in this article
(http://www.dotnetjohn.com/articles.aspx?articleid=188) which can
probably be plug directly into ibatisnet datasource.

Regards,
Tom Nguyen


-----Original Message-----
From: Brian Kierstead [mailto:[EMAIL PROTECTED] Sent: Friday, January 12, 2007 2:36 PM
To: [email protected]
Subject: Using iBatis to map XML to Objects

I've worked out a way to make iBatis do the dirty work of mapping xml into domain objects and thought it be of interest to the group. I've had a few instances where I have to turn xml data from some web service into objects and I don't like having to do all the work myself.

This is just for SQL Server, but I'm sure you can create the equivalent statements for other servers quite easily.

Brian

First the map:
<?xml version="1.0" encoding="UTF-8" ?>
<sqlMap ...>
    <resultMaps>
        <resultMap id="XmlTestResult" class="XmlTest">
            <result column="CustomerID" property="Id"
dbType="varchar(10)"/>
<result column="ContactName" property="Name" dbType="varchar(20)"/>
        </resultMap>
    </resultMaps>

    <statements>
        <select id="XmlTestBase">
            DECLARE @idoc int
            EXEC sp_xml_preparedocument @idoc OUTPUT, '$xml$'
            SELECT * FROM OPENXML (@idoc, '$query$',1) WITH ($columns$)
            EXEC sp_xml_removedocument @idoc
        </select>
<select id="XmlTest" parameterClass="hashtable" resultMap="XmlTestResult" extends="XmlTestBase" /> </statements>
</sqlMap>

XmlTestBase does the work and XmlTest sets the result map. The result maps must contain the dbType values since they are used to construct the

$columns$. The $query$ and the $xml$ are passed in from the persistence layer and can come from anywhere (url, config file, etc...)

// calling from the persistence layer:
public ArrayList XmlTest()
{
            ArrayList result = new ArrayList();
string xml = "<ROOT>" + "<Customer CustomerID=\"VINET\" ContactName=\"Paul Henriot\">" + "<Order OrderID=\"10248\" CustomerID=\"VINET\" EmployeeID=\"5\"
OrderDate=\"1996-07-04T00:00:00\">"
                         + "<OrderDetail ProductID=\"11\"
Quantity=\"12\"/>"
                         + "<OrderDetail ProductID=\"42\"
Quantity=\"10\"/>"
                         + "</Order>"
                         + "</Customer>"
+ "<Customer CustomerID=\"LILAS\" ContactName=\"Carlos Gonzlez\">" + "<Order OrderID=\"10283\" CustomerID=\"LILAS\" EmployeeID=\"3\"
OrderDate=\"1996-08-16T00:00:00\">"
                         + "<OrderDetail ProductID=\"72\"
Quantity=\"3\"/>"
                         + "</Order>"
                         + "</Customer>"
                         + "</ROOT>";

            Hashtable hashtable = new Hashtable(3);
            hashtable.Add("xml", xml);
            hashtable.Add("query", "/ROOT/Customer");
            hashtable.Add("columns", GetResultMapColumns("XmlTest"));

            ExecuteFill("XmlTest", hashtable, result);
return result;
}

// Examine the map and retrieve the columns and types used to build the $columns$ value in the map
public string GetResultMapColumns(string statementName)
{
            string result = string.Empty;

            // get the map information
            ISqlMapper sqlMap = GetLocalSqlMap();
IMappedStatement statement = sqlMap.GetMappedStatement(statementName);
            RequestScope scope =
statement.Statement.Sql.GetRequestScope(
                statement, null, sqlMap.LocalSession);

            // retrive the column list and types from the result map
            for (int i = 0; i < scope.ResultMap.Properties.Count; i++)
            {
                // get the result objects
                ResultProperty resultProperty =
scope.ResultMap.Propertiesi;

                // add comma
                if (result != string.Empty)
                    result += ", ";

                // add column name
                result += resultProperty.ColumnName;
// ads the type if (resultProperty.DbType != null && resultProperty.DbType != string.Empty)
                    result += " " + resultProperty.DbType;
            }

            return result;
}



************************************************************************************
This e-mail message and any files transmitted herewith, are intended solely for 
the
use of the individual(s) addressed and may contain confidential, proprietary or privileged information. If you are not the addressee indicated in this message (or responsible for delivery of this message to such person) you may not review, use, disclose or distribute this message or any files transmitted herewith. If you receive this message in error, please contact the sender by reply e-mail and delete
this message and all copies of it from your system.
************************************************************************************


Reply via email to