Menu

Help RootElement tagname

2006-11-25
2013-05-20
  • Martijn Laarman

    Martijn Laarman - 2006-11-25

    I want to check the RootElement's tagname to make sure the xml is one i want to process i am however getting (null) all the time :(

                TiXmlNode* node = 0;
                TiXmlElement* itemElement = 0;
                node = doc.RootElement();
                itemElement = node->ToElement();
                assert(itemElement);
                const char *root = node->Value();

    excerpt from my xml file starting from line 1.
    - <dialog direction="horizontal" dname="dcXMLdialog" space="5 5 5 5">
    - <pane direction="vertical" weight="2">
    - <control type="panel" weigth="1" id="1">
      <childcontrol type="toolbar" id="12" weight="1" />
      <childcontrol type="listview" id="20" styles="report smallicon grid" weight="4" />
    - <childcontrol type="panel" id="16" weigth="8">
      <childcontrol type="IPaddress" id="17" weight="1" />
      <childcontrol type="button" id="18" styles="" weight="4" />
    - <pane direction="vertical" weight="2">

    Any help would be appreciated alot :)

     
    • Jewe

      Jewe - 2006-11-28

      Type error?

      const char *root = itemElement->Value();

       
      • Ellers

        Ellers - 2006-11-29

        Huh?

        Read the source.

        e.g.:
          http://tinyxml.cvs.sourceforge.net/tinyxml/tinyxml/tinyxml.h?view=markup

        see various occurrences like:
          const char *Value()

        Assuming that in your code itemElement is a ptr to a TiXmlNode then const char* is the right type.

        Perhaps you can be more specific/detailed in your question?

         
    • Martijn Laarman

      Martijn Laarman - 2006-11-30

      well i resolved this question by doing the rather simple:
      --
      valid_DCXML = doc.FirstChildElement("dialog");
      if (valid_DCXML) {
      --

      However my question still sorta remains intact:
      <pre>
      void walkScript(TiXmlElement* element, char *dname, int depth=0) { 
          TiXmlElement* child = 0; 
          TString cmd;
          for( child = element->FirstChildElement(); child; child = child->NextSiblingElement() ) { 
              TiXmlElement* parent = child->Parent()->ToElement();
              cmd.sprintf("/echo -a Element %s %s %i parent: %s",
                  child->Value(),child->Attribute("type"),depth,parent->Value());
              mIRCcom(cmd.to_chr());
              walkScript(child->ToElement(), dname, depth+1); 
          } 
      }
      </pre>

      I use this slightly modified function that i found on these boards to iterate over all the elements in my XML which works perfectly however i am now faced with the rather silly question of how to do:
      <pre>
      if (parent->Value() == "control") {
          if (parent->Attribute("type") == "listview") { }
      }
      </pre>
      Admittingly my c++ knowledge is very weak.

      Example XML:
      <pre>
      <dialog cascade="v" space="5 5 5 5">
          <pane weigth="12">
              <control type="stacker" id="1" weigth="2">
                  <control type="panel" id="2" weigth="2">
                      <control type="listview" id="3" weigth="2" styles="report smallicon" />
                  </control>
              </control>
          </pane>
          <control type="dialog" id="4" weigth="2">
              <pane weigth="12">
                  <control type="listview" id="5" weigth="2" />
                  <control type="panel" id="6" weigth="2">
                      <control type="pbar" id="7" weigth="2" styles="report smallicon" />
                  </control>
              </pane>
          </control>
          <control type="toolbar" id="8" weigth="8" heigth="30" styles="report smallicon" />
      </dialog>
      </pre>

       
      • Ellers

        Ellers - 2006-11-30

        I can't say for sure if its the problem but this:

        if (parent->Value() == "control") {
        if (parent->Attribute("type") == "listview") { }

        will only work if you are using classes for the strings, but it looks like you're using plain char *, in which case use:

        if (0==strcmp(parent->Value(), "control")) {
        if (0==strcmp(parent->Attribute("type"), "listview")) { }

        HTH
        Ellers

         
    • Martijn Laarman

      Martijn Laarman - 2006-11-30

      Thanks that does the job :)
      if (0==strcmp(valid_DCXML->Attribute("cascade"), "v")) { }
      works but if i check on an attribute that doesn't excist the program throws an exception:
      if (0==strcmp(valid_DCXML->Attribute("spaca"), "5 5 5 5")) { }

      But i think that can be solved by doing:
      const char * spaca= valid_DCXML->Attribute("spaca")
      if (spaca) {
      if (0==strcmp(spaca, "5 5 5 5")) { }

      right ?

       
      • Ellers

        Ellers - 2006-11-30

        That should be it exactly :)

         
    • Martijn Laarman

      Martijn Laarman - 2006-11-30

      Alright :) Thanks for getting me on the right track now i can start doing the fun part: DOM parsing :D

       

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.