2016-11-05 3 views
-1
그래서

TinyXML 캔트 내 파일을로드

TiXmlDocument doc; 
TiXmlDeclaration* decl = new TiXmlDeclaration("1.0", "", ""); 
doc.LinkEndChild(decl); 
TiXmlElement* cm = new TiXmlElement("CocktailMachine"); 
doc.LinkEndChild(cm); 

TiXmlElement* disp = new TiXmlElement("dispensers"); 
cm->LinkEndChild(disp); 
TiXmlElement* cock = new TiXmlElement("cocktails"); 
cm->LinkEndChild(cock); 
TiXmlElement* ingr = new TiXmlElement("ingredients"); 
cm->LinkEndChild(ingr); 






for (size_t i = 0; i < dispensers.size(); i++) 
{ 
    stringstream ss; 
    ss << i; 
    string s = ss.str(); 
    TiXmlElement* x_disp = new TiXmlElement(s.c_str()); 
    disp->LinkEndChild(x_disp); 
    x_disp->SetAttribute("number", dispensers.at(i)->get_number()); 
    if (dispensers.at(i)->get_ingredient() == NULL) { 

     x_disp->SetAttribute("ingredient", "NULL"); 

    } 
    else { 


     x_disp->SetAttribute("ingredient", dispensers.at(i)->get_ingredient()->get_name().c_str()); 

    } 

} 


if (cocktails.size() != 0) 
    for (size_t i = 0; i < cocktails.size(); i++) 
    { 


     stringstream ss; 
     ss << i; 
     string s = ss.str(); 
     TiXmlElement* ct = new TiXmlElement(s.c_str()); 
     cock->LinkEndChild(ct); 
     ct->SetAttribute("name", cocktails.at(i)->get_name().c_str()); 

     for (size_t j = 0; j < cocktails.at(i)->get_ingredients().size(); j++) 
     { 

      stringstream ss; 
      ss << j; 
      string s = ss.str(); 
      TiXmlElement* ct_s = new TiXmlElement(s.c_str()); 
      ct->LinkEndChild(ct_s); 

      ct_s->SetAttribute("ingredient", cocktails.at(i)->get_ingredients().at(j)->get_name().c_str()); 
     } 

    } 



if (ingredients.size() != 0) 
    for (size_t i = 0; i < ingredients.size(); i++) 
    { 
     stringstream ss; 
     ss << i; 
     string s = ss.str(); 
     TiXmlElement* x_ingr = new TiXmlElement(s.c_str()); 
     ingr->LinkEndChild(x_ingr); 
     x_ingr->SetAttribute("name", ingredients.at(i)->get_name().c_str()); 

    } 


doc.SaveFile("CM.xml"); 
doc.Clear(); 

이이 CM.xml

에 몇 가지 물건을 저장 한

가 몇 가지 물건을 저장했다 대학에 대한 몇 가지 물건을 노력하고 필자와 내가 할 tinyxml를 선택

<?xml version="1.0" ?> 
 
<CocktailMachine> 
 
    <dispensers> 
 
     <0 number="1" ingredient="NULL" /> 
 
     <1 number="2" ingredient="NULL" /> 
 
     <2 number="3" ingredient="NULL" /> 
 
     <3 number="4" ingredient="NULL" /> 
 
     <4 number="5" ingredient="NULL" /> 
 
     <5 number="6" ingredient="NULL" /> 
 
    </dispensers> 
 
    <cocktails> 
 
     <0 name="Tequila Sunrise"> 
 
      <0 ingredient="Tequila" /> 
 
      <1 ingredient="Orange juice" /> 
 
      <2 ingredient="Grenadine" /> 
 
     </0> 
 
     <1 name="tq2"> 
 
      <0 ingredient="Tequila" /> 
 
      <1 ingredient="Orange juice" /> 
 
      <2 ingredient="Grenadine" /> 
 
     </1> 
 
    </cocktails> 
 
    <ingredients /> 
 
</CocktailMachine>
(코드가 아닌 내가 알고 있지만 이것은 XML을 게시 할 수있는 유일한 방법이었다)

내가 잘

<?xml version="1.0" ?> 
 
<Hello>World</Hello>

를 작동하고 항상

거짓이 "helloworld.xml"를로드하려고 반환이

TiXmlDocument* doc = new TiXmlDocument("example1.xml"); 
if (!doc->LoadFile()) 
    cout << "false"; 

으로로드하려고 할 때

그래서 내 xml에 문제가 있습니까? <0 number="1" ingredient="NULL" />

당신은 태그 이름과 번호를 가질 수 없습니다 : 여기 Jonny0815

+1

'0'은 유효한 XML 이름이 아니므로 텍스트가 올바른 형식의 XML 문서가 아닙니다. –

답변

1

사전 에서

감사는 문제입니다.

"0 유효한 XML 이름이 아닙니다"좋은 주말

OT가 D : 여기에 새로운 ... 내가 같은 질문을 표시 할 수 있습니다에게

0

정말 고마워요 내 문제들을 해결 "답변"또는 "완료"하거나 가까운 요청을 열어서 모든 사람들이 명령을받습니다.