找了很久的数据,只找到xml格式的数据,但是用visual studio打开后我却看不懂。
没有编程背景,所以不知道怎么提取这些数据。
请问需要用什么软件?
或者需要写什么代码吗?
我想要xml文件中的数据。
对上面的第2节中的xml文件进行解析。
第一步,加载xml文件
第二步,找到根节点
第三步,获取子节点信息
Import.cpp代码如下:
#include <iostream>
#include <vector>
#include "tinyxml2.h"
using namespace std;
using namespace tinyxml2;
tinyxml2::XMLDocument doc;
typedef struct
{
string value;
}RowContent;
typedef struct
{
vector<RowContent> rowContents;
}Row;
typedef struct
{
string ddl;
vector<Row> rows;
}Table;
typedef struct
{
string name;
vector<Table> tables;
}Database;
//解析xml文件
void DP(vector<Database> & DataBases)
{
{
/*if (!root)
{
return;
}
const char* content;
content = root->GetText();
if (content)
{
cout << content << endl;
}
DP(root->FirstChildElement());
DP(root->NextSiblingElement());*/
}
XMLDocument doc;
doc.LoadFile("C:/Users/Administrator/Desktop/DataBaseXML.xml");
XMLElement *XMLdataBases = doc.RootElement();
XMLElement *XMLdataBase = XMLdataBases->FirstChildElement("database");
while (XMLdataBase)
{
XMLElement *DBName = XMLdataBase->FirstChildElement("name");
Database database;
database.name = (string)DBName->GetText();
XMLElement *DBTables = XMLdataBase->FirstChildElement("tables");
XMLElement *DBTable = DBTables->FirstChildElement("table");
while (DBTable)
{
XMLElement *XMLddl = DBTable->FirstChildElement("ddl");
Table table;
table.ddl = (string)XMLddl->GetText();
XMLElement *DBRows = DBTable->FirstChildElement("rows");
XMLElement *DBRow = DBRows->FirstChildElement("row");
while (DBRow)
{
Row row;
XMLElement *XMLRowValue = DBRow->FirstChildElement("value");
while (XMLRowValue)
{
RowContent rowContent;
const XMLAttribute *nullValue = XMLRowValue->FirstAttribute()->Next();
if (!nullValue)
{
rowContent.value = (string)XMLRowValue->GetText();
}
else
{
rowContent.value = "NULL";
}
row.rowContents.push_back(rowContent);
XMLRowValue = XMLRowValue->NextSiblingElement();
}
table.rows.push_back(row);
DBRow = DBRow->NextSiblingElement();
}
database.tables.push_back(table);
DBTable = DBTable->NextSiblingElement();
}
DataBases.push_back(database);
//下一个兄弟节点
XMLdataBase = XMLdataBase->NextSiblingElement();
}
}
int main()
{
vector<Database> DataBases;
DP(DataBases);
for (vector<Database>::iterator it = DataBases.begin(); it != DataBases.end(); ++it)
{
Database database = *it;
cout << database.name.c_str() << endl;
for (vector<Table>::iterator it1 = database.tables.begin(); it1 != database.tables.end(); ++it1)
{
Table table = *it1;
cout << table.ddl.c_str() << endl;
for (vector<Row>::iterator it2 = table.rows.begin(); it2 != table.rows.end(); ++it2)
{
Row row = *it2;
for (vector<RowContent>::iterator it3 = row.rowContents.begin(); it3 != row.rowContents.end(); ++it3)
{
RowContent rowContent = *it3;
cout << rowContent.value.c_str();
}
cout << endl;
}
}
cout << endl;
}
system("pause");
return 0;
}