locked
open and read xml file RRS feed

  • Question

  • Hi,

    I am trying to open and read XML File in c++.I am using the following method. I don't want to use .NET.Is there any other way that I can read XML content other than the following method?

            std::ifstream in;
    static CString xml[1000];
    static int xmlx = 0;
    if(xmlx == 0)
    {
    in.open("..\\..\\XMLFILE.xml",std::ifstream::in);
    if(!in.is_open())
    {
    AfxMessageBox(_T("can't open file"));
    }
    while(!in.eof())
    {
    char line[1000];
    int length = 1000;
    in.getline(line,length);
    xml[xmlx] = line;
    ++xmlx;
    }
    in.close();
    }

    Thank you.

    Wednesday, September 7, 2016 2:03 AM

Answers

All replies

  • use boost::property_tree to read, write or manipulate xml file or string.
    Wednesday, September 7, 2016 2:35 AM
  • In order to parse and manipulate XML, consider MSXML library too. Check the sample and documentation: https://msdn.microsoft.com/en-us/library/ms756075(v=vs.85).aspx.

    • Proposed as answer by Baron Bi Wednesday, September 7, 2016 5:51 AM
    • Marked as answer by Hart Wang Tuesday, September 20, 2016 7:40 AM
    Wednesday, September 7, 2016 4:01 AM
  • how to read the contents in the file?
    Wednesday, September 7, 2016 12:00 PM
  • Read the documentation starting at MSXML SDK Overview and experiment with the samples.

    • Marked as answer by Hart Wang Tuesday, September 20, 2016 7:40 AM
    Wednesday, September 7, 2016 12:19 PM
  • I am trying to open and read XML File in c++.I am using the following method. I don't want to use .NET.Is there any other way that I can read XML content other than the following method?

    std::ifstream in;
    static CString xml[1000];
    static int xmlx = 0;
    if(xmlx == 0)
    {
    in.open("..\\..\\XMLFILE.xml",std::ifstream::in);
    if(!in.is_open())
    {
    AfxMessageBox(_T("can't open file"));
    }
    while(!in.eof())
    {
    char line[1000];
    int length = 1000;
    in.getline(line,length);
    xml[xmlx] = line;
    ++xmlx;
    }
    in.close();
    }

    The correct way to read a file into a vector of CString is

    std::ifstream in;
    std::vector<CString> xml
    std::string line;
    while (std::getline(in, line))
    {
      xml.push_back(line.c_str());
    }
    if (!in.eof())
    {
      // some error occurred
    }
    
    

    But to parse XML you need a library. MSXML is one choice, but there are others. See the discussion in

    http://stackoverflow.com/questions/170686/best-open-xml-parser-for-c


    David Wilkinson | Visual C++ MVP

    Wednesday, September 7, 2016 1:06 PM
  • Another option is XmlLite

    https://msdn.microsoft.com/en-us/library/ms752872

    Thursday, September 8, 2016 4:37 AM