错误:无法打开主文档实体

时间:2018-09-18 11:49:58

标签: c++11 xerces-c

我正在尝试使用xerces库在我的c ++程序中解析xml文件(在xsd中也有架构定义)。为了开始工作,我编写了一个小程序,在其中我仅使用xml文件初始化了std :: unique_pointer。如果在初始化时使用包含xml文件的std :: string对象,则会收到以下错误,而如果直接使用xml文件进行初始化,则程序运行会很好。

主程序如下:

#include <stdio.h>
#include <iostream>
#include "ShDataTypeRel15.hxx"
#include<fstream>
#include<string>

using namespace std;

int main (int argc, char* argv[])
{
   try
    {

fstream t("/home/vishal/UDA_XML/ShDataTypeRel15.xml", ios::in);
stringstream buffer;
buffer << t.rdbuf();

std::string xml_file = buffer.str();

        std::unique_ptr<tSh_Data> Shdata(Sh_Data(xml_file));

    }
      catch (const xml_schema::exception& e)
      {
              cout <<"Exception caught"<<std::endl;
          std::cerr << e << std::endl;
          return 1;
      }


return 0;
}

当我将std::unique_ptr<tSh_Data> Shdata(Sh_Data(xml_file));替换为std::unique_ptr<tSh_Data> Shdata(Sh_Data(argv[1]));时,程序运行正常(我将xml文件的路径作为命令行输入。)

我收到以下错误:

Exception caught
:0:0 error: unable to open primary document entity '/home/vishal/UDA_XML/<?xml version="1.0"?>

上面的错误声明后面是xml文件。

1 个答案:

答案 0 :(得分:0)

在我将XML文件的位置而不是该XML文件的全部内容存储在字符串对象中之后,问题解决了。 即 现在我的std :: string xml_file = path_to_xml_file;