TXMLDocumentRegisterDocBinding (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

The following example uses the RegisterDocBinding function.

Class diagram

Code

// Custom interface
__interface INTERFACE_UUID("{49C50BE3-4084-4A66-B919-000984A657FB}") ISpec
	: public IInterface {
public:
	virtual String __fastcall GetSpecField() = 0;
};
typedef System::DelphiInterface<ISpec>_di_ISpec;

// Custom class
class TSpecXMLNode : public TXMLNode, public ISpec {
	INTFOBJECT_IMPL_IUNKNOWN(TXMLNode);

public:
	void __fastcall AfterConstruction() {
		printf("%ls AfterConstruction.\n", TSpecXMLNode::ClassName());
	}

	String __fastcall GetSpecField() {
		return "FieldValue";
	}
};

void Test_RegisterDocBinding() {
	const String tag = "TestElement";
	_di_IXMLDocument document;

	document = interface_cast<Xmlintf::IXMLDocument>(new TXMLDocument(NULL));
	document->Active = true;

	// Create document element.
	document->DocumentElement = document->CreateElement(tag, "");

	// Register implementation class.
	// Should display "TSpecXMLNode AfterConstruction".
	document->RegisterDocBinding(tag, __classid(TSpecXMLNode));

	// Use the document in some way. Otherwise no binding happens.
	printf("Document element local name is %ls\n",
		document->DocumentElement->LocalName);

	// _di_IXMLNode interface to document element
	const _di_IXMLNode node = document->DocumentElement;

	// _di_ISpec interface to document element
	_di_ISpec specNode;

	if (node->Supports(specNode)) {
		printf("SpecField=%ls\n", specNode->GetSpecField());
	}
	else {
		// Should not happen.
		printf("Interface unsupported.\n");
	}
}

Uses