开发者

storing xml to a string

开发者 https://www.devze.com 2023-02-26 11:00 出处:网络
I was trying to store an xml to a string.Is that really doable in C++?I am getting a copile error: error: parse error before string constant

I was trying to store an xml to a string.Is that really doable in C++?I am getting a copile error:

error: parse error before string constant

when trying to write a line of code as following :

string xmlString = "<animalList dnPrefix="String">
  <node>                           开发者_开发问答               
    <animal ID="xxxxxxxx">            
      <Status>managed</Status>     
      <Type>ENB</Type>                  
      <Subtype>0</Subtype>
      <Version1>0.0</Version1>
      <Version2>0.0</Version2>
      <Name>ChicagoWest5678</Name>
      <LocalName>ChicagoWest5678</LocalName>
    </animal>
    <animal ID ="yyyyyy">
      <Status>managed</Status>     
      <Type>ENB</Type>                  
      <Subtype>0</Subtype>
      <Version1>0.0</Version1>
      <Version2>0.0</Version2>
      <Name>ChicagoWest5678</Name>
      <LocalName>ChicagoWest5678</LocalName>
    </animal> 
  </node>
</animalList> ";

is there any other way other than saving it to a file..?can't i directly store it into a string ..Please do help me friends...


You have to escape the " characters and the newline characters.

Something like:

std::string str = "bryan says: \"quoted text\" :) \n\
other line";

Which will give you the following string:

bryan says: "quoted text" :)
other line

Also note that if you intend to store specific utf-8 or Latin1 characters in your string, the encoding of your source file must be set accordingly.

In most cases, it is generally easier to store your xml file separately and to include it as a resource of your program later. This will make your code much more readable and will ease modifications on the xml structure if ever needed.

In this case also beware, because std::string has no specific support for those characters and length() might give unwanted results.


You have to esacpe all the " characters with a backslash (\). You will have to escape the newline characters with a \ as well.

Your code would look like:

std::string xmlString = "<animalList dnPrefix=\"String\">\
<node>                                        \
<animal ID=\"xxxxxxxx\">            \
    <Status>managed</Status>     \
    <Type>ENB</Type>                  \
    <Subtype>0</Subtype>\
    <Version1>0.0</Version1>\
    <Version2>0.0</Version2>                                       <Name>ChicagoWest5678</Name>\
    <LocalName>ChicagoWest5678</LocalName>\
\
</animal>\
                    <animal ID =\"yyyyyy\">\
                    <Status>managed</Status>     \
    <Type>ENB</Type>                  \
    <Subtype>0</Subtype>\
    <Version1>0.0</Version1>\
    <Version2>0.0</Version2>                                      <Name>ChicagoWest5678</Name>\
    <LocalName>ChicagoWest5678</LocalName>\
         </animal> \
             </node>\
</animalList> " ;


As has been pointed out, you need to escape quotes, and use "\n" for new lines. I would add that adjacent string literals concatenate, so you can break the string down into nicely formatted lines:

std::string xmlString =
    "<animalList dnPrefix=\"String\">\n"
    "  <node>\n"                                          
    "    <animal ID=\"xxxxxxxx\">\n"
    //  ...            
    "  </node>\n"
    "</animalList>\n";
0

精彩评论

暂无评论...
验证码 换一张
取 消