- Dec 30, 2002
- 5,747
- 1
- 81
I am rewriting a tool that outputs xml files. Unfortunately due to the nature of the industry I'm in the output needs to be human readable through programs like notepad. This is being written in Visual C++ 6. Does anyone have some tips about outputting "pretty xml"?
More specifically, I want the ability to specify formatting structures. For example I want to be able to output a list in both of these manners:
<DataList> A B C D E </DataList>
- or -
<DataList>
A
B
C
D
E
</DataList>
or the ability to specify that a certain type of element gets written in a specific manner. For example I want to be able to specify that all "DataNode" elements get formatted like this:
<DataNode>
<Name>Blah</Name>
<Value>2</Value> <Index>12</Index> <Age>52</Age>
<Value>43</Value> <Index>93</Index> <Age>36</Age>
<Value>2</Value> <Index>4</Index> <Age>18</Age>
<Value>-1</Value> <Index>50</Index> <Age>32</Age>
</DataNode>
Instead of this:
<DataNode>
<Name>Blah</Name>
<Value>2</Value>
<Index>12</Index>
<Age>52</Age>
<Value>43</Value>
<Index>93</Index>
<Age>36</Age>
<Value>2</Value>
<Index>4</Index>
<Age>18</Age>
<Value>-1</Value>
<Index>50</Index>
<Age>32</Age>
</DataNode>
(With proper tabbing of course.)
The old way we did this was basically a hack job. You can see the limitations of the below code. It came out formatted nicely, but we are starting to run up against its limitations.
What I've written so far is something that builds an XML tree. The current operator<< function does one of two things. If the element contains only text it is written like this:
<element>text</element>
And if the element contains more elements it is written like this:
<element>
--more elements--
</element>
This doesn't give me much control over the formatting
I've already got the indentation problem figured out (ie, things are always tabbed properly with respect to their parent elements), but the formatting I'm not so sure about. Really all I need it to be able to specify the whitespace around tags. spaces, tabs and newlines.
So I think I've gotten the problem fully outlined. Any suggestions?
More specifically, I want the ability to specify formatting structures. For example I want to be able to output a list in both of these manners:
<DataList> A B C D E </DataList>
- or -
<DataList>
A
B
C
D
E
</DataList>
or the ability to specify that a certain type of element gets written in a specific manner. For example I want to be able to specify that all "DataNode" elements get formatted like this:
<DataNode>
<Name>Blah</Name>
<Value>2</Value> <Index>12</Index> <Age>52</Age>
<Value>43</Value> <Index>93</Index> <Age>36</Age>
<Value>2</Value> <Index>4</Index> <Age>18</Age>
<Value>-1</Value> <Index>50</Index> <Age>32</Age>
</DataNode>
Instead of this:
<DataNode>
<Name>Blah</Name>
<Value>2</Value>
<Index>12</Index>
<Age>52</Age>
<Value>43</Value>
<Index>93</Index>
<Age>36</Age>
<Value>2</Value>
<Index>4</Index>
<Age>18</Age>
<Value>-1</Value>
<Index>50</Index>
<Age>32</Age>
</DataNode>
(With proper tabbing of course.)
The old way we did this was basically a hack job. You can see the limitations of the below code. It came out formatted nicely, but we are starting to run up against its limitations.
Code edited so I don't get fired.
-- snip --
fout<<"\t\t\t\t\t<tag1>\x0A";
fout<<"\t\t\t\t\t\t<tag2>\x0A";
fout<<"\t\t\t\t\t\t\t";
for (i=0;i<count;i++)
{
fout<<var1[k][ i]<<"\t";
}
fout<<"\x0A";
fout<<"\t\t\t\t\t\t</tag2>\x0A\x0A";
fout<<"\t\t\t\t\t\t<tag3>\t"<<k+var2+var3+1<<"\t</tag3>\x0A";
fout<<"\t\t\t\t\t\t<tag4>\tvar4\t</tag4>\x0A";
-- snip --
What I've written so far is something that builds an XML tree. The current operator<< function does one of two things. If the element contains only text it is written like this:
<element>text</element>
And if the element contains more elements it is written like this:
<element>
--more elements--
</element>
This doesn't give me much control over the formatting
I've already got the indentation problem figured out (ie, things are always tabbed properly with respect to their parent elements), but the formatting I'm not so sure about. Really all I need it to be able to specify the whitespace around tags. spaces, tabs and newlines.
So I think I've gotten the problem fully outlined. Any suggestions?
