分享
 
 
 

20051010: xerces-c

王朝other·作者佚名  2006-01-10
窄屏简体版  字體: |||超大  

这两天一直想学习在C++中环境下进行XML编程,但是进度有点缓慢,目前只学会用DOM进行一些处理,有些处理还有问题(如newDoc方法中不知如何在b.xml文件头加入xml version="1.0"等等内容),我随便写了个XML文件进行测试:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE person SYSTEM "a.dtd">

<person>

<people>

<name>陈</name>

<sex>male</sex>

<age>30</age>

</people>

<people>

<name>abched</name>

<sex>female</sex>

<age>1</age>

</people>

</person>

声明了一个XML处理类:

struct suPerson

{

string _name;

string _sex;

int _age;

};

class TParse

{

private:

string _strErrMsg;

DOMImplementation *_impl;

DOMBuilder *_parser;

DOMWriter* _writer;

DOMDocument* _doc;

DOMDocument* _newDoc;

vector<suPerson> _vecPerson;

bool parseNode(DOMNode *n);

public:

TParse();

~TParse();

bool parse(const char* strXmlFile);

bool newDoc(const char* strXmlFile);

string getErrMsg()

{

return _strErrMsg;

}

vector<suPerson> getVector()

{

return _vecPerson;

}

};

类的具体定义如下:

#include "Parse.h"

TParse::TParse()

{

try

{

XMLPlatformUtils::Initialize();

}

catch (const XMLException& toCatch)

{

char* message = XMLString::transcode(toCatch.getMessage());

_strErrMsg = message;

XMLString::release(&message);

throw _strErrMsg;

}

XMLCh tempStr[100];

XMLString::transcode("LS", tempStr, 99);

_impl = DOMImplementationRegistry::getDOMImplementation(tempStr);

_parser = _impl->createDOMBuilder(DOMImplementationLS::MODE_SYNCHRONOUS, 0);

_writer = ((DOMImplementationLS*)_impl)->createDOMWriter();

// optionally, set some DOMWriter features

// set the format-pretty-print feature

if (_writer->canSetFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true))

_writer->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true);

// set the byte-order-mark feature

if (_writer->canSetFeature(XMLUni::fgDOMWRTBOM, true))

_writer->setFeature(XMLUni::fgDOMWRTBOM, true);

}

TParse::~TParse()

{

_parser->release();

if (_newDoc)

_newDoc->release();

_writer->release();

}

bool TParse::parse(const char* strXmlFile)

{

try

{

_doc = _parser->parseURI(XMLString::transcode(strXmlFile));

}

catch (const XMLException& toCatch)

{

char* message = XMLString::transcode(toCatch.getMessage());

_strErrMsg = message;

XMLString::release(&message);

throw _strErrMsg;

}

catch (const DOMException& toCatch)

{

char* message = XMLString::transcode(toCatch.msg);

_strErrMsg = message;

XMLString::release(&message);

throw _strErrMsg;

}

catch (...)

{

_strErrMsg = "Unexpected Exception \n";

throw _strErrMsg;

}

if (!_doc)

{

_strErrMsg = "_doc is null";

return false;

}

DOMNode* n = (DOMNode*)_doc->getDocumentElement();

_vecPerson.clear();

if (!n)

{

_strErrMsg = "it is not an legal node";

return false;

}

if (n)

{

if (n->getNodeType() == DOMNode::ELEMENT_NODE)

{

DOMNodeList* nodeList = n->getChildNodes();

unsigned int nListLen = nodeList->getLength();

for (unsigned int i=0; i<nListLen; ++i)

{

DOMNode* nodeTemp = nodeList->item(i);

if (nodeTemp->getNodeType() == DOMNode::ELEMENT_NODE)

{

suPerson suTemp;

memset(&suTemp, 0, sizeof(suTemp));

for (DOMNode* node1=nodeTemp->getFirstChild(); node1!=0; node1=node1->getNextSibling())

{

char* name = XMLString::transcode(node1->getNodeName());

string strTemp = name;

if (strTemp == "name")

{

suTemp._name = XMLString::transcode(node1->getFirstChild()->getNodeValue());

node1->getFirstChild()->setNodeValue(XMLString::transcode((suTemp._name+" hello").c_str()));

}

else if (strTemp == "sex")

{

suTemp._sex = XMLString::transcode(node1->getFirstChild()->getNodeValue());

}

else if (strTemp == "age")

{

suTemp._age = XMLString::parseInt(node1->getFirstChild()->getNodeValue());

}

XMLString::release(&name);

}

_vecPerson.push_back(suTemp);

}

}

}

}

return true;

}

//---------------------------------------------------------------------------

bool TParse::newDoc(const char* strXmlFile)

{

try

{

_newDoc = _impl->createDocument();

}

catch (const XMLException& toCatch)

{

char* message = XMLString::transcode(toCatch.getMessage());

_strErrMsg = message;

XMLString::release(&message);

throw _strErrMsg;

}

catch (const DOMException& toCatch)

{

char* message = XMLString::transcode(toCatch.msg);

_strErrMsg = message;

XMLString::release(&message);

throw _strErrMsg;

}

catch (...)

{

_strErrMsg = "Unexpected Exception \n";

throw _strErrMsg;

}

DOMElement* root = _newDoc->createElement(XMLString::transcode("person"));

for (unsigned int i=0; i<2; ++i)

{

DOMElement* item = _newDoc->createElement(XMLString::transcode("people"));

DOMElement* name = _newDoc->createElement(XMLString::transcode("name"));

DOMAttr* attr = _newDoc->createAttribute(XMLString::transcode("id"));

attr->setNodeValue(XMLString::transcode("hello"));

name->appendChild(_newDoc->createTextNode(XMLString::transcode("C陈")));

name->setAttributeNode(attr);

DOMElement* sex = _newDoc->createElement(XMLString::transcode("male"));

sex->appendChild(_newDoc->createTextNode(XMLString::transcode("male")));

DOMElement* age = _newDoc->createElement(XMLString::transcode("age"));

age->appendChild(_newDoc->createTextNode(XMLString::transcode("30")));

item->appendChild(name);

item->appendChild(sex);

item->appendChild(age);

root->appendChild(item);

}

_newDoc->appendChild(root);

// construct the LocalFileFormatTarget

XMLFormatTarget *myFormatTarget = new LocalFileFormatTarget(strXmlFile);

// serialize a DOMNode to the local file "myXMLFile.xml"

_writer->writeNode(myFormatTarget, *_newDoc->getDocumentElement());

// optionally, you can flush the buffer to ensure all contents are written

myFormatTarget->flush();

// release the memory

delete myFormatTarget;

return true;

}

测试代码:

#include "Parse.h"

int main (int argc, char* args[])

{

try

{

TParse parse;

if (!parse.parse("a.xml"))

{

cout << "error: " << parse.getErrMsg() << endl;

}

vector<suPerson> vecTemp = parse.getVector();

for (unsigned int i=0; i<vecTemp.size(); ++i)

{

cout << i << "--------------" << endl;

cout << "name: " << vecTemp[i]._name << endl;

cout << "sex: " << vecTemp[i]._sex << endl;

cout << "age: " << vecTemp[i]._age << endl;

}

cout << "--------------------------------" << endl;

parse.newDoc("b.xml");

}

catch(const string& str)

{

cout << "error: " << str << endl;

}

cin.get();

return 0;

}

这边请教大家一个问题,如何用代码实现在生成的b.xml头部加上XML的版本和编码声明?通过ultraedit看b.xml,里面的字符的默认格式是UNICODE编码

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有