📄 wkstoxml.c
字号:
/*------------------------------------------------------------------------------*
* File Name: *
* Creation: *
* Purpose: OriginC Source C file *
* Copyright (c) Originlab Corp. 2002 *
* All Rights Reserved *
* *
* Modification Log: *
*------------------------------------------------------------------------------*/
////////////////////////////////////////////////////////////////////////////////////
// you can include just this typical header file for most Origin built-in functions and classes
// and it takes a reasonable amount of time to compile,
#include <origin.h>
// this file include most of the other header files except the NAG header, which takes longer to compile
// NAG routines
//#include <OC_nag.h> // this contains all the NAG headers,
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
//This sample demonstrates how to use COM object through OriginC's COM support and
//how to access worksheet properties from OriginC.
//
// In this sample, the MSXML parser being used is version 3. You can switch to other
//version by changing the XML_PARSER_APP_ID definition
//
// Function WKSToXML will export an Origin worksheet into an XML document based on
//the schema definition (that schema file is located under OriginC\Samples\COM\
//folder, worksheet.xsd).
//
// Function XMLToWKS will import a formatted XML document into worksheet
//
//
// To export a Worksheet into an XML document:
// 1, make sure there is a worksheet exist (such as Data1) and all columns
// (or columns need to be exported) are numeric format
// 2, load WksToXML.c into workspace and compile it
// 3, execute following command from labtalk window
// WKSToXML Data1 c:\myData1.xml
// This command will export data from worksheet Data1 into XML document,
// c:\myData1.xml
//
// To import a formatted XML document into Worksheet
// 1, load WksToXML.c into workspace and compile it if that has not been done
// 2, execute following command from labtalk window
// XMLToWKS c:\myData1.xml
// That will create a new worksheet and import data from that XML document
// into this worksheet
//
//Please Note:
// 1, Currently, only numeric columns can be exported, all other types will be skipped.
// Support for other column types will be added in the future.
////////////////////////////////////////////////////////////////////////////////////
//use MSXML version 3 feature
//#define XML_PARSER_APP_ID "msxml2.domdocument.3.0"
//use MSXML version 2 feature
//#define XML_PARSER_APP_ID "msxml2.domdocument"
//use MSXML version 1 feature
#define XML_PARSER_APP_ID "msxml.domdocument"
#define XML_INSTRUCTION_TAG "xml"
#define XML_INSTRACTION_CONTENTS "version=\"1.0\""
#define WORKSHEET_TAG "Worksheet"
#define COLUMN_TAG "Column"
#define DATASET_TAG "Dataset"
#define NAME_ATTRIBUTE "Name"
#define LABEL_ATTRIBUTE "Label"
#define NUMOFCOLS_ATTRIBUTE "NumCols"
#define FORMAT_ATTRIBUTE "Format"
#define SUBFORMAT_ATTRIBUTE "SubFormat"
#define TYPE_ATTRIBUTE "Type"
#define NUMROWS_ATTRIBUTE "NumRows"
#define INTERNAL_DATATYPE_ATTRIBUTE "InternalDataType"
#define BIN_BASE64_DATATYPE "bin.base64"
#define SCHEMA_DEFINITION "worksheet.xsd"
#define RELATIVE_PATH_TO_SCHEMA "OriginC\\Samples\\COM\\"
////////////////////////////////////////////////////////////////////////////////////
// start your functions here
static Object g_XMLParser;
static BOOL getXMLParser(Object& parser)
{
//this will remove previous object
parser = CreateObject(XML_PARSER_APP_ID);
if( !parser )
{
printf("Fail to create the XML parser object\n");
return FALSE;
}
return TRUE;
}
//------------------------------------------------------------------------------------------
//XMLToWKS(string strXMLFile = "c:\\Data1.xml")
// The function to import an XML document into worksheet
//Arguments:
// strXMLFile: the full path to the XML file
//------------------------------------------------------------------------------------------
void XMLToWKS(string strXMLFile = "c:\\Data1.xml")
{
if( !getXMLParser(g_XMLParser) )
return;
//disable asynchronous download for embedded documents
g_XMLParser.async = FALSE;
if( !loadXMLFile(strXMLFile) )
return;
Object rootElement;
if( !getRootElement(rootElement) )
return;
if( !rootElement.hasChildNodes() )
{
printf("No child nodes to read\n");
return;
}
Worksheet wks;
wks.Create();
if(!wks)
{
printf("Fail to create a new worksheet\n");
return;
}
//delete all columns from worksheet
while( wks.DeleteCol(0) )
;
Object numOfColsAttr;
string strQuery;
int nNumOfCols;
//get number of columns
strQuery.Format("@%s", NUMOFCOLS_ATTRIBUTE);
numOfColsAttr = rootElement.selectSingleNode(strQuery);
nNumOfCols = numOfColsAttr.nodeTypedValue;
//retrieve node list with all <Column></Column> records
Object colNodeList;
colNodeList = rootElement.childNodes;
if( !colNodeList )
{
printf("Fail to get node list\n");
return;
}
//loop through all nodes and construct the worksheet
Object colNode;
for( int ii=0; ii < nNumOfCols; ii++ )
{
colNode = colNodeList.nextNode();
if( !colNode )
continue;
string strName = colNode.nodeName;
if( strName.Compare(COLUMN_TAG) != 0 )
{
printf("Error to read information: it is not a Column node\n");
continue;
}
//get the first child from column node
// that node is the Dataset node with all data included
Object datasetNode;
datasetNode = colNode.firstChild;
if( !datasetNode )
continue; //this <Column> node has no Dataset child
strName = datasetNode.nodeName;
if( strName.Compare(DATASET_TAG) != 0 )
{
printf("Error to read information: it is not a Dataset node\n");
continue;
}
//add a new column, then use information from XML
//document to initialize column
int nIndex = wks.AddCol();
Column colObj = wks.Columns(nIndex);
setColumnValue(colNode, datasetNode, colObj);
}
out_str("Done!");
}
//------------------------------------------------------------------------------------------
//loadXMLFile(string strFile)
// Parse the XML document in DOM mode
//Arguments:
// strFile: full path to the XML file location
//Return:
// TRUE/FALSE
//------------------------------------------------------------------------------------------
static BOOL loadXMLFile(string strFile)
{
g_XMLParser.async = FALSE;
int nRet = g_XMLParser.load(strFile);
if( nRet != 0 )
{
Object Error = g_XMLParser.parseError;
int nErrCode;
nErrCode = Error.errorCode;
if( nErrCode == 0 )
return TRUE;
string strReason = Error.reason;
printf("Error code is %d\nThe reason is : %s\n", nErrCode, strReason);
return FALSE;
}
return TRUE;
}
//------------------------------------------------------------------------------------------
//getRootElement(Object& rootElement)
// Get the root element from the parsed document. The root node is the <Worksheet> node
//Arguments:
// rootElement: return the object represent the root element in that XML document
//Return:
// TRUE/FALSE
//------------------------------------------------------------------------------------------
static BOOL getRootElement(Object& rootElement)
{
rootElement = g_XMLParser.documentElement;
if( !rootElement )
{
printf("Fail to get root element\n");
return FALSE;
}
return TRUE;
}
//------------------------------------------------------------------------------------------
//getAttributeByName(Object& node, string strAttribute, string& strValue)
// Get attribute's value from its name
//Arguments:
// node: the node to retrieve attribute
// strAttribute: the attribute name
// strValue: value of the attribute, in string format
//Return:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -