CPersonPtr spPer = CPerson::CreateInstance();
// Load the XML spPer->FromXmlFile(_T("SampleFile.xml"));
printf(_T("%s was born %s"), spPer->GetName().c_str(), spPer->GetDateOfBirth().ToString().c_str()); printf(_T(", and lives at %d, %s\n"), spPer->GetAddress()->GetHouseNo(), spPer->GetAddress()->GetPostCode().c_str());
printf(_T("Cars Owned (%d)\n"), spPer->GetCars()->GetCount());
for (CCarCol::iterator itr = spPer->GetCars()->begin(); itr != spPer->GetCars()->end(); itr++) { CCarPtr spCar = *itr; printf(_T(" %s, %s\n"), spCar->GetMake().c_str(), spCar->GetModel().c_str()); } | IXMLDOMDocument2Ptr spDoc; spDoc.CreateInstance(__uuidof(DOMDocument40));
spDoc->load(_T("SampleFile.xml"));
IXMLDOMElementPtr spElmPerson = GetFirstElement(spDoc); if (spElmPerson == NULL || spElmPerson->nodeName != _bstr_t("Person")) throw new CLtException(_T("Must start with Person"));
IXMLDOMElementPtr spElmName = GetFirstElement(spElmPerson); if (spElmName == NULL || spElmName->nodeName != _bstr_t("Name")) throw new CLtException(_T("Missing Person->Name"));
IXMLDOMElementPtr spElmDOB = GetNextElement(spElmName); if (spElmDOB == NULL || spElmDOB->nodeName != _bstr_t("DateOfBirth")) throw new CLtException(_T("Missing Person->DateOfBirth"));
IXMLDOMElementPtr spElmAddress = GetNextElement(spElmDOB); if (spElmAddress == NULL || spElmAddress->nodeName != _bstr_t("Address")) throw new CLtException(_T("Missing Person->Address"));
IXMLDOMElementPtr spElmHouseNo = GetFirstElement(spElmAddress); if (spElmHouseNo == NULL || spElmHouseNo->nodeName != _bstr_t("HouseNo")) throw new CLtException(_T("Missing Person->Address->HouseNo"));
IXMLDOMElementPtr spElmPostCode = GetNextElement(spElmHouseNo); if (spElmPostCode==NULL||spElmPostCode->nodeName != _bstr_t("PostCode")) throw new CLtException(_T("Missing Person->Address->PostCode"));
if (GetNextElement(spElmPostCode) != NULL) throw new CLtException(_T("Unexpected Element found"));
wprintf(L"%s was born %s, and lives at %d, %s\n", spElmName->text.GetBSTR(), spElmDOB->text.GetBSTR(), spElmHouseNo->text.GetBSTR(), spElmPostCode->text.GetBSTR());
IXMLDOMElementPtr spElmCar = GetNextElement(spElmAddress); while (spElmCar != NULL) { if (spElmCar->nodeName != _bstr_t("Car")) throw new CLtException(_T("Unknown element "));
IXMLDOMElementPtr spElmMake = GetFirstElement(spElmCar); if (spElmMake == NULL || spElmMake->nodeName != _bstr_t("Make")) throw new CLtException(_T("Missing Person->Car->Make"));
IXMLDOMElementPtr spElmModel = GetNextElement(spElmMake); if (spElmModel == NULL || spElmModel->nodeName != _bstr_t("Model")) throw new CLtException(_T("Missing Person->Car->Model"));
if (GetNextElement(spElmModel) != NULL) throw new CLtException(_T("Unexpected Element found"));
wprintf(L" %s, %s\n", spElmMake->text.GetBSTR(), spElmModel->text.GetBSTR());
spElmCar = GetNextElement(spElmCar); } IXMLDOMElementPtr GetNextElement(IXMLDOMNodePtr spXmlNode) { while (spXmlNode != NULL) { spXmlNode = spXmlNode->nextSibling; IXMLDOMElementPtr spElm = spXmlNode; if (spElm != NULL) return spElm; } return NULL; }
IXMLDOMElementPtr GetFirstElement(IXMLDOMNodePtr spXmlParent) { if (spXmlParent == NULL) return NULL; else if (spXmlParent->firstChild == NULL) return NULL; else { IXMLDOMElementPtr spElm = spXmlParent->firstChild; if (spElm != NULL) return spXmlParent->firstChild; else return GetNextElement(spXmlParent->firstChild); } } |