Menu

What is the source code to parse this XMLfile

Anonymous
2003-08-01
2013-05-20
  • Anonymous

    Anonymous - 2003-08-01

    What is the source code to parse this XML file ?

    I can read the first item <Carte... but I can't find the method to read the other items <Carte...

    <?xml version="1.0" ?>
    <!-- Liste des cartes Keithley -->
    <Keithley>
        <Carte Name="KPCI-1801HC" Driver="KPCI1800" NbAI="64" NbAO="2" NbDI="4" NbDO="8" NbDIO="0" />
        <Carte Name="KPCI-3107" Driver="KPCI3108" NbAI="16" NbAO="0" NbDI="6" NbDO="6" NbDIO="32" />
        <Carte Name="KPCI-PIO24" Driver="KPCIPIO" NbAI="0" NbAO="0" NbDI="0" NbDO="0" NbDIO="24" />
        <Carte Name="KPCI-3160" Driver="KPCI3160" NbAI="0" NbAO="0" NbDI="0" NbDO="0" NbDIO="96" />
    </Keithley>

    Thanks for your help,

    PurLi

     
    • Yves Berquin

      Yves Berquin - 2003-08-01

      If you have a pointer to the first one, the next one is :

      first -> NextSiblingElement ();

      or if you specifically search the next "Carte" :

      first -> NextSiblingElement ("Carte");

      Yves

       
    • whazilla

      whazilla - 2006-07-28

      here is my xml file

      <Main>

                  <MENU>
              <MENUItem priority="1">Games
      <sub1>Load UMD </sub1>
      <sub2>Load DEVHOOK 2.71 NOKX </sub2>
              </MENUItem>

              <MENUItem priority="2"> Media
      <sub> Images   </sub>
      <sub> Videos   </sub>
      <sub> Pictures </sub>
      <sub> Books    </sub>
              </MENUItem>
             
              <MENUItem priority="3"> Network
      <sub> Internet </sub>
      <sub> RSS</sub>
              </MENUItem>
             
              <MENUItem priority="4"> Settings

      <sub> System Update </sub>
      <sub> System Settings
                   <subsub>Nickname</subsub>
                  <subsub>Date and Time</subsub>
                  <subsub>Skin</subsub>
                  <subsub>Plugins</subsub>
                  <subsub>Performance</subsub>
                  </sub>
              </MENUItem>
         
              </MENU>
          </Main>

      the thing i want it todo is get Nextsliblingelement("menuItem")
      and put all menuitems in array aswell as count them

      but i can't even make it show all menuitems on screen

      help needed

      this is the code i use

      int count = 0;
      for( node = doc.FirstChild();
               node!=NULL;
               node = node -> NextSiblingElement ("MENUItem"))
          {  count++;
             printf("%s", node->Value());
          }

      it lacks todo what i want it todo

       
    • Steve Hyatt

      Steve Hyatt - 2006-08-16

      Hi.

      This is what I do to 'iterate' over a bunch of elements.  Basically I have a function that builds a vector of all elements with a given name:

      #include "tinyxml.h"

      #include <iostream>
      #include <vector>
      using namespace std;

      typedef vector <TiXmlElement*> NodeList;
      void findElements(TiXmlElement* node, const char* searchName, NodeList& nodes)
      {
          if (node) {
              string name (searchName);
             
              if (name == node->Value ()) {
                  nodes.push_back (node);
              }
             
              TiXmlElement* child = node->FirstChildElement ();
              for (; child != NULL; child = child->NextSiblingElement ()) {
                  findElements(child, searchName, nodes);
              }
          }
      }

      TiXmlElement* getRootNode(TiXmlDocument* doc, const char* filename,
              bool exitOnError = false)
      {
          assert (doc != NULL);   
          assert (filename != NULL);
         
          doc->LoadFile (filename);
          if (doc->Error ()) {
              if (exitOnError) {
                  exit (1);
              }
          }
         
          TiXmlElement* rootNode = doc->RootElement ();
         
          return rootNode;
      }

      int main()
      {
          // get a list of all nodes named 'Carte'
          TiXmlDocument doc;
          TiXmlElement* rootNode = getRootNode(&doc, "test.xml");

          NodeList nodes;
          findElements(rootNode, "Carte", nodes);
         
          cout << "found " << nodes.size() << " elements:" << endl;
         
          NodeList::iterator it;
          for (it = nodes.begin (); it != nodes.end (); it++) {
              TiXmlElement* node = (TiXmlElement*) *it;
             
              cerr << "  node: '" << node->Value() << "' type=" << node->Type()
                   << endl;
          }

      }

      Steve

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.