- Si te fijas lo has estado haciendo bien, Sibling es Hermano o Campañero, pRoot no tiene ningún hermano o compañero, esto lo tienes que hacer sobre pBodys tal como lo vienes haciendo con pParm = pParms->FirstChildElement("Input");
- Usa: pBodys = pBodys->NextSiblingElement("Test");
- También corrige: (int)count->Attribute("cont") en tu FOR, esto devuelve un Puntero y lo que conviertes a INT es el Puntero, no el valor. Mejor usa: count->IntAttribute("cont") en su lugar:
Código
#include <iostream> #include "tinyxml2.cpp" #include "tinyxml2.h" using namespace std; using namespace tinyxml2; int main() { XMLDocument doc; doc.LoadFile("example.xml"); XMLElement *pRoot, *pBodys, *pParms, *pParms2, *pProcess, *pApp, *pLineFormat, *pParm, *pParm2, *count; pRoot = doc.FirstChildElement("Tests"); if (pRoot) { count = pRoot->FirstChildElement("count"); cout << "cont = " << count->Attribute("cont") << endl; pBodys = pRoot->FirstChildElement("Test"); for (int i = 0; i < count->IntAttribute("cont"); i++) { cout << "id = " << pBodys->Attribute("id") << endl; if (pBodys) { pParms = pBodys->FirstChildElement("Inputs"); if (pParms) { pParm = pParms->FirstChildElement("Input"); while (pParm) { cout << "port = " << pParm->Attribute("port") << " "; cout << "value = " << pParm->Attribute("value") << endl; pParm = pParm->NextSiblingElement("Input"); } } pParms2 = pBodys->FirstChildElement("Outputs"); if (pParms2) { pParm2 = pParms2->FirstChildElement("Output"); while (pParm2) { cout << "port = " << pParm2->Attribute("port") << " "; cout << "value = " << pParm2->Attribute("value") << endl; pParm2 = pParm2->NextSiblingElement("Output"); } } } pBodys = pBodys->NextSiblingElement("Test"); } } return 0; }
Código
<Tests> <count cont="2"></count> <Test id="test0"> <Inputs> <Input port="A" value="1" /> <Input port="B" value="4.56" /> <Input port="C" value="7" /> </Inputs> <Outputs> <Output port="D" value="10" /> </Outputs> </Test> <Test id="test1"> <Inputs> <Input port="K" value="3" /> <Input port="L" value="9.56" /> </Inputs> <Outputs> <Output port="P" value="6" /> </Outputs> </Test> </Tests>
Código
C:\Users\EdSon\Desktop>g++ xmlreader.cpp -o xmlreader.exe && xmlreader.exe cont = 2 id = test0 port = A value = 1 port = B value = 4.56 port = C value = 7 port = D value = 10 id = test1 port = K value = 3 port = L value = 9.56 port = P value = 6 C:\Users\EdSon\Desktop>
- Documentación TinyXML-2: http://leethomason.github.io/tinyxml2/classtinyxml2_1_1_x_m_l_element.html