为什么此代码会删除以前XML标记的内容?

时间:2011-08-17 22:09:57

标签: c++ xml openframeworks

这是openFrameworks C ++代码。它会保存有关游戏中每次移动的信息,但每个新游戏都会清除<MOVE> <PLAYER/> <MOVE/>内所有以前的<GAME><GAME/>标记内的所有值。

bool My_xml::setup() {
  XML.loadFile(ofxiPhoneGetDocumentsDirectory() + "scoresAndSettings.xml")

  //read the settings from XML
  //if the settings file doesn't exist we assign default values

  globalsettings.user_id = XML.getValue("GLOBALSETTINGS:USERID", ofRandom(std::numeric_limits<float>::max() - 1));
  if (globalsettings.user_id == 0) { // for first game only
    XML.setValue("GLOBALSETTINGS:USERID", globalsettings.user_id);
  }
  globalsettings.playing_big_board = XML.getValue("GLOBALSETTINGS:BIGBOARD", false);
  globalsettings.drawCrossHairs = XML.getValue("GLOBALSETTINGS:DRAWCROSSHAIRS", false);
  globalsettings.graphFFT = XML.getValue("GLOBALSETTINGS:GRAPHFFT", true); //
  globalsettings.showDebugText = XML.getValue("GLOBALSETTINGS:SHOWDEBUGTEXT", false);

  gameNumber = XML.addTag("GAME");
  XML.setAttribute("GAME", "number", gameNumber+1, gameNumber);
  XML.setAttribute("GAME", "start", makeTimeStamp(), gameNumber);
  XML.pushTag("GAME", gameNumber);

  return true;
}

void My_xml::saveMoveInfo() {
  int moveNumber    = XML.addTag("MOVE");
  if( XML.pushTag("MOVE", moveNumber) ){
    // put in data from this move
    int player = ((testApp*)ofGetAppPtr())->gameplay->whoseTurnIsIt;
    string playerTag = "PLAYER " + ofToString(player);
    int tagNum = XML.addTag(playerTag);
    XML.addAttribute(playerTag, "move", moveNumber++, tagNum);
    XML.addAttribute(playerTag, "duration", lastMoveDuration, tagNum);
    XML.addAttribute(playerTag, "wait", waitBeforeThisMove, tagNum);
    XML.addAttribute(playerTag, "timestamp", makeTimeStamp(), tagNum);
    XML.popTag();
  }

  XML.saveFile( ofxiPhoneGetDocumentsDirectory() + "scoresAndSettings.xml" );
}

void My_xml::winGame() {
  writeToFile();
}

void My_xml::writeToFile() {
  XML.popTag();
  XML.setAttribute("GAME", "lat", ofToString(coreLocation->getLatitude(), 10), gameNumber);
  XML.setAttribute("GAME", "long", ofToString(coreLocation->getLongitude(), 10), gameNumber);
  XML.setAttribute("GAME", "end", makeTimeStamp(), gameNumber);
  XML.saveFile( ofxiPhoneGetDocumentsDirectory() + "scoresAndSettings.xml" );
}

编辑/添加:此代码存储校准数据,不会覆盖/清除/吃掉每个游戏。

  if (animations->training != NOT_TRAINING) {
    int numGameTags = XML.XML.getNumTags("GAME");
    XML.XML.pushTag("GAME", numGameTags-1);
    int moveNumber  = XML.XML.addTag("CALIBRATION");

    if (gameplay->whoseTurnIsIt < 2) { // PLAYER 1
      switch (animations->training) {
        case TRAINING_UP:
          XML.player1settings.pitch_high = gameplay->high_pitch_thresh;
          XML.XML.setAttribute("CALIBRATION", "player", gameplay->whoseTurnIsIt, moveNumber);
          XML.XML.setAttribute("CALIBRATION", "type", "pitch", moveNumber);
          XML.XML.setAttribute("CALIBRATION", "level", "high", moveNumber);
          XML.XML.setValue("CALIBRATION", gameplay->high_pitch_thresh, moveNumber);
          break;
        // etc.
        default:
          break;
      } 
    } else { // PLAYER 2
      switch (animations->training) {
        case TRAINING_UP:
          XML.player2settings.pitch_high = gameplay->high_pitch_thresh;
          XML.XML.setAttribute("CALIBRATION", "player", gameplay->whoseTurnIsIt, moveNumber);
          XML.XML.setAttribute("CALIBRATION", "type", "pitch", moveNumber);
          XML.XML.setAttribute("CALIBRATION", "level", "high", moveNumber);
          XML.XML.setValue("CALIBRATION", gameplay->high_pitch_thresh, moveNumber);
          break;
        // etc.
        default:
          break;
      } // end switch
    } // end else
    XML.XML.popTag();
  }

编辑2: 我只是注意到XML标签实际上正在被更改,而不仅仅是删除了内容。请注意,最新游戏中的代码为<PLAYER 1><PLAYER 2>,但所有旧数据中只有<PLAYER /> ...

<GAME number="3" start="2011-08-19|16:00:59|2.7" lat="51.38325077" long="-2.36163312" end="2011-08-19|16:01:29|32.8">
    <PLAYER />
</GAME>
<GAME number="4" start="2011-08-19|16:14:12|2.6">
    <PLAYER />
</GAME>
<GAME number="5" start="2011-08-19|16:16:10|2.9">
    <PLAYER 1 col="0" row="2" err="0" move="1" duration="0.012999" wait="19.784000" timestamp="2011-08-19|16:16:30|22.8"></PLAYER 1>
    <PLAYER 2 col="0" row="2" err="-2" move="1" duration="0.219000" wait="1.605001" timestamp="2011-08-19|16:16:32|24.6"></PLAYER 2>
    <PLAYER 2 col="0" row="2" err="-2" move="2" duration="0.226000" wait="1.500000" timestamp="2011-08-19|16:16:34|26.4"></PLAYER 2>
</GAME>

EDIT3 :Aaaaand就是这样。标签名称中没有空格。啧。

2 个答案:

答案 0 :(得分:0)

你觉得游戏编号在setup()writeToFile()中是一样的吗? 一次保存后,您的XML文件是否正确写入? 同时也应该使用pushTag()popTag()放在正确的标签中。在使用标签之前,最好直接执行此操作。因此,如果我理解您的代码,则必须在writeToFile()之后直接调用setup(),因为您已经推送到setup()中的游戏节点。我会尝试修复它,并使用游戏ID为writeToFile创建一个参数。

答案 1 :(得分:0)

XML标记中不能包含空格。

重新打开文件时,

<PLAYER 1>标记被吃掉了,无论我的代码对它做了什么。

<PLAYER1>很好,并解决了问题。