解析xml时被卡住了,求解中--> 已解决

41 篇文章 1 订阅
2 篇文章 0 订阅

原因是自己写的程序保存文件时没有按照标准pascal的格式,今天写了个小程序,转换了其文本格式,问题得以解决,具体解决办法的code如下:

//xml格式转换功能函数
void xmlFormatForTab()
{
	vector<string> filesOfXmls;
	string pth_xml;
	pth_xml = "M:\\car\\";
	getFiles(pth_xml, filesOfXmls);
	int sizeOfXmls = filesOfXmls.size();
	cout << sizeOfXmls << endl;

	ifstream ifs_read_xml;
	fstream fs_write_xml;

	for (int l = 0; l < sizeOfXmls; l++)
	{
		ifs_read_xml.open(filesOfXmls[l].c_str(), ios::in);
		string filesOfxml = filesOfXmls[l];
		const char pth_tmp[17] = "\\carFinal";
		filesOfxml.insert(6, pth_tmp);
		filesOfXmls[l].erase(7,1);
		filesOfxml.erase(15,1);
		string new_pth_xml = filesOfxml;
		fs_write_xml.open(new_pth_xml, ios::out);
		for (int line = 0; line < 33; line++)
		{
			if (line == 0)
			{
				continue;
			}
			else
			{
				char p[100] = { 0 };
				int pos = 0;
				string p_tmp;

				ifs_read_xml.getline(p, 100, '\n');
				cout << strlen(p) << endl;
				cout << line << endl;
				for (int index = 0; index < strlen(p); index++)
				{
					if (p[index] == '<')
					{
						pos = index-1;
						break;
					}
					else
					{
						continue;
					}
				}
				cout << pos << endl;
				if (line == 3 || line == 4 || line == 5 || line == 10 || line == 11 || line == 14 || line == 15 || line == 19 || line == 20 || line == 21 || line == 32)
				{
					p_tmp = p;
					p_tmp.erase(0, 2);
					cout << p_tmp << endl;
					p_tmp.insert(0, 1, '\t');
					fs_write_xml.write(p_tmp.c_str(), strlen(p_tmp.c_str()));
					fs_write_xml.write("\n", 1);
				}
				else if (line == 6 || line == 7 || line == 8 || line == 9 || line == 12 || line == 13 || line == 16 || line == 17 || line == 18 || line == 22 || line == 23 || line == 24 || line == 25 || line == 26 || line == 31)
				{
					p_tmp = p;
					p_tmp.erase(0, 4);
					cout << p_tmp << endl;
					p_tmp.insert(0, 2, '\t\t');
					fs_write_xml.write(p_tmp.c_str(), strlen(p_tmp.c_str()));
					fs_write_xml.write("\n", 1);
				}
				else if (line == 2 || line > 32)
				{
					p_tmp.erase(0, 1);
					p_tmp = p;
					cout << p_tmp << endl;
					fs_write_xml.write(p_tmp.c_str(), strlen(p_tmp.c_str()));
					fs_write_xml.write("\n", 1);
				}
				else if (line==27||line==28||line==29||line==30)
				{
					p_tmp = p;
					p_tmp.erase(0, 6);
					cout << p_tmp << endl;
					p_tmp.insert(0, 3, '\t\t\t');
					fs_write_xml.write(p_tmp.c_str(), strlen(p_tmp.c_str()));
					fs_write_xml.write("\n", 1);
				}
				else
				{
					cout << "skip\n" << endl;
				}
				
			}
		}
		fs_write_xml.write("</annotation>", 13);
		fs_write_xml.close();
		ifs_read_xml.close();
	}

	
}
写的比较啰嗦,也不具有通用性,不过能解决自己的问题,也OK了!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值