根据xml解析创建类的对象

// #include "SDK.h"

// enum emtype{facility_target, aircraft, groundvehicle, launchvehicle, ship, missile, sensor, satellite};

// int main()
// {

//     string stype = "aircraft";
//     emtype m_type = emtype.Parse();

//     cout << m_type << endl;

//     system("pause");
//     return 0;
// }

#include "SDK.h"

class Object
{
protected:
    Object() {}

public:
    virtual ~Object() {}
    static void Register(ClassInfo *ci);
    static Object *CreateObject(std::string name);
    static std::map<std::string, ClassInfo *> *classInfoMap;
};

class ClassInfo
{
public:
    ClassInfo(const std::string className, ObjectConstructorFn ctor);
    ClassInfo();
    Object *CreateObject() const;

public:
    std::string m_className;
    ObjectConstructorFn m_objectConstructor;
};

std::map<std::string, ClassInfo *> *Object::classInfoMap = new std::map<std::string, ClassInfo *>();

void Object::Register(ClassInfo *ci)
{
    if (NULL != ci && classInfoMap->find(ci->m_className) == classInfoMap->end())
    {
        classInfoMap->insert(std::map<std::string, ClassInfo *>::value_type(ci->m_className, ci));
    }
}

Object *Object::CreateObject(std::string name)
{
    std::map<std::string, ClassInfo *>::const_iterator iter = classInfoMap->find(name);
    if (iter != classInfoMap->end())
    {
        return iter->second->CreateObject();
    }
    return NULL;
}

ClassInfo::ClassInfo(const std::string className, ObjectConstructorFn ctor) : m_className(className), m_objectConstructor(ctor)
{
    Object::Register(this);
}

ClassInfo::ClassInfo()
{
}

Object *ClassInfo::CreateObject() const
{
    return m_objectConstructor ? (*m_objectConstructor)() : 0;
}

class facility_target : public Object
{
    DECLARE_CLASS()
public:
    facility_target() {}
    ~facility_target() {}

public:
    string name;
    string type;
    string createPara;
    double Cartesian_X = 0.0;
    double Cartesian_Y = 0.0;
    double Cartesian_Z = 0.0;
    double Cartesian_HighAboveGround = 0.0;
    double Cylindrical_Radius = 0.0;
    double Cylindrical_Longitude = 0.0;
    double Cylindrical_Z = 0.0;
    double Cylindrical_HighAboveGround = 0.0;
    double Geocentric_Latitude = 0.0;
    double Geocentric_Longitude = 0.0;
    double Geocentric_Z = 0.0;
    double Geocentric_HighAboveGround = 0.0;
    double Geodetic_Latitude = 0.0;
    double Geodetic_Longitude = 0.0;
    double Geodetic_Z = 0.0;
    double Geodetic_HighAboveGround = 0.0;
    double Spherical_Latitude = 0.0;
    double Spherical_Longitude = 0.0;
    double Spherical_Z = 0.0;
    double Spherical_HighAboveGround = 0.0;
};

//定义创建类
IMPLEMENT_CLASS("facility_target", facility_target)
// IMPLEMENT_CLASS("aircraft", aircraft)
// IMPLEMENT_CLASS("groundvehicle", groundvehicle)
// IMPLEMENT_CLASS("launchvehicle", launchvehicle)
// IMPLEMENT_CLASS("ship", ship)
// IMPLEMENT_CLASS("missile", missile)
// IMPLEMENT_CLASS("sensor", sensor)
// IMPLEMENT_CLASS("satellite", satellite)

//定义对象的变量名
static Object *m_object;
static facility_target *m_facility_target;
static Object *m_aircraft;
static Object *m_groundvehicle;
static Object *m_launchvehicle;
static Object *m_ship;
static Object *m_missile;
static Object *m_sensor;
static Object *m_satellite;

void createClass()
{
    XMLDocument *doc = new XMLDocument;
    if (doc->LoadFile("test1.xml") != XML_SUCCESS)
    {
        return;
    }
    XMLElement *root = doc->RootElement();
    XMLElement *model = root->FirstChildElement();
    string strName;
    string createType;
    while (model)
    {
        const XMLAttribute *attrModel = model->FirstAttribute();
        if (!strcmp(attrModel->Value(), "地面设施")) //地面设施/目标
            {
                //m_object = Object::CreateObject("facility_target"); //创建对应类
                //m_object->classInfoMap[];
                m_facility_target = new facility_target;
                m_facility_target->name = attrModel->Value(); //将名字初始化
                //strcpy(strType,"facility_target");
                //strType = attrModel->Value();
                //cType = strType.c_str();
            }
        
        XMLElement *attr = model->FirstChildElement();
        
        while (attr)
        {
            if (!strcmp(attr->Name(), "Attributes")) //判断是Attributes 还是 Sensors
            {
                XMLElement *param = attr->FirstChildElement();
                while(param)
                {
                    const XMLAttribute *attrPar = param->FirstAttribute();
                    if(!strcmp(attrPar->Value(), "createPara"))
                        createType = attrPar->Next()->Value();
                    param = param->NextSiblingElement();
                }
                param = attr->FirstChildElement();
                while (param) //遍历 <Attributes> 节点
                {
                    const XMLAttribute *attrParam = param->FirstAttribute();
                    
                    if (!strcmp(attrModel->Next()->Value(), "facility_target")) // 地面设施/目标 类
                    {
                        //cout << "   " << attrParam->Name() << " = " << attrParam->Value() << endl;
                        if (!strcmp(attrParam->Value(), "type"))
                            m_facility_target->type = attrParam->Next()->Value();
                        
                        if (!strcmp(attrParam->Value(), "createPara"))
                            m_facility_target->createPara = attrParam->Next()->Value();
                        
                        if (!strcmp(attrParam->Value(), "X") && createType == "Cartesian")
                            m_facility_target->Cartesian_X = strtod(attrParam->Next()->Value(),NULL);
                        
                        if (!strcmp(attrParam->Value(), "Y") && createType == "Cartesian")
                            m_facility_target->Cartesian_Y = strtod(attrParam->Next()->Value(),NULL);

                        if (!strcmp(attrParam->Value(), "Z") && createType == "Cartesian")
                            m_facility_target->Cartesian_Z = strtod(attrParam->Next()->Value(),NULL);

                        if (!strcmp(attrParam->Value(), "HighAboveGround") && createType == "Cartesian")
                            m_facility_target->Cartesian_HighAboveGround = strtod(attrParam->Next()->Value(),NULL);
                        
                        if (!strcmp(attrParam->Value(), "Radius") && createType == "Cylindrical")
                            m_facility_target->Cylindrical_Radius = strtod(attrParam->Next()->Value(),NULL);
                        
                        if (!strcmp(attrParam->Value(), "Longitude") && createType == "Cylindrical")
                            m_facility_target->Cylindrical_Longitude = strtod(attrParam->Next()->Value(),NULL);

                        if (!strcmp(attrParam->Value(), "Z") && createType == "Cylindrical")
                            m_facility_target->Cylindrical_Z = strtod(attrParam->Next()->Value(),NULL);

                        if (!strcmp(attrParam->Value(), "HighAboveGround") && createType == "Cylindrical")
                            m_facility_target->Cylindrical_HighAboveGround = strtod(attrParam->Next()->Value(),NULL);
                        
                        if (!strcmp(attrParam->Value(), "Latitude") && createType == "Geocentric")
                            m_facility_target->Geocentric_Latitude = strtod(attrParam->Next()->Value(),NULL);
                        
                        if (!strcmp(attrParam->Value(), "Longitude") && createType == "Geocentric")
                            m_facility_target->Geocentric_Longitude = strtod(attrParam->Next()->Value(),NULL);

                        if (!strcmp(attrParam->Value(), "Z") && createType == "Geocentric")
                            m_facility_target->Geocentric_Z = strtod(attrParam->Next()->Value(),NULL);

                        if (!strcmp(attrParam->Value(), "HighAboveGround") && createType == "Geocentric")
                            m_facility_target->Geocentric_HighAboveGround = strtod(attrParam->Next()->Value(),NULL);
                        
                        if (!strcmp(attrParam->Value(), "Latitude") && createType == "Geodetic")
                            m_facility_target->Geodetic_Latitude = strtod(attrParam->Next()->Value(),NULL);
                        
                        if (!strcmp(attrParam->Value(), "Longitude") && createType == "Geodetic")
                            m_facility_target->Geodetic_Longitude = strtod(attrParam->Next()->Value(),NULL);

                        if (!strcmp(attrParam->Value(), "Z") && createType == "Geodetic")
                            m_facility_target->Geodetic_Z = strtod(attrParam->Next()->Value(),NULL);

                        if (!strcmp(attrParam->Value(), "HighAboveGround") && createType == "Geodetic")
                            m_facility_target->Geodetic_HighAboveGround = strtod(attrParam->Next()->Value(),NULL);
                        
                        if (!strcmp(attrParam->Value(), "Latitude") && createType == "Spherical")
                            m_facility_target->Spherical_Latitude = strtod(attrParam->Next()->Value(),NULL);
                        
                        if (!strcmp(attrParam->Value(), "Longitude") && createType == "Spherical")
                            m_facility_target->Spherical_Longitude = strtod(attrParam->Next()->Value(),NULL);

                        if (!strcmp(attrParam->Value(), "Z") && createType == "Spherical")
                            m_facility_target->Spherical_Z = strtod(attrParam->Next()->Value(),NULL);

                        if (!strcmp(attrParam->Value(), "HighAboveGround") && createType == "Spherical")
                            m_facility_target->Spherical_HighAboveGround = strtod(attrParam->Next()->Value(),NULL);
                        
                    }
                    
                    param = param->NextSiblingElement();
                }
            }
            // if (!strcmp(attr->Name(), "Sensors"))
            // {
            //     XMLElement *sensor = param->FirstChildElement();
            //     while (sensor)
            //     {
            //         attrParam = sensor->FirstAttribute();
            //         while (attrParam)
            //         {
            //             cout << "   " << attrParam->Name() << " = " << attrParam->Value() << endl;

            //             attrParam = attrParam->Next();
            //         }
            //         sensor = sensor->NextSiblingElement();
            //     }
            // }
            attr = attr->NextSiblingElement();
        }
        model = model->NextSiblingElement();
    }
}

int main()
{
    // Object* obj = Object::CreateObject("facility_target");
    // delete obj;
    createClass();
    cout << m_facility_target->Cylindrical_Longitude << endl;
    cout << m_facility_target->Cylindrical_HighAboveGround << endl;
    cout << m_facility_target->Cylindrical_Z << endl;
    cout << m_facility_target->createPara << endl;
    cout << m_facility_target->Cylindrical_Radius << endl;
    cout << m_facility_target->type << endl;


    system("pause");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值