Menu

Problems parsing file

2007-04-11
2013-05-20
  • Roger Durańona Vargas

    I have this file:

    <?xml version="1.0" ?>
    <map>
    <info
      version=1
      id=1
      name="Testing map"
      msg="Welcome to Testing map"
      pvp="no"
    />
    <-- NPCs spawn points -->
    <npcs>
      <npc id=1000 type=2 name="Fat Ninja" x=4900 y=2000 />
      <npc id=1001 type=3 name="Small Ninja" x=4920 y=2010 />
    </npcs>
    </map>

    But I havent found the correct way to parse it. I dont know if the file is wrong and I should redefine the structure or if Im using the wrong parsin method. Can somebody clear me how to do it?

     
    • Ellers

      Ellers - 2007-04-12

      Can't say if you're parsing it right or not because you didn't post any code.

      However, your xml is invalid -- attributes must be quoted, they can be unquoted, like old HTML.

      Tip: put your xml in a file and load it in IE or firefox -- if it loads ok you'll get a nice tree structure, proving that your xml is ok.

      HTH
      Ellers

       
    • Roger Durańona Vargas

      I fixed the xml and here is the parsing code:

               TiXmlHandle chandle(&mapfile);
             
              //load map info
              cout <<"Looking for info...";
             
              cvalue=chandle.FirstChild("map").FirstChild("info").Element();       
              //cvalue=chandle.FirstChild("info").Element(); //another variant
             
              if (cvalue) {
                 name = cvalue->Attribute("name");  
                 welcomsg = cvalue->Attribute("welcome");  
                 cvalue->QueryIntAttribute("id", &id);
                 cout <<id<<name<<endl;
              } else cout<<"Not found info"<<endl;

      This fails to find the desired section. Also, the following approach fails with a crash:

              TiXmlElement *root = mapfile.RootElement();
              //load map info
              cout <<"Looking for info...";
             
             
              cvalue=root->FirstChildElement("info");
              if (cvalue) {
                 name = cvalue->Attribute("name");  
                 welcomsg = cvalue->Attribute("welcome");  
                 cvalue->QueryIntAttribute("id", &id);
                 cout <<id<<name<<endl;
              } else cout<<"Not found info"<<endl;

       
      • Ellers

        Ellers - 2007-04-13

        Which line causes the crash, according to your debugger?
        Are you on win32, linux, or other?
        What is the latest XML?

         
    • Lee Thomason

      Lee Thomason - 2007-04-13

      A good way to check if you XML is valid is to load it into a browser. Both IE and Firefox have very good parsers and can detect invalid XML.

      lee

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.