Archived Forum Post

Index of archived forum posts

Question:

AddChildTree + GetChild - return only odd node. Bag in function GetChild?

Aug 12 '13 at 21:27

Hi. Please help me. GetChild - return only odd nodes. All OK when comment line: xmlC->AddChildTree( *child )

    int testAddChildTree()
    {
        CkXml xmlSource;    
        xmlSource.LoadXml( "<a><b>1</b><b>2</b><b>3</b><b>4</b>\
                            <b>5</b><b>6</b><b>7</b><b>8</b></a>");
        xmlSource.GetRoot2();

        CkXml xmlDest;
        xmlDest.put_Tag("a");
        CkXml* xmlC = xmlDest.NewChild("C","");

        int iCount = xmlSource.get_NumChildren();
        printf("get_NumChildren: %d\n", iCount);

        for (int i = 0; i < iCount; i++ )
        {
            CkXml* child = xmlSource.GetChild(i);
            if (child)
            {
                printf("%d: %s\n", i, child->content() );
                xmlC->AddChildTree( *child );
                delete child;
            }
        }
        delete xmlC;

        printf("%s\n", xmlDest.xml());

        return 0;
    }

Output:

get_NumChildren: 8
0: 1
1: 3
2: 5
3: 7
<?xml version="1.0" encoding="utf-8" ?>
<a>
    <C>
        <b>1</b>
        <b>3</b>
        <b>5</b>
        <b>7</b>
    </C>
</a>

Answer

What happens if you do not delete the child while looping through the XMLSource?