Re: boost attribute question
I would assume you can't since that isn't even valid xml.
you cant have the '=1' attached to the element name
you aren't closing the vector element
with valid xml it would be either (with attribute, note that attributes are strings)
Code:
<TextCoords>
<vector index="1">
<x>1</x>
<y>1</y>
</vector>
<vector index="2">
<x>1</x>
<y>0</y>
</vector>
<TextCoords>
or with an extra index element
Code:
<TextCoords>
<vector>
<index>1</index>
<x>1</x>
<y>1</y>
</vector>
<vector>
<index>2</index>
<x>1</x>
<y>0</y>
</vector>
<TextCoords>
Re: boost attribute question
Hello OReubens,
Yes, I'd like to see the first structure to happen in my xml. How do I re-code my snippet in order to
achieve that pattern?
Thanks
Jack
Re: boost attribute question
the easiest way is to construct the xml directly, off the top of my head ( not tested ):
Code:
void stick(wptree& tree, std::wstring location, Point2 p, std::size_t id )
{
std::wstringstream xml;
if( xml << _T("<vector index=\"") << id << _T("\">")
_T("<x>") << p.x << _T("</x>")
_T("<y>") << p.y << _T("</y>")
_T("</vector>") )
{
wptree xml_tree;
read_xml( xml, xml_tree ); // note: this can throw
tree.add_child( location, xml_tree ); // supposing a correct location
}
// else <some error condition>
}
otherwise, you can create each element manually. Note that attributes and comments needs a special "path" to be interpreted by the xml parser as such. For example, the path of the attribute 'b' in "<a b='0'/>" is "a.<xmlattr>.b".
Re: boost attribute question
Hi superbonzo,
Wow, it does work more easily than building it manually.
It saves a lot of time from constructing everything from scratch.
Thanks
Jack
Re: boost attribute question
just as a side note.
streaming into a string(stream) is a VERY simplistic view on how to create an XML.
it'll work for this particular case, but it won't for many other cases (if you need to handle encodings, namespaces, strings with characters not supported by your character set, ...).
that's why you have libraries to help you create xml "the right way" (XML dom, SAX, XmlLite, and many others)
Re: boost attribute question
sure, but boost.property_tree indeed has only VERY simplistic xml support ( it uses streams internally for everything, has no xmlencoding support, no entities etc ... ), and the OP used it from the very beginning. So, it's ( hopefully :) ) safe to assume that he just doesn't want a "true" xml in the first place.
Re: boost attribute question
Hello both of you,
I now have a problem parsing it back into the application program.
BOOST_FOREACH doesn't support wstring.
If I parse it with ptree, I am afraid foreign characters will not be recognized....
Any ideas?
Thanks