QByteArray xmlcontents; QXmlStreamWriter xmlstreamwriter(&xmlcontents); xmlstreamwriter.setAutoFormatting(true); xmlstreamwriter.writeStartDocument(); xmlstreamwriter.writeStartElement("xml"); xmlstreamwriter.writeStartElement("round"); xmlstreamwriter.writeAttribute("id", mRoundId); xmlstreamwriter.writeAttribute("tableid", mTableId); //player1 xmlstreamwriter.writeStartElement("player1"); xmlstreamwriter.writeAttribute("framescore", mPlayer1Frame); xmlstreamwriter.writeAttribute("points", mPlayer1Point); xmlstreamwriter.writeAttribute("break", mPlayer1ContinuePoint);
xmlstreamwriter.writeCharacters("Hello World"); //添加内容 xmlstreamwriter.writeEndElement(); //player2 xmlstreamwriter.writeStartElement("player2"); xmlstreamwriter.writeAttribute("framescore", mPlayer2Frame); xmlstreamwriter.writeAttribute("points", mPlayer2Point); xmlstreamwriter.writeAttribute("break", mPlayer2ContinuePoint); xmlstreamwriter.writeEndElement(); xmlstreamwriter.writeEndElement(); xmlstreamwriter.writeEndElement(); xmlstreamwriter.writeEndDocument();
结果如下
<?xml version="1.0" encoding="UTF-8"?> <xml> <round id="" tableid="1"> <player1 framescore="" points="" break="">Hello World</player1> <player2 framescore="" points="" break=""/> </round> </xml>