两个关于xml 的问题

shiter
人工智能领域优质创作者
博客专家认证
2015-05-12 10:52:57
1.现在问个问题,解析xml,设计调查问卷的类,比如调查问卷的问题,单选多选,文本问题,用什么设计模式设计类比较好

2,第二个,解析xml使用tinyxml2,但是loadfile进来直接是内存对象的dom树结构。


我们觉的想用他的visitor,结果是没有实现。我只想现在简单的问题解析出来放到上面设计的类里面就行。


问题里面还有一些子问题

<questions 一些属性。。。>
<question一些属性。。。>
<label>您的性别是?
<\label>
<\question>
<question一些属性。。。>
<\question>


<\questions>

...全文
365 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaohuh421 2015-05-15
  • 打赏
  • 举报
回复
tinyxml2 直接解析你的xml 然后通过遍历, 得到每一个属性页面. 然后给你的结构体赋值. 你的想像是美好的. 不复制内存, 只用指针, 那么你的结构体存在的同时, 要保证那个tinyxml2相关的对象存在. 所以你的想法完全是把简单的事情想复杂了. 现在不管是内存还是手机, 那么几K的内存. 完全不是事. 除非是嵌入式
shiter 2015-05-15
  • 打赏
  • 举报
回复
引用 7 楼 xiaohuh421 的回复:
tinyxml2 直接解析你的xml 然后通过遍历, 得到每一个属性页面. 然后给你的结构体赋值. 你的想像是美好的. 不复制内存, 只用指针, 那么你的结构体存在的同时, 要保证那个tinyxml2相关的对象存在. 所以你的想法完全是把简单的事情想复杂了. 现在不管是内存还是手机, 那么几K的内存. 完全不是事. 除非是嵌入式
看了一个类似这样的方法

/****************************************************************************
 Copyright (c) 2010 Максим Аксенов
 Copyright (c) 2010 cocos2d-x.org  
 Copyright (c) 2013 Martell Malone
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 ****************************************************************************/

#include "platform/CCSAXParser.h"

#include <vector> // because its based on windows 8 build :P

#include "platform/CCFileUtils.h"
#include "tinyxml2.h"


NS_CC_BEGIN

class XmlSaxHander : public tinyxml2::XMLVisitor
{
public:
	XmlSaxHander():_ccsaxParserImp(0){};
	
	virtual bool VisitEnter( const tinyxml2::XMLElement& element, const tinyxml2::XMLAttribute* firstAttribute );
	virtual bool VisitExit( const tinyxml2::XMLElement& element );
	virtual bool Visit( const tinyxml2::XMLText& text );
	virtual bool Visit( const tinyxml2::XMLUnknown&){ return true; }

	void setSAXParserImp(SAXParser* parser)
	{
		_ccsaxParserImp = parser;
	}

private:
	SAXParser *_ccsaxParserImp;
};


bool XmlSaxHander::VisitEnter( const tinyxml2::XMLElement& element, const tinyxml2::XMLAttribute* firstAttribute )
{
	//log(" VisitEnter %s",element.Value());

	std::vector<const char*> attsVector;
	for( const tinyxml2::XMLAttribute* attrib = firstAttribute; attrib; attrib = attrib->Next() )
	{
		//log("%s", attrib->Name());
		attsVector.push_back(attrib->Name());
		//log("%s",attrib->Value());
		attsVector.push_back(attrib->Value());
	}
    
    // nullptr is used in c++11
	//attsVector.push_back(nullptr);
    attsVector.push_back(nullptr);

	SAXParser::startElement(_ccsaxParserImp, (const CC_XML_CHAR *)element.Value(), (const CC_XML_CHAR **)(&attsVector[0]));
	return true;
}
bool XmlSaxHander::VisitExit( const tinyxml2::XMLElement& element )
{
	//log("VisitExit %s",element.Value());

	SAXParser::endElement(_ccsaxParserImp, (const CC_XML_CHAR *)element.Value());
	return true;
}

bool XmlSaxHander::Visit( const tinyxml2::XMLText& text )
{
	//log("Visit %s",text.Value());
	SAXParser::textHandler(_ccsaxParserImp, (const CC_XML_CHAR *)text.Value(), static_cast<int>(strlen(text.Value())));
	return true;
}

SAXParser::SAXParser()
{
    _delegator = nullptr;
}

SAXParser::~SAXParser(void)
{
}

bool SAXParser::init(const char *encoding)
{
    CC_UNUSED_PARAM(encoding);
    // nothing to do
    return true;
}

bool SAXParser::parse(const char* xmlData, size_t dataLength)
{
	tinyxml2::XMLDocument tinyDoc;
	tinyDoc.Parse(xmlData, dataLength);
	XmlSaxHander printer;
	printer.setSAXParserImp(this);
	
	return tinyDoc.Accept( &printer );	
}

bool SAXParser::parse(const std::string& filename)
{
    bool ret = false;
    Data data = FileUtils::getInstance()->getDataFromFile(filename);
    if (!data.isNull())
    {
        ret = parse((const char*)data.getBytes(), data.getSize());
    }

    return ret;
}

void SAXParser::startElement(void *ctx, const CC_XML_CHAR *name, const CC_XML_CHAR **atts)
{
    ((SAXParser*)(ctx))->_delegator->startElement(ctx, (char*)name, (const char**)atts);
}

void SAXParser::endElement(void *ctx, const CC_XML_CHAR *name)
{
    ((SAXParser*)(ctx))->_delegator->endElement(ctx, (char*)name);
}
void SAXParser::textHandler(void *ctx, const CC_XML_CHAR *name, int len)
{
    ((SAXParser*)(ctx))->_delegator->textHandler(ctx, (char*)name, len);
}
void SAXParser::setDelegator(SAXDelegator* delegator)
{
    _delegator = delegator;
}

NS_CC_END





shiter 2015-05-14
  • 打赏
  • 举报
回复
引用 5 楼 xiaohuh421 的回复:
不要烂用设计模式,
大牛,现在是这样,我用tinyxml2,有个loadfile直接把dom格式的xml加载进了内容,我的类想把数据放到我的类中,但是又不想多复制内存,现在的想法是类的成员全部作为指针,指向相关的内容,不知道这样的设计是否合理
xiaohuh421 2015-05-14
  • 打赏
  • 举报
回复
不要烂用设计模式,
shiter 2015-05-13
  • 打赏
  • 举报
回复
引用 2 楼 ID870177103 的回复:
很困难吗 类qustions获得一个qustion[]和它自身参数 类qustion是qustions子类,用于获得它自身参数和label等内容 至于dom的用法,百科一些它有哪些成员函数不就知道了?
什么交dom的用法?
shiter 2015-05-13
  • 打赏
  • 举报
回复
引用 1 楼 xiaohuh421 的回复:
不要什么都想到设计模式, 老老实实的一条条解析吧. xml就是一个文档. 顺序遍历解析出来即可.
学着玩,想试试设计的观察者模式
ID870177103 2015-05-13
  • 打赏
  • 举报
回复
很困难吗
类qustions获得一个qustion[]和它自身参数
类qustion是qustions子类,用于获得它自身参数和label等内容
至于dom的用法,百科一些它有哪些成员函数不就知道了?
xiaohuh421 2015-05-13
  • 打赏
  • 举报
回复
不要什么都想到设计模式, 老老实实的一条条解析吧. xml就是一个文档. 顺序遍历解析出来即可.

65,171

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧