XML分析器项目

时间:2019-05-22 18:30:50

标签: c++

我必须做XML解析器。它必须读取文件,例如:

<people>
<person id=”0”>
        <name>John Smith</name>
        <address>USA</address>
</person>
<person id=”1”>
        <name>Ivan Petrov</name>
    <address>Bulgaria</address>
</person>
</people>

这是我的代码:

#include "stdafx.h"
#include<iostream>
#include<string>
#include<fstream>

class Person
{
private:
    std::string name;
    std::string address;
    unsigned int id;
public:
    Person()
    {
        name = "";
        address = "";
        id = -1;
    }
    Person(std::string enteredName, std::string enteredAdd, unsigned int enteredID)
    {
        name = enteredName;
        address = enteredAdd;
        id = enteredID;
    }

    void setName(std::string enteredName)
    {
        name = enteredName;
    }
    void setAddress(std::string enteredAddress)
    {
        address = enteredAddress;
    }
    void setID(unsigned int enteredID)
    {
        id = enteredID;
    }
};

//class People
//{
//  Person *p;
//};

void printAllInfo(std::string readLine)
{
    std::cout << readLine;
    std::cout << std::endl;
}
void operations()
{
    std::cout << "-------Operations-------" << std::endl;
    std::cout << "> Print" << std::endl;
    std::cout << "> Select" << std::endl;
    std::cout << "> Exit" << std::endl;
    std::cout << "Choose operation: ";
}

int main()
{
    unsigned int counter = -1;
    Person *p;

    std::string fileName;
    std::cout << "Enter a text file name: ";
    std::getline(std::cin, fileName);

    std::string readLine;
    std::fstream myFile;

    myFile.open(fileName);
    while (myFile.get() && std::getline(myFile, readLine))
    {
        std::string searching = "name";
        std::size_t found = readLine.find(searching);
        if (found != std::string::npos)
        {
            p = new Person[++counter];
            std::string name;

            std::string searching = "<name>";
            std::size_t found = readLine.find(searching);
            if (found != std::string::npos)
            {

            }

            p[counter].setName(name);
            std::string address;
            for (int i = 10; i < readLine.length() - 12; i++)
            {
                address[i] = readLine[i];
            }
            p[counter].setAddress(address);
        }
    }

    std::string operation;
    operations();
    std::getline(std::cin, operation);
    if (operation == "Print" || operation == "print")
    {
        while (std::getline(myFile, readLine))
        {
            printAllInfo(readLine);     
        }
    }
    else if (operation == "Exit" || operation == "exit")
    {
        myFile.close();
        return 0;
    }

    //delete[]p;
    myFile.close();
    return 0;
}

因此,我必须执行“保存”,“另存为”,“ 打开:

  

打开C:\ Temp \ file.xml   成功打开file.xml

您能帮我吗?谁可以从某个目录中打开文件并将其另存为目录中的内容?还应如何将其设置为.xml?因为我只使用.txt

如果在某些情况下此代码是正确的,我必须说,当它从文件读取文件的某一行时,getline将获得整行,并将其放入字符串中。因此,我想从该字符串中取出不带标签的名称/地址,并将其放在新字符串中,新对象中。如果这样做是正确的方法,那就好了。但是这样,当我尝试从字符串中取出名称并将其放入其他字符串时,我得到一个错误,即该字符串超出了诸如此类的范围。

0 个答案:

没有答案