LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-09-2008, 09:06 PM   #1
BeaverusIV
Member
 
Registered: Oct 2004
Location: New Zealand
Distribution: Arch Linux 2010.05
Posts: 129

Rep: Reputation: 18
[C++] I just wanna parse a bit of xml....


Hey all.
I am working on a game and it uses xml to store item/character/map information etc. In Windows I used Code Synthesis XSD or something to do it and it was real slow for my small xml files, so I don't really want to do the DOM thing this time...

These files hold information that is parsed and put into classes so I don't need the whole thing in memory, I just wanna be able to go:

xml.open("character.xml");
name = xml->character->name();
level = xml->character->level();
for(i = xmlNodes("items"); i != i.end(); i++) {
itemList.push(i->itemID());
}

etc, etc.

Can someone help me? I just need a nudge in the right direction. I looked into libxml2 but it looks way to complicated for my needs, how is SAX?

Thank you for your replies.
 
Old 12-09-2008, 10:43 PM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
What will be the extent of the structural depth and complexity? There is a huge difference between parsing and being able to use what you've parsed. Technically you can parse XML using a few hundred lines, but making it into something useful is a different issue. Do you just plan to serialize a structure, then store a set of XML-formatted lists of member values, or will the data have to be structured in, e.g., a binary tree, with arbitrary depth and nesting?

I'm not familiar with any specific XML parsers, but I've written one on my own. From what it sounds like, it would be massive overkill for what you need. Parsing is the simple part, so if you don't mind working with strings and you have some time then you could write a parser yourself. If you need some sort of complex structure other than filling a finite depth of member data then it will depend on your specific application.
ta0kira

PS It actually sounds like XML is overkill itself. If the files don't need to be user-editable, you could just store a list of character1.name = "Death Master"-like lines. Those are remarkably easy to parse, especially if you don't have to take into account extra whitespace, etc. Writing parsers is a great skill to have, so a good time to start is now, while your requirement is simple. Later on you can expand it with new features if necessary.

PPS If you're going to go to the trouble of having functions like xml->character->name();, you might as well just parse directly from/into the class that contains name.

Last edited by ta0kira; 12-09-2008 at 10:56 PM.
 
Old 12-10-2008, 12:27 AM   #3
BeaverusIV
Member
 
Registered: Oct 2004
Location: New Zealand
Distribution: Arch Linux 2010.05
Posts: 129

Original Poster
Rep: Reputation: 18
Sample xml:
Code:
<character version="0.3">
    <name>BeaverusIV</name>
    <level>1</level>
    <class>Ninja</class>
    <experience>150</experience>
    <stats>
        <health>100</health>
        <energy>100</energy>
        <strength>5</strength>
        <speed>10</speed>
        <intelligence>1</intelligence>
        <free_stats>0</free_stats>
    </stats>
    <skills>
        <skill id="2" level="3"/>
        <skill id="3" level="5"/>
        <skill id="4" level="0"/>
        <skill id="5" level="0"/>
    </skills>
    <resistances>
        <physical>0</physical>
        <mind_control>0</mind_control>
        <fire>0</fire>
        <wind>0</wind>
        <water>0</water>
        <magic>0</magic>
        <static>0</static>
        <pierce>0</pierce>
    </resistances>
    <equipped>
        <head>0</head>
        <torso>2</torso>
        <feet>0</feet>
        <hands>0</hands>
        <weapon1>1</weapon1>
        <weapon2>0</weapon2>
        <necklace>0</necklace>
        <ring1>0</ring1>
        <ring2>0</ring2>
    </equipped>
    <inventory>
        <slot>0</slot>
    </inventory>
</character>
or:

Code:
<spells version="0.1">
    <spell id="1">
        <name>Attack</name>
        <animation>0</animation>
        <requirements>
            <level>0</level>
            <class>ANY</class>
            <energy base="0" per_lvl="0"/>
        </requirements>
        <damage type="1" base="0" per_lvl="0"/>
    </spell>
    <spell id="2">
        <name>Fireball</name>
        <animation>1</animation>
        <requirements>
            <level>5</level>
            <class>NINJA</class>
            <energy base="10" per_lvl="2"/>
        </requirements>
        <damage type="2" base="50" per_lvl="10"/>
    </spell>
</spells>
I wanted to work with xml because it is a standard file format and easily editable/understandable without any special tools. I have used plain text files with the variables in before, they are definitely easier.
I might just make my own parser that just handles the files I give it, therefore just putting it into construction/destruction of classes.
I liked xml also because I could easily change things around and previous code still worked as long as those tags were still in; with my text files I usually had to change a whole lot of code everytime I moved things around.
 
Old 12-10-2008, 02:46 AM   #4
pinniped
Senior Member
 
Registered: May 2008
Location: planet earth
Distribution: Debian
Posts: 1,732

Rep: Reputation: 50
Maybe something like this would do:

www.minixml.org

(libmxml1)

The library is meant to be 'light' and may be missing some features that you would like - for example checking that nested tags are matched correctly (just an example - I can't remember if nested tags are checked).

libxml2 is a Gnome thing, and yes it is complicated.
 
Old 12-10-2008, 03:12 AM   #5
BeaverusIV
Member
 
Registered: Oct 2004
Location: New Zealand
Distribution: Arch Linux 2010.05
Posts: 129

Original Poster
Rep: Reputation: 18
Looks great. Thanks I'll give it a try. I'd hate to have to make my own parser/file type, this kinda thing is such a small part of creating a game... Once this is done I can get ready to recode from DirectX into SDL... :/
 
Old 12-10-2008, 05:38 AM   #6
arunmathew1984
Member
 
Registered: Nov 2008
Posts: 31

Rep: Reputation: 15
I not 100% sure whether you can use it from your C++ application - but there is an API for Java called Castor that you might be able to use for your requirement.

Linux Archive

Last edited by arunmathew1984; 12-20-2008 at 11:16 AM.
 
Old 12-10-2008, 09:27 AM   #7
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 111Reputation: 111
I prefer rapidxml for my XML parsing needs. It's blazing fast.
 
Old 12-10-2008, 09:31 AM   #8
boseko
LQ Newbie
 
Registered: Dec 2008
Posts: 2

Rep: Reputation: 0
Hi,

Quote:
Originally Posted by BeaverusIV View Post
In Windows I used Code Synthesis XSD or something to do it and it was real slow for my small xml files, so I don't really want to do the DOM thing this time...

These files hold information that is parsed and put into classes so I don't need the whole thing in memory, I just wanna be able to go:

xml.open("character.xml");
name = xml->character->name();
level = xml->character->level();
for(i = xmlNodes("items"); i != i.end(); i++) {
itemList.push(i->itemID());
}
While you say you don't want to have DOM, your code fragment is based on the assumption that there is some sort if an in-memory representation of XML stored in the 'xml' variable that you then query to populate your classes. If your want a substantial speed-up then you will need to get rid of the intermediate in-memory representation and parse XML data directly into your classes. Normally this means you will need to use an even-driven API such as SAX or XmlReader.

BTW, the CodeSynthesis XSD tool that you mentioned above provides two XML Schema to C++ mappings C++/Tree (in-memory) and C++/Parser (event-driven). It sounds like C++/Parser may be exactly what you want since it is SAX-based (you can use either Expat or Xerces-C++ as the underlying XML parser), quite fast, and is a lot easier to use than raw SAX. In particular, it does all the state management and text to data conversion for you.

Here is an example API you will get for the character element in the XML file you showed:

Code:
class character_pimpl
{
  virtual void name (string n)
  {
     ...
  }

  virtual void level (int l)
  {
     ...
  }

  ...
};
That is, you implement the name(), level(), etc. functions and then the runtime will call them as it parses the XML with the corresponding data which you can use to populate your classes.

More information on the C++/Parser mapping:

http://www.codesynthesis.com/products/xsd/c++/parser/

Getting started guide:

http://www.codesynthesis.com/project.../parser/guide/

Boris
 
  


Reply

Tags
c++, parse, simple, xml


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to parse the xml string Dyuri Linux - Software 2 09-03-2008 08:49 AM
Parse XML in bash script MikeyCarter Linux - Software 1 02-16-2007 01:19 PM
kopete icq can't parse xml-documents ungua Suse/Novell 2 10-24-2004 10:19 AM
fontconfig expat xml parse PROBLEMS!! OrganicOrange84 Linux - Software 0 08-18-2004 12:35 PM
xml parse.dll error installing gimp xcaper General 1 04-16-2004 03:07 PM


All times are GMT -5. The time now is 08:00 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration