boost attribute question
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: boost attribute question

  1. #1
    Join Date
    Dec 2010
    Posts
    634

    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>

  2. #2
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,966

    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>

  3. #3
    Join Date
    Dec 2010
    Posts
    634

    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

  4. #4
    Join Date
    Oct 2008
    Posts
    1,146

    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".

  5. #5
    Join Date
    Dec 2010
    Posts
    634

    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

  6. #6
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,966

    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)

  7. #7
    Join Date
    Oct 2008
    Posts
    1,146

    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 07:40 AM.

  8. #8
    Join Date
    Dec 2010
    Posts
    634

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center