C - 如何存储XML字符串并保持格式化?

时间:2016-09-12 06:14:37

标签: c xml formatting printf string-formatting

我有以下字符串,我需要保持格式,缩进,换行等。任何想法如何做?

char *myXMLString = 
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<painting>"
"   <img src=\"madonna.jpg\" alt=\"Foligno Madonna, by Raphael\"/>"
"   <caption>MY_REPLACED_TEXT"
"   <date>1511</date>-<date>1512</date>.</caption>"
"</painting>";

printf只提供一个长文本。

1 个答案:

答案 0 :(得分:3)

在您认为合适的地方添加"\n",例如:

char *myXMLString = 
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" "\n"
"<painting>\n"
"   <img src=\"madonna.jpg\" alt=\"Foligno Madonna, by Raphael\"/>" "\n"
"   <caption>MY_REPLACED_TEXT\n"
"   <date>1511</date>-<date>1512</date>.</caption>\n"
"</painting>\n";