2014-03-19 2 views
1

yaml 파일을 구문 분석하는 동안 전체 노드 객체를 가져 오려고합니다. 내 YAML 데이터 나 데이터를 얻기 위해 노력하고 있어요 방법은부모 yaml 노드에서 완전한 yaml 노드 자식 개체를 얻는 방법?

YAML::Node parentNode = YAML::LoadFile(abc.yaml); // My yaml file name is abc 

    if(!parentNode.IsNull()) 
    { 
     if(parentNode.IsMap()) 
     { 
      YAML::iterator it = parentNode.begin(); 
      std::cout << "Parent is " << it->first.as<std::string>() << std:endl; // Parent 
      if(it->second.IsScalar()) 
      { 
      } 
      else if(it->second.IsMap()) 
      { 
       YAML::Node rootChild = it->second; 
       YAML::iterator chilItr = rootChild.begin(); 
       std::cout << "Child count is " << chilItr->second.size() << std:endl; // 3 
       while(chilItr != rootChild.end()) 
       { 
        YAML::Node child = *chilItr; 
        if(child.IsMap()) //This causes exceetion 
        { 
         YAML::iterator ChildIterator = Child.begin(); 
         std::cout << " Child is " << ChildIterator->first.as<std::string>() << std::endl; 
        } 
        chilItr++; 
       } 
      } 
     } 
    } 

child.IsMap() 예외를 던지고있다

--- 
Parent: 
    Child1: ABC 
    Child2: 
    Subchild1: 123 
    Subchild2: 456 
    Child3: XYZ 
... 

아래 데이터

과 비슷한 것 같습니다? 기본적으로 요구 사항은 부모 객체에서 자식 YAML 노드 객체를 어떻게 잡아낼 수 있습니까?

답변

0

예를 들어 3의 사용자 의견이 인쇄되지 않을 것이므로 정확한 YAML 파일 또는 정확한 코드를 붙여 넣은 것으로 생각하지 않습니다. 여기에 YAML 파일 예제를 사용하여 기본적인 아이디어가 있습니다.

YAML::Node parentNode = YAML::LoadFile("abc.yaml"); 
if (parentNode.IsMap()) { 
    YAML::iterator it = parentNode.begin(); 
    YAML::Node key = it->first; 
    YAML::Node child = it->second; 
    std::cout << "Parent is " << key.as<std::string>() << "\n"; // Parent 
    if(child.IsMap()) { 
    std::cout << "Child count is " << child.size() << "\n"; // 3 

    // Now that you've got a map, you can iterate through it: 
    for (auto it = child.begin(); it != child.end(); ++it) { 
     YAML::Node childKey = it->first; 
     YAML::Node childValue = it->second; 

     // Now you can check the childValue object 
     if(childValue.IsMap()) { 
     // Iterate through it, if you like: 
     for (auto it = childValue.begin(); it != childValue.end(); ++it) { 
      // ... 
     } 
     } 
    } 
    } 
} 
+0

죄송합니다. 방금 요구 사항 요약으로 코드를 입력했습니다. 실제 코드가 아닙니다. –

+0

실제로 키와 값을 모두 가진 자식 노드를 원한다면 값을 보내는 대신 다른 노드로 노드를 전달할 수 있습니다. 반복기에서 키와 값을 가진 새로운 노드를 생성하지 않고 키와 값을 모두 참조로 자식을 전달합니다. –

+0

실제로 키와 값이 둘 다있는 자식 노드가 필요하므로 노드의 참조를 값만 보내는 대신 다른 함수로 전달할 수 있습니다. iterator에서 key와 value를 가진 새로운 노드를 생성하고 싶지는 않지만 노드의 참조를 전달한다. –