2013-04-21 5 views
6

부스트의 속성 트리를 사용하여 XML을 읽고 쓰고 있습니다. 스프레드 시트 응용 프로그램을 사용하여 스프레드 시트의 내용을 XML로 저장하려고했습니다.동일한 키를 가진 노드를 속성 트리에 추가

int main(int argc, char const *argv[]) 
{ 
boost::property_tree::ptree pt; 

pt.put("spreadsheet.cell.name", "a2"); 
pt.put("spreadsheet.cell.contents", "adsf"); 

write_xml("output.xml", pt); 

boost::property_tree::ptree ptr; 
read_xml("output.xml", ptr); 

ptr.put("spreadsheet.cell.name", "d6"); 
ptr.put("spreadsheet.cell.contents", "345"); 
ptr.put("spreadsheet.cell.name", "d2"); 
ptr.put("spreadsheet.cell.contents", "=d6"); 

write_xml("output2.xml", ptr); 

return 0; 
} 

나는 put 방법은 대체를 참조하십시오이 question을 바탕으로 : 내가 쓴 간단한 테스트 프로그램의

<?xml version="1.0" encoding="UTF-8"?> 
<spreadsheet> 
    <cell> 
     <name>A2</name> 
     <contents>adsf</contents> 
    </cell> 
    <cell> 
     <name>D6</name> 
     <contents>345</contents> 
    </cell> 
    <cell> 
     <name>D2</name> 
     <contents>=d6</contents> 
    </cell> 
</spreadsheet> 

이 그래서는 XML에 대해 다음 형식을 사용하도록 요구하고 학교 과제입니다 새로운 노드를 추가하는 대신 해당 노드의 노드. 정확히 내가보고하고있는 기능입니다 :

Output.xml

<?xml version="1.0" encoding="utf-8"?> 
<spreadsheet> 
    <cell> 
    <name>a2</name> 
    <contents>adsf</contents> 
    </cell> 
</spreadsheet> 

Output2.xml

<?xml version="1.0" encoding="utf-8"?> 
<spreadsheet> 
    <cell> 
    <name>d2</name> 
    <contents>=d6</contents> 
    </cell> 
</spreadsheet> 

나는이 add_child 방법을 참조 documentation에서 찾고있는 것이다 Add the node at the given path. Create any missing parents. If there already is a node at the path, add another one with the same key.

난 그 방법을 사용하는 방법을 알아낼 수 없다, 누군가가 그것을 사용하는 방법을 설명 할 수 있을까?

원하는 파일 형식을 얻으려면이 문제를 해결하는 더 좋은 방법이 있습니까?

+0

방금 ​​자식 이름으로 셀 이름을 사용할 수 없습니다? 즉, "spreadsheet.cell.d6" ' –

+0

@ k-ballo는 XML 요구 사항을 충족하지 못하기 때문입니다. – Deekor

답변

15

구성원 기능 을 자식 노드로 다른 노드의 DOM에 삽입 할 수 있습니다. 제공하는 키 경로가 이미 존재하면 중복 키가 추가되고 대신 하위 키가 삽입됩니다. 우리가 예제를 조금 변경하면 결과를 검토 할 수 있습니다. 처음 add_child를 호출 할 때

#include <boost/property_tree/ptree.hpp> 
#include <boost/property_tree/xml_parser.hpp> 

int main() 
{ 
    // Create the first tree with two elements, name and contents 
    boost::property_tree::ptree ptr1; 
    ptr1.put("name", "a2"); 
    ptr1.put("contents", "adsf"); 

    // Create the a second tree with two elements, name and contents 
    boost::property_tree::ptree ptr2; 
    ptr2.put("name", "d6"); 
    ptr2.put("contents", "345"); 

    // Add both trees to a third and place them in node "spreadsheet.cell" 
    boost::property_tree::ptree ptr3; 
    ptr3.add_child("spreadsheet.cell", ptr1); 
    ptr3.add_child("spreadsheet.cell", ptr2); 

    boost::property_tree::write_xml("output.xml", ptr3); 

    return 0; 
} 

는 키 "spreadsheet.cell"에 대한 노드가 존재하지 않고 생성된다. 그런 다음 트리의 내용 (namecontents)을 새로 만든 노드에 추가합니다. add_child을 호출하면 두 번째로 "spreadsheet.cell"이 이미 있지만 "put"과 달리 "셀"이라고도하는 형제 노드를 만들어 같은 위치에 삽입합니다.

최종 출력 :

<?xml version="1.0" encoding="utf-8"?> 
<spreadsheet> 
    <cell> 
    <name>a2</name> 
    <contents>adsf</contents> 
    </cell> 
    <cell> 
    <name>d6</name> 
    <contents>345</contents> 
    </cell> 
</spreadsheet> 
+0

대단한 설명. 그러나 만약'd6' 셀을 업데이트하고 싶다면'ptr3'에서 그 셀로 이동하여 내용을 어떻게 바꿀 수 있습니까? – Deekor

+2

@Deekor '스프레드 시트'의 자식을 반복하여 '셀'형식의 모든 자식을 찾습니다. 언제든지 'name'의 내용을 알아 내고 일치하는 것을 발견하면 그것을 삭제하십시오. –

+0

세부 사항을 보려면 문서를 살펴볼 필요가 있습니다. 그렇지 않으면 net의 add_child 관련 문서가 많지 않습니다. – sb32134

관련 문제