Hi
I have basic XML file:
<?xml version="1.0" encoding="UTF-8"?>
<Reminders>
<W20190408>xxxx</W20190408>
<W20190624>yyyy</W20190624>
</Reminders>
I want to be able to support multi-line for these values. What is the simplest way to cater for that with XML?
I was going to use a 卢 character and then swap it as a \r\n escape. I tried writing &X1 into the text but it ended us as literal & etc text in the file.
What is the right way?
What about CDATA?
<![CDATA[ Text
With new line
]]>
Thanks for the suggestion @comargo . I found that CDATA
will also take into account the whitespace before the text on each line.Since I like the content formatted nicely in the file it seemes messy with the wrapped text right over on the left edge of the file. Does that make sense?
The XML file is for computer, not for human. So far the pretty-printing is not the goal.
@comargo Valid point. I guess I have in mind the way that I like my "code" presented. But I take on board what you say.
I have not actually worked with CDATA
in tinyxml2 before. What is the right way to read and write it? At the moment I have:
for (RemindersList::const_iterator iterReminder = m_mapReminders.begin();
iterReminder != m_mapReminders.end();
++iterReminder)
{
CReminder rReminder = iterReminder->second;
// Create reminder node
XMLElement *pReminder = m_docReminders.NewElement(CT2CA(rReminder.GetKey(), CP_UTF8));
pReminder->SetText(CT2CA(rReminder.GetReminder(), CP_UTF8));
// Append to XML document
pReminders->InsertEndChild(pReminder);
}
I didn't tried my self yet, but if you'll take a search tool for 'CDATA' keyword in xmltest.cpp file there are some blocks about it.
I had to change it like this @comargo 馃憤
// Iterate the reminders
for (RemindersList::const_iterator iterReminder = m_mapReminders.begin();
iterReminder != m_mapReminders.end();
++iterReminder)
{
CReminder rReminder = iterReminder->second;
// Create reminder node
XMLElement *pReminder = m_docReminders.NewElement(CT2CA(rReminder.GetKey(), CP_UTF8));
XMLText *pText = m_docReminders.NewText(CT2CA(rReminder.GetReminder(), CP_UTF8));
pText->SetCData(true);
pReminder->InsertEndChild(pText);
// Append to XML document
pReminders->InsertEndChild(pReminder);
}
However, note that tintyxml2 expects the newline constants to be \n
. For my CEdit
controls they have to be \r\n
. So before I commit the string to / from the XML I have to swap the constants.
Is there a setting in tinyxml2 to cater for this? I am surprised I am not allowed to just use \r\n
.
Please advise.
@ajtruckle as you can see in source (tinyxml2.cpp line 1147) - there is no settings for it... I can't tell, what is the correct behavior.
@ajtruckle as I see, there are no common understanding for correct behavior: https://stackoverflow.com/questions/15016004/whats-the-correct-way-to-encode-cr-lf-line-breaks-in-text-xml-values
But there is a common solution for such task: encode CR-LF sequence with
pair of entities....
@ajtruckle Here is one more answer for question: https://stackoverflow.com/questions/2817378/does-the-xml-specification-states-that-parser-need-to-convert-n-r-to-n-always
Most helpful comment
The XML file is for computer, not for human. So far the pretty-printing is not the goal.