How Can I Load a document if the base element is an extension?

Problem

It is possible that the base element in an XML Document is an extension of some other type. This being the case you may not know the kind of document you are dealing with before you load it. However, you need to know the type of the document in order to load it into the correct type of object.

Note:
The FpML schemas take this approach.

Simple Example

<xsd:element name = "Request" type = "RequestType"> ... <xsd:complexType name = "RequestType" abstract = "true"> ... <xsd:complexType name = "BalanceRequest"> <xsd:complexContent> <xsd:extension base = "RequestType"> <xsd:sequence> ... <xsd:complexType name = "StatementRequest"> <xsd:complexContent> <xsd:extension base = "RequestType"> <xsd:sequence> ...

Sample Document

<?xml version = "1.0" encoding = "UTF-8"?> <Request xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:type = "StatementRequest"> ...

Extract From FpML Schema

<xsd:element name = "FpML" type = "Document"> ... <xsd:complexType name = "Document" abstract = "true"> ... <xsd:complexType name = "Message" abstract = "true"> <xsd:complexContent> <xsd:extension base = "Document"> <xsd:sequence> ... <xsd:complexType name = "NotificationMessage"> <xsd:complexContent> <xsd:restriction base = "Message"> <xsd:sequence> ... <xsd:complexType name = "TradeAffirmation"> <xsd:complexContent> <xsd:extension base = "NotificationMessage"> <xsd:sequence> ...

Sample FpML Document

<?xml version = "1.0" encoding = "UTF-8"?> <FpML version = "4-0" xmlns = "http://www.fpml.org/2003/FpML-4-0" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:type = "TradeAffirmation"> ...

Resolution

Know what your dealing with.

If you already know the type of document you are dealing with i.e. in he above example BalanceRequest or StatementRequest (or in FpML TradeAffirmation). Then you can simply create the object and load the data into it.

  C++

Sample Document

try { // We know the correct type, so just create an instance of it StatementRequestPtr spObj = StatementRequest::CreateInstance(); // Now read the document into the new object spObj->FromXmlFile(strFullPath.c_str()); } catch (CLtException& e) { _tprintf(_T("FAILED - to validate %s\nError %s\n"), lpctFilename, e.GetFullMessage().c_str()); }

FpML Document

try { // We know the correct type, so just create an instance of it TradeAffirmationPtr spObj = TradeAffirmation::CreateInstance(); // Now read the document into the new object spObj->FromXmlFile(strFullPath.c_str()); } catch (CLtException& e) { _tprintf(_T("FAILED - to validate %s\nError %s\n"), lpctFilename, e.GetFullMessage().c_str()); }

  C#

Sample Document

try { // Now use the class factory to create the object using the root element in the docuemnt as a guide StatementRequest xmlDoc = new StatementRequest(); // now load the data into the newly created object xmlDoc.FromXmlFile(filename); } catch (Exception e) { // Note : exceptions are likely to contain inner exceptions // that provide further detail about the error. while (e != null) { Console.WriteLine("Error - " + e.Message); e = e.InnerException; } }

FpML Document

try { // Now use the class factory to create the object using the root element in the docuemnt as a guide TradeAffirmation ta = new TradeAffirmation(); // now load the data into the newly created object ta.FromXmlFile(filename); } catch (Exception e) { // Note : exceptions are likely to contain inner exceptions // that provide further detail about the error. while (e != null) { Console.WriteLine("Error - " + e.Message); e = e.InnerException; } }

  Java

Sample Document

try { StatementRequest sr = new StatementRequest(); // now load the data into the newly created object sr.fromXmlFile(filename); } catch (Throwable e) { // Note : exceptions are likely to contain inner exceptions // that provide further detail about the error. System.out.println("OK - Successfully failed to load an invalid file " + filename); while (e != null) { System.out.println("Error " + e.getMessage()); e = e.getCause(); } }

FpML Document

try { TradeAffirmation ta = new TradeAffirmation(); // now load the data into the newly created object ta.fromXmlFile(filename); } catch (Throwable e) { // Note : exceptions are likely to contain inner exceptions // that provide further detail about the error. System.out.println("OK - Successfully failed to load an invalid file " + filename); while (e != null) { System.out.println("Error " + e.getMessage()); e = e.getCause(); } }

  Visual Basic

Sample Document

Dim oSR As New Sample.StatementRequest ' now load the the document into the object oSR.FromXmlFile strFilename

FpML Document

Dim oTA As New FPML4.TradeAffirmation ' now load the the document into the object oTA.FromXmlFile strFilename

Load the Document Dynamically

Using this approach you do not need to know the type of the document you are dealing with. You just need to know its base type.
So in the case of the simple sample, this would be an interface based in RequestType (IRequestType). In the case of the FpML Sample this is Document, (so IDocument).

  C++

Sample Document

try { // load the document using the XML parser that comes with Liquid XML LtXmlLib16::CXmlDocumentPtr XmlDoc; XmlDoc.Load(strFullPath.c_str()); // Now use the class factory to create the object using the root element in the docuemnt as a guide IRequestTypePtr spObj = CClassFactory::IRequestTypeCreateObject(XmlDoc.GetDocumentElement()).Ptr(); // Now read the document into the new object spObj->FromXmlElement(XmlDoc.GetDocumentElement()); } catch (CLtException& e) { _tprintf(_T("FAILED - to validate %s\nError %s\n"), lpctFilename, e.GetFullMessage().c_str()); }

FpML Document

try { // load the document using the XML parser that comes with Liquid XML LtXmlLib16::CXmlDocument XmlDoc; XmlDoc.Load(strFullPath.c_str()); // Now use the class factory to create the object using the root element in the docuemnt as a guide LtXmlLib16::CXmlObjectBasePtr spObj = tns::CClassFactory::IDocumentCreateObject(XmlDoc.GetDocumentElement()).Ptr(); // Now read the document into the new object spObj->FromXmlFile(strFullPath.c_str()); // Check the actual type of the root element std::tstring typeName = XmlDoc.GetDocumentElement()->GetAttribute(_T("type"), _T("http://www.w3.org/2001/XMLSchema-instance")); if (typeName == _T("RequestTradeConfirmation")) { // cast the spObj to specific type tns::CRequestTradeConfirmationPtr spRequestTradeConfirmation = dynamic_cast<tns::CRequestTradeConfirmation*>(spObj.Ptr()); std::tstring strXML = spRequestTradeConfirmation->ToXml(); _tprintf(strXML.c_str()); } } catch (CLtException& e) { _tprintf(_T("FAILED - to validate %s\nError %s\n"), lpctFilename, e.GetFullMessage().c_str()); }

  C#

Sample Document

try { // use the standard .Net XML document to load the XML System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument(); xmlDoc.Load(filename); // Now use the class factory to create the object using the root element in the docuemnt as a guide IRequestType xmlDoc = (IRequestType)ClassFactory.IRequestTypeCreateObject(xmlDoc.DocumentElement); // now load the data into the newly created object xmlDoc.FromXmlElement(xmlDoc.DocumentElement); } catch (Exception e) { // Note : exceptions are likely to contain inner exceptions // that provide further detail about the error. while (e != null) { Console.WriteLine("Error - " + e.Message); e = e.InnerException; } }

FpML Document

try { // use the standard .Net XML document to load the XML System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument(); xmlDoc.Load(filename); // Now use the class factory to create the object using the root element in the docuemnt as a guide IDocument fpmlDoc = (IDocument)tns.ClassFactory.IDocumentCreateObject(xmlDoc.DocumentElement); // now load the data into the newly created object fpmlDoc.FromXmlElement(xmlDoc.DocumentElement); } catch (Exception e) { // Note : exceptions are likely to contain inner exceptions // that provide further detail about the error. while (e != null) { Console.WriteLine("Error - " + e.Message); e = e.InnerException; } }

  Java

Sample Document

try { // load the document using the XML parser that comes with Liquid XML com.liquid_technologies.ltxmllib16.dom.XmlDocument parser = new com.liquid_technologies.ltxmllib16.dom.XmlDocument(); parser.parse(filename); // Now use the class factory to create the object using the root element in the docuemnt as a guide com.liquid_technologies.ltxmllib16.dom.XmlElement documentElement = parser.getDocumentElement(); IRequestType doc = (IRequestType)ClassFactory.IRequestTypeCreateObject(documentElement); // now load the data into the newly created object doc.fromXmlElement(fpmlDoc.getDocumentElement()); } catch (Throwable e) { // Note : exceptions are likely to contain inner exceptions // that provide further detail about the error. System.out.println("OK - Successfully failed to load an invalid file " + filename); while (e != null) { System.out.println("Error " + e.getMessage()); e = e.getCause(); } }

FpML Document

try { // load the document using the XML parser that comes with Liquid XML com.liquid_technologies.ltxmllib16.dom.XmlDocument parser = new com.liquid_technologies.ltxmllib16.dom.XmlDocument(); parser.parse(filename); // Now use the class factory to create the object using the root element in the docuemnt as a guide com.liquid_technologies.ltxmllib16.dom.XmlElement documentElement = parser.getDocumentElement(); IDocument fpmlDoc = (IDocument)tns.ClassFactory.IDocumentCreateObject(documentElement); // now load the data into the newly created object fpmlDoc.fromXmlElement(fpmlDoc.getDocumentElement()); } catch (Throwable e) { // Note : exceptions are likely to contain inner exceptions // that provide further detail about the error. System.out.println("OK - Successfully failed to load an invalid file " + filename); while (e != null) { System.out.println("Error " + e.getMessage()); e = e.getCause(); } }

  Visual Basic

Sample Document

Dim elm As IDocument Dim oCF As New Sample.ClassFactory Dim oMsXML As New MSXML2.DOMDocument ' Because the initial element is abstract ' we need to create it using the ClassFactory method IRequestTypeCreateObject oMsXML.Load strFilename ' Get the class factory to create the appropriate object based on the document. Set elm = oCF.IDocumentCreateObject(oMsXML.documentElement) ' now load the the document into the object elm.FromXmlFile strFilename

FpML Document

Dim elm As IDocument Dim oCF As New FPML4.ClassFactory Dim oMsXML As New MSXML2.DOMDocument ' Because the initial FpML element is abstract ' we need to create it using the ClassFactory method IDocumentCreateObject oMsXML.Load strFilename ' Get the class factory to create the appropriate object based on the document. Set elm = oCF.IDocumentCreateObject(oMsXML.documentElement) ' now load the the document into the object elm.FromXmlFile strFilename

 

Notes

Description Value
Article Created 6/2/2006
Versions Liquid XML 2005 (4.2.0) and greater