Menu

Adding std::string inside element

2006-04-10
2013-05-20
  • Joe Van Dyk

    Joe Van Dyk - 2006-04-10

    Hi,

    I've got a string that looks like:
    "<xml /> <more_xml />".

    I want to insert that inside a TiXmlElement.  How can I do that?  I can't figure out a way to correctly parse it, as there's two (or more) root elements in the string.

     
    • Joe Van Dyk

      Joe Van Dyk - 2006-04-10

      So, if I had an existing document that looked like this:

      <xml>
        <element>
        </element>
      </xml>

      I'd want to insert the string so the document looks like:

      <xml>
        <element>
          <xml /> <more_xml />
        </element>
      </xml>

       
    • Joe Van Dyk

      Joe Van Dyk - 2006-04-10

      Aha, I can create a TiXmlText object and insert that into the element.  However, quoting then becomes an issue, because all the '<' and '&' and '>' characters in the string are converted to '&lt;', etc.

      Any ideas?

       
    • Lee Thomason

      Lee Thomason - 2006-04-10

      Assuming you have a TiXmlElement* to <element>, you would want to do something like:

      element->LinkEndChild( new TiXmlElement( "xml" ));
      element->LinkEndChild( new TiXmlElement( "more_xml" ));

      ...because you want to insert new *elements*, not new text. If you are generating those elements from text, you can always:

      TiXmlElement* fooElement = new TiXmlElement( "foo" );
      foo->Parse( "<more_xml/>";
      element->LinkEndChild( fooElement );

      Noting that "xml" is a reserved name, and you shouldn't use it.

      hope that helps!
      lee

       
    • Joe Van Dyk

      Joe Van Dyk - 2006-04-10

      Thanks for the response.

      The problem is that I have a string that contains multiple xml elements.  They're not broken up into separate strings, so I can't do:

      std::string the_input_string = "<an_element /> <another_element />";
      foo->Parse(the_input_string);

      right?

      I know my life would be a lot easier if I had separate strings for each element.

       
    • Joe Van Dyk

      Joe Van Dyk - 2006-04-10

      What I'd really like is some function that would return a vector of TiXmlElements given a string that contains multiple root elements.

      Then I could insert each one of those elements into the root element.

       
    • Lee Thomason

      Lee Thomason - 2006-04-10

      Actually, if you used:

      TiXmlDocument xml;
      xml.Parse( "<an_element /> <another_element />" );

      You could then add the child elements of 'xml' to another XmlDocument, which I think gets you the behavior you want.

      A vector if TiXmlElements isn't going to happen - it's a little too specialized. :)

      lee

       
      • Joe Van Dyk

        Joe Van Dyk - 2006-04-10

        I thought a document could only have one root element (and there's two in that string).

         
    • Lee Thomason

      Lee Thomason - 2006-04-10

      TinyXML errs towards permissiveness - including allowing more than one root element. If taking advantage of a loophole in TinyXML bothers you (and maybe it should in case we tighten it down in the future) you could wrap the elements you want to parse in a root elment by prepending a <root> and postpending a </root>.

      lee

       
    • Joe Van Dyk

      Joe Van Dyk - 2006-04-10

      Oh.... I see now.  I didn't know that a TiXlDocument could have multiple children.  I thought it was merely tolerant (i.e. ignores) any additional root elements in the document.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.