views:

40

answers:

1

Hi,

I am using XML Data Mapping and having a problem with generating complex types while using it.

If i am having an XML like below its working fine

<?xml version="1.0" standalone="yes" ?>
<Sample>
      <connection>
        <item  Name="ABC">123</item>
        <item  Name="XYZ">123</item>
        <item  Name="MNO">123</item>
      </connection>
      <connection>
        <item  Name="ABC">123</item>
        <item  Name="XYZ">123</item>
        <item  Name="MNO">123</item>
      </connection>
</Sample>

I am getting complex types as SampleType, ConnectionType and ItemType.

But if i have the XML like

<?xml version="1.0" standalone="yes" ?>
<Sample>
      <connection>
        <item  Name="ABC"/>
        <item  Name="XYZ"/>
        <item  Name="MNO"/>
      </connection>
      <connection>
        <item  Name="ABC"/>
        <item  Name="XYZ"/>
        <item  Name="MNO"/>
      </connection>
</Sample>

I am getting complex types as SampleType, ConnectionType, ItemType, ItemType2, ItemType22, ItemType222, ItemType2222, and ItemType22222 i.e., ItemTypes were equal to number of items present in the XML.

Why this is happening and how can i solve this problem?.

+2  A: 

It is because the data mapper is not meant to infer data from an XML file (or better phrased XML document), but from an XML Schema.

An XML Schema describes the syntax an XML document should adhere to.

XML schema's can for instance be stored as XSD files, or DTD files.

So the first step you should take is to create an XSD schema. You could start with generating an XSD from the XML, then polishing that XSD.

The online XML-2-XSD tool can help you with generating that XSD, but there are many more tools.

Then use that XSD in the data mapper, and you make a much better chance.

Note: XML types are defined differently than most programming languages, so you cannot always map your XML data types to Delphi. Your simple case will work, but as soon as you do recursion or null in XML, it can get pretty hairy.

Edit: added XSD sample for both XML documents.

I used XmlForAsp to infer the XSD so you have a head start.

The inferred XSD for both the first and second XML document is the same:

<?xml version="1.0" encoding="utf-16"?>
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
  <xsd:element name="Sample" type="SampleType" />
  <xsd:complexType name="SampleType">
    <xsd:sequence>
      <xsd:element maxOccurs="unbounded" name="connection" type="connectionType" />
    </xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="connectionType">
    <xsd:sequence>
      <xsd:element maxOccurs="unbounded" name="item" type="itemType" />
    </xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="itemType">
    <xsd:attribute name="Name" type="xsd:string" />
  </xsd:complexType>
</xsd:schema>

--jeroen

Jeroen Pluimers
Thanks Jeroen. "Your simple case will work ..." Can you suggest me any alternative to solve it.
Bharat
Your simple case is very easy to convert to XSD; I've included a working XSD for both XML documents in my answer.
Jeroen Pluimers
Thanks Jeroen .
Bharat