-
February 3rd, 2014, 11:51 PM
#1
boost attribute question
Code:
/* Append a Point2 value to
* the tree
*/
void stick(wptree& tree, std::wstring location, Point2 p)
{
MCHAR finalLocations[100];
std::wstring alocation(_T(".vector"));
//wptree& ret = tree.put(location, "");
for (int i = 0; i < 2; i++)
{
float f;
wcscpy(finalLocations, location.c_str());
wcscat(finalLocations, alocation.c_str());
switch (i)
{
case 0:
wcscat(finalLocations, _T(".x"));
f = p.x;
break;
case 1:
wcscat(finalLocations, _T(".y"));
f = p.y;
break;
}
tree.add(finalLocations, f);
}
//return ret;
}
Code:
<TexCoords>
<vector>
<x>1</x>
<y>1</y>
<x>1</x>
<y>0</y>
How can I make the xml look like this
Code:
<TexCoords>
<vector=1>
<x>1</x>
<y>1</y>
<vector=2>
<x>1</x>
<y>0</y>
-
February 4th, 2014, 07:59 AM
#2
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>
-
February 5th, 2014, 01:17 AM
#3
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
-
February 5th, 2014, 03:19 AM
#4
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".
-
February 5th, 2014, 05:36 AM
#5
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
-
February 5th, 2014, 08:02 AM
#6
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)
-
February 5th, 2014, 08:37 AM
#7
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.
Last edited by superbonzo; February 5th, 2014 at 08:40 AM.
-
February 5th, 2014, 03:08 PM
#8
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|