为什么此代码会删除以前的 XML 标记的内容?
这是 openFrameworks C++ 代码。它保存有关游戏中每个动作的信息,但每个新游戏都会清除
适用于任何先前的
标签。
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 标签实际上被更改了,而不仅仅是删除了内容。请注意,在最近的游戏中,标签是
或
,但只是
> 在所有旧数据中...
<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:啊啊就是这样。标签名称中没有空格。谢什。
This is openFrameworks C++ code. It's saving information about each move made in a game, but each new game wipes out all of the values inside <MOVE> <PLAYER/> <MOVE/>
for any previous <GAME><GAME/>
tags.
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" );
}
EDIT/Addition: This code, which stores calibration data, doesn't get overwritten/cleared/eaten each game.
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();
}
EDIT 2:
I just noticed that the XML tags are actually being altered, not just having the contents removed. Note that the tag is <PLAYER 1>
or <PLAYER 2>
in the most recent game, but just <PLAYER />
in all the old data...
<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 that's it. No spaces in a tag name. Sheesh.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您确定 setup() 和 writeToFile() 中的游戏编号相同吗?
一次保存后,您的 XML 文件写入正确吗?
还要确保使用
pushTag()
和popTag()
位于正确的标签中。最好在使用标签之前直接执行此操作。因此,如果我正确理解您的代码,则必须在setup()
之后直接调用writeToFile()
,因为您已在setup() 中推送到游戏节点
。我会尝试解决这个问题,并使用游戏 ID 为 writeToFile 创建一个参数。are you shure that gamenumber is the same in
setup()
andwriteToFile()
?Is your XML File written correctly after one save?
Also be shure to be in the right tags with
pushTag()
andpopTag()
. Its better to do this straight before you use the tag. So if I'm understanding your code rightwriteToFile()
has to be called directly aftersetup()
because you push to your Game Node already insetup()
. I would try to fix that and would make a parameter to writeToFile with the Game ID.XML 标记中不能有空格。
当文件重新打开时,
标签被吃掉,无论我的代码对它做了什么。
很好,解决了问题。XML tags can't have spaces in them.
<PLAYER 1>
tags were getting eaten when the file was reopened, regardless of whatever my code was doing to it.<PLAYER1>
is fine, and fixes the problem.