QString Entity::SerializeToXMLString() const { QDomDocument scene_doc("Scene"); QDomElement scene_elem = scene_doc.createElement("scene"); SerializeToXML(scene_doc, scene_elem); return scene_doc.toString(); }
void VRTDataset::FlushCache() { GDALDataset::FlushCache(); if( !bNeedsFlush || bWritable == FALSE) return; bNeedsFlush = FALSE; // We don't write to disk if there is no filename. This is a // memory only dataset. if( strlen(GetDescription()) == 0 || EQUALN(GetDescription(),"<VRTDataset",11) ) return; /* -------------------------------------------------------------------- */ /* Create the output file. */ /* -------------------------------------------------------------------- */ VSILFILE *fpVRT; fpVRT = VSIFOpenL( GetDescription(), "w" ); if( fpVRT == NULL ) { CPLError( CE_Failure, CPLE_AppDefined, "Failed to write .vrt file in FlushCache()." ); return; } /* -------------------------------------------------------------------- */ /* Convert tree to a single block of XML text. */ /* -------------------------------------------------------------------- */ char *pszVRTPath = CPLStrdup(CPLGetPath(GetDescription())); CPLXMLNode *psDSTree = SerializeToXML( pszVRTPath ); char *pszXML; pszXML = CPLSerializeXMLTree( psDSTree ); CPLDestroyXMLNode( psDSTree ); CPLFree( pszVRTPath ); /* -------------------------------------------------------------------- */ /* Write to disk. */ /* -------------------------------------------------------------------- */ VSIFWriteL( pszXML, 1, strlen(pszXML), fpVRT ); VSIFCloseL( fpVRT ); CPLFree( pszXML ); }
CPLErr GDALPamDataset::TrySaveXML() { CPLXMLNode *psTree; CPLErr eErr = CE_None; nPamFlags &= ~GPF_DIRTY; if( psPam == NULL || (nPamFlags & GPF_NOSAVE) ) return CE_None; /* -------------------------------------------------------------------- */ /* Make sure we know the filename we want to store in. */ /* -------------------------------------------------------------------- */ if( !BuildPamFilename() ) return CE_None; /* -------------------------------------------------------------------- */ /* Build the XML representation of the auxilary metadata. */ /* -------------------------------------------------------------------- */ CPLString osVRTPath = CPLGetPath(psPam->pszPamFilename); psTree = SerializeToXML( osVRTPath ); if( psTree == NULL ) return CE_None; /* -------------------------------------------------------------------- */ /* If we are working with a subdataset, we need to integrate */ /* the subdataset tree within the whole existing pam tree, */ /* after removing any old version of the same subdataset. */ /* -------------------------------------------------------------------- */ if( psPam->osSubdatasetName.size() != 0 ) { CPLXMLNode *psOldTree, *psSubTree; CPLErrorReset(); CPLPushErrorHandler( CPLQuietErrorHandler ); psOldTree = CPLParseXMLFile( psPam->pszPamFilename ); CPLPopErrorHandler(); if( psOldTree == NULL ) psOldTree = CPLCreateXMLNode( NULL, CXT_Element, "PAMDataset" ); for( psSubTree = psTree->psChild; psSubTree != NULL; psSubTree = psSubTree->psNext ) { if( psSubTree->eType != CXT_Element || !EQUAL(psSubTree->pszValue,"Subdataset") ) continue; if( !EQUAL(CPLGetXMLValue( psSubTree, "name", "" ), psPam->osSubdatasetName) ) continue; break; } if( psSubTree == NULL ) { psSubTree = CPLCreateXMLNode( psOldTree, CXT_Element, "Subdataset" ); CPLCreateXMLNode( CPLCreateXMLNode( psSubTree, CXT_Attribute, "name" ), CXT_Text, psPam->osSubdatasetName ); } CPLXMLNode *psOldPamDataset = CPLGetXMLNode( psSubTree, "PAMDataset"); if( psOldPamDataset != NULL ) { CPLRemoveXMLChild( psSubTree, psOldPamDataset ); CPLDestroyXMLNode( psOldPamDataset ); } CPLAddXMLChild( psSubTree, psTree ); psTree = psOldTree; } /* -------------------------------------------------------------------- */ /* Try saving the auxilary metadata. */ /* -------------------------------------------------------------------- */ const char *pszNewPam; int bSaved; CPLPushErrorHandler( CPLQuietErrorHandler ); bSaved = CPLSerializeXMLTreeToFile( psTree, psPam->pszPamFilename ); CPLPopErrorHandler(); /* -------------------------------------------------------------------- */ /* If it fails, check if we have a proxy directory for auxilary */ /* metadata to be stored in, and try to save there. */ /* -------------------------------------------------------------------- */ if( bSaved ) eErr = CE_None; else if( PamGetProxy(GetDescription()) == NULL && ((pszNewPam = PamAllocateProxy(GetDescription())) != NULL)) { CPLErrorReset(); CPLFree( psPam->pszPamFilename ); psPam->pszPamFilename = CPLStrdup(pszNewPam); eErr = TrySaveXML(); } else { CPLError( CE_Warning, CPLE_AppDefined, "Unable to save auxilary information in %s.", psPam->pszPamFilename ); eErr = CE_Warning; } /* -------------------------------------------------------------------- */ /* Cleanup */ /* -------------------------------------------------------------------- */ CPLDestroyXMLNode( psTree ); return eErr; }