Working with XML, anyone knows a nice guide?

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Morgawr
Posts: 95
Joined: Wed Sep 26, 2007 7:04 pm

Working with XML, anyone knows a nice guide?

Post by Morgawr »

Hello everyone, I just realized I need to learn some XML for my program because that would make it easier so, since I have some time, I wanted to flick through a nice guide/manual on the net before buying a real book (maybe).

Does anyone know a very nice guide on the net for XML? A complete guide with maybe examples and easy-tips and anything that makes a guide "nice" :roll:

I've already checked the search feature in this forums but didn't find anything in here and I am already browsing google as we speak, looking for a nice guide, but surely some help from you professional programmers would be awesome :wink:

very very thanks again :D
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

Google is amazing !!! :roll:
just if you're to dump to use google: :twisted:
http://www.google.de/search?hl=de&q=%2B ... uche&meta=
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Morgawr
Posts: 95
Joined: Wed Sep 26, 2007 7:04 pm

Post by Morgawr »

Acki wrote:Google is amazing !!! :roll:
just if you're to dump to use google: :twisted:
http://www.google.de/search?hl=de&q=%2B ... uche&meta=
heh yeah :oops: I found http://www.w3schools.com/xml/xml_view.asp this page right after I posted that message and read it (most of it) and now I can understand the basics behind XML... so well, I made a test program to see if it worked but I'm having problems because I don't really know how to start reading from an XML file... this is my program (the important part):

Code: Select all

...
    IrrXMLReader* read = createIrrXMLReader("texting.xml");
    if(read==0)
     cout << "not loaded\n";

    u32 var1;
    u32 var2;
    while(read->read())
    {
        if(read->getNodeName()=="var1")
         var1 = (u32)read->getAttributeValue(read->getNodeName());
    }
    cout << var1 << "\n";// << var2 << "\n";
...
(nevermind the commented out part, it was part of a test)
and this is my XML file:

Code: Select all

<?xml version="1.0" encoding="ISO-8859-1"?>
<body>
    <var1 ID="1">3</var1>
    <var2 ID="2">4</var2>
    <body2>
        <var3 ID="3">5</var3>
        <stringa ID="4">ciao a tutti</stringa>
    </body2>
</body>
I don't know if the reading process is correct or not, I don't think so since it's not working properly, I get an int number like "3086andsomeotherdigits" instead of "3"... I also tried getAttributeValueAsInt but I get the same results... can somebody give me a brief explanation or tell me what's the wrong part in that code? thanks a lot :roll:
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
Morgawr
Posts: 95
Joined: Wed Sep 26, 2007 7:04 pm

Post by Morgawr »

As I said in the post above, already found that guide :lol:

thanks :wink:
Morgawr
Posts: 95
Joined: Wed Sep 26, 2007 7:04 pm

Post by Morgawr »

Sorry for double posting but I updated my code with something like this just to see if it works:

Code: Select all

    while(read->read())
    {
        if((read->getNodeType()==EXN_ELEMENT))
        {
         cout << "\n inside exn_element \n" << read->getNodeName();
         if(read->getNodeName()=="var1")
         {
          cout << read->getNodeName()<<"\n";
          cout << "\n inside getnodename() \n";
         }
        }
    }
and it's totally screwed up o.o
I mean, I don't understand how it's possible that it NEVER enters into the second if but inside the first if when I print all the getNodeName() I DO get a "var1" node name, which doesn't get passed into the following if condition... I really don't understand this, can somebody enlighten me very please? :cry:
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

I'm not sure, because I use tinyXML (it's realy easy to use)... ;)

but you get a char_type* as return value of getNodeName so you can't compare it with ==, or am I wrong ???

maybe try something like this:

Code: Select all

         stringc test = read->getNodeName();
         if(test.equals_ignore_case("var1"))
or try tinyXML... :lol:
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Morgawr
Posts: 95
Joined: Wed Sep 26, 2007 7:04 pm

Post by Morgawr »

Acki wrote:I'm not sure, because I use tinyXML (it's realy easy to use)... ;)

but you get a char_type* as return value of getNodeName so you can't compare it with ==, or am I wrong ???

maybe try something like this:

Code: Select all

         stringc test = read->getNodeName();
         if(test.equals_ignore_case("var1"))
or try tinyXML... :lol:
Wow it works :lol: I guess it was a lower/upper case problem, thanks a lot :D

anyways what's tinyXML? It might be interesting and, if as you said it's easier, might want to try it out before I build all my program on IrrXML just to get screwed later :)
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

and again, google is your friend... ;)

http://sourceforge.net/projects/tinyxml
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
dlangdev
Posts: 1324
Joined: Tue Aug 07, 2007 7:28 pm
Location: Beaverton OR
Contact:

Post by dlangdev »

wow, thanks acki for the tinyxml link.

i'm currently reading the docs at the moment...

http://www.grinninglizard.com/tinyxmldo ... rial0.html

i've started coding using msxml 6.0 api and it looks pretty solid so far as it fits with the design spec. but tinyxml could bring more stuff in shorter time. although it seems that it doesn't load from a string, as most that i found load from a file.
Image
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

I love tinyXML, I also used it with my IrrExtensions... ;)
small examples:

reading:

Code: Select all

TiXmlDocument doc("./defaults.dat");
if(doc.LoadFile()){
  TiXmlElement* root = doc.RootElement();
  if(root){
    TiXmlElement* grp = root->FirstChildElement("Tools");
    if(grp){
      for(TiXmlElement* itm = grp->FirstChildElement("set"); itm != NULL; itm = itm->NextSiblingElement("set")){
        int id;
        if(itm->Attribute("ID", &id)){
          cCheckButton* o = getToolWidget(id)->chkWidget;
          if(o){
            int i;
            if(itm->Attribute("Value", &i))  o->value((bool)i);
          }
        }
      }
    }
  }
}
writing:

Code: Select all

TiXmlDocument doc;
TiXmlDeclaration* decl = new TiXmlDeclaration("1.0", "", "");
doc.LinkEndChild(decl);
TiXmlElement* root = new TiXmlElement("IrrExt13_Defaults");
TiXmlElement* tools = new TiXmlElement("Tools");
for(int t = 0; t < lstTools.size(); t++){
  TiXmlElement* elm = new TiXmlElement("set");
  elm->SetAttribute("ID", (int)lstTools[t].ID);
  elm->SetAttribute("Value", (int)lstTools[t].chkWidget->value());
  tools->LinkEndChild(elm);
}
root->LinkEndChild(tools);
doc.LinkEndChild(root);
doc.SaveFile("./defaults.dat");
and the XML file:

Code: Select all

<?xml version="1.0" ?>
<IrrExt13_Defaults>
    <Tools>
        <set ID="0" Value="1" />
        <set ID="2" Value="1" />
        <set ID="4" Value="1" />
        <set ID="5" Value="0" />
        <set ID="6" Value="0" />
        <set ID="7" Value="0" />
        <set ID="8" Value="0" />
        <set ID="10" Value="0" />
        <set ID="12" Value="0" />
        <set ID="14" Value="1" />
        <set ID="100" Value="0" />
        <set ID="102" Value="0" />
        <set ID="103" Value="0" />
        <set ID="104" Value="0" />
        <set ID="105" Value="0" />
        <set ID="1" Value="0" />
        <set ID="3" Value="0" />
        <set ID="9" Value="0" />
        <set ID="11" Value="0" />
        <set ID="13" Value="0" />
        <set ID="101" Value="0" />
    </Tools>
</IrrExt13_Defaults>
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
dlangdev
Posts: 1324
Joined: Tue Aug 07, 2007 7:28 pm
Location: Beaverton OR
Contact:

Post by dlangdev »

acki: that code looks beautiful, i like it.

i've got demo #1 for msxml coded and tested, what i found out using msxml sdk is the following:

1) xmldoc object can load from file or string, pretty much covering the two important feature of loading from a file and getting xml data from a remote machine, typically a soap call return data.

2) once loaded, i can walk the dom and pick the elements with corresponding attribs. though i still don't like dealing with LPCSTR and BSTR, though. they're nasty.

anyway, i still have many steps to take, including one demo using tinyxml.

thanks for the code, really appreciate it.
Image
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Ok, then let's check about irrXML

Code: Select all

io::IXMLReader* reader = FileSystem->createXMLReader(file);
if (reader)
{
  while(reader->read())
  {
    if (reader->getNodeType() == io::EXN_ELEMENT)
    {
      if (core::stringc("set") == reader->getNodeName())
      {
        s32 id;
        if (reader->getAttributeValue(L"ID").size())
        {
          id = reader->getAttributeValueAsInt(L"ID");
          cCheckButton* o = getToolWidget(id)->chkWidget;
          if(o)
          {
             if (reader->getAttributeValue(L"Value").size())
               o->value(reader->getAttributeValueAsInt(L"Value"));
          }
        }
      }
    }
  }
}
and writing

Code: Select all

Writer = FileSystem->createXMLWriter(file);
if (Writer)
{
  Writer->writeXMLHeader();
  Writer->writeElement(L"IrrExt13_Defaults", false);
  Writer->writeLineBreak();
  Writer->writeElement(L"Tools", false);
  Writer->writeLineBreak();
  for(int t = 0; t < lstTools.size(); t++)
  {
    Writer->writeElement(L"set", true,
      L"ID", core::stringw(lstTools[t].ID).c_str(),
      L"Value", core::stringw(lstTools[t].chkWidget->value()).c_str());
  }
  Writer->writeClosingTag(L"Tools");
  Writer->writeLineBreak();
  Writer->writeClosingTag(L"IrrExt13_Defaults");
  Writer->drop();
}
I don't think that these approaches are really different in code size or programming complexity. Of course, irrXML is doing a SAX style, while tinyXML seems to have a DOM. But for the presented purpose using SAX is pretty straight.
CuteAlien
Admin
Posts: 9933
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

The main problem with irrXML for me is that it writes 32-bit chars in linux and I still have not found a single editor on Linux which can handle this. Which means that every time I write something with it in Linux I have to switch to windows whenever I need to modify the xml's. And that just sucks.

edit: And yeah, sorry, that doesn't really belong in the thread. Seeing tinyXml and irrXML compared on the level of reading/writing was just getting on my nerves, as this is so far from the biggest problem I have to fight when using irrXML.

edit2: Also tinyXml and irrXML do different jobs anyway. With tinyXml you work with xml-structures in c++. Mainly in memory! Additionally it offers you the functions to load & save those structures (with a single command!). irrXML is a pure xml reader/writer.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Post Reply