Título: tinyXML c++
Publicado por: statham en 31 Enero 2019, 12:40 pm
Quiero leer un archivo xml donde se encuentran varias pruebas, pero siempre obtengo la primera y no sale del bucle. Si hago una iteración bien, pero cuando hago el bucle, no puedo pasar a la siguiente prueba.
Y si uso // pBodys = pRoot-> NextSiblingElement ("Test"); saltA un error en la iteración 2, (https://i.gyazo.com/9a108bf422299b66abfe91127668a63c.png) si no lo uso, permanece en un bucle infinito
(https://i.gyazo.com/133be25514a8a000fce87e2fc7cc52ad.png)
No puedo seguir adelante. un saludo
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"); std::cout << "cont=" << count->Attribute("cont") << std::endl; pBodys = pRoot->FirstChildElement("Test"); //for (int i = 0; i < (int)count->Attribute("cont"); i++) {
std::cout << "id=" << pBodys->Attribute("id") << std::endl; if (pBodys) { pParms = pBodys->FirstChildElement("Inputs"); if (pParms) { pParm = pParms->FirstChildElement("Input"); while (pParm) {
std::cout << "port=" << pParm->Attribute("port") << " "; std::cout << "value=" << pParm->Attribute("value") << std::endl;
pParm = pParm->NextSiblingElement("Input"); } } pParms2 = pBodys->FirstChildElement("Outputs"); if (pParms2) { pParm2 = pParms2->FirstChildElement("Output"); while (pParm2) {
std::cout << "port=" << pParm2->Attribute("port") << " "; std::cout << "value=" << pParm2->Attribute("value") << std::endl;
pParm2 = pParm2->NextSiblingElement("Output"); } }
}
//pBodys = pRoot->NextSiblingElement("Test"); //} }
return 0; }
DOC example.xml <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>
Título: Re: tinyXML c++
Publicado por: EdePC en 1 Febrero 2019, 00:21 am
Saludos, - 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: #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; }
<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:\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
Título: Re: tinyXML c++
Publicado por: statham en 1 Febrero 2019, 11:27 am
Saludos, - 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: #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; }
<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:\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 muchisimas gracias me sirivio!! Un saludo.
|