Parsing XML [SOLVED o_o]

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
User avatar
gas
Miko-Class Veteran
Posts: 842
Joined: Mon Jan 26, 2009 7:21 pm
Contact:

Parsing XML [SOLVED o_o]

#1 Post by gas »

I'm thinking about using an XML file to store a (large) number of values. Then use a parser to convert those data into a [list [of list]]

The tree is something like:
<TREE>
<element>
<NAME>poppy</NAME>
<LEGS>
<feet>1</feet>
<feet>3</feet>
<feet>5</feet>
</LEGS>
</element>
</TREE>

and must return:
$ sillyness=[(poppy,(1,3,5)),...]
for each 'element'.

Being a total noob in parsing stuff, can someone please post a point by point guide to have a game parsing an XML tree?
The guide must include what to download and what to put in the distributable game.
I know about BeautifulSoup, but I'm totally lost with lxml.

Thank you.
Last edited by gas on Mon Feb 20, 2017 7:49 pm, edited 1 time in total.
If you want to debate on a reply I gave to your posts, please QUOTE ME or i'll not be notified about. << now red so probably you'll see it.

10 ? "RENPY"
20 GOTO 10

RUN

User avatar
SuperbowserX
Veteran
Posts: 270
Joined: Sat Jan 07, 2017 3:09 pm
Contact:

Re: Parsing XML

#2 Post by SuperbowserX »

Are you trying to make a 2d array? You could use something like this.

https://www.renpy.org/wiki/renpy/doc/co ... _from_file

From there you could study up on the procedure for reading lines from files in Python. Google is your friend!

User avatar
gas
Miko-Class Veteran
Posts: 842
Joined: Mon Jan 26, 2009 7:21 pm
Contact:

Re: Parsing XML

#3 Post by gas »

SuperbowserX wrote:Are you trying to make a 2d array? You could use something like this.

https://www.renpy.org/wiki/renpy/doc/co ... _from_file

From there you could study up on the procedure for reading lines from files in Python. Google is your friend!
Hahaha! I was so concerned about importing that lxml thing that I forgot to check the RENPY documentation XD.
Thank you, I think now could be very easy, there's a great documentation for ElementTree.
If you want to debate on a reply I gave to your posts, please QUOTE ME or i'll not be notified about. << now red so probably you'll see it.

10 ? "RENPY"
20 GOTO 10

RUN

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2404
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: Parsing XML [SOLVED o_o]

#4 Post by Ocelot »

Just some points:
1) Do not use renpy.loader.transfn, or anything from renpy.loader. It is not supported. Use renpy.file instead of open+transfn.
2) I prefer JSON over XML. It is more terse, its data structures translates directly to Python ones, there is builtin library to handle it.
< < insert Rick Cook quote here > >

User avatar
gas
Miko-Class Veteran
Posts: 842
Joined: Mon Jan 26, 2009 7:21 pm
Contact:

Re: Parsing XML [SOLVED o_o]

#5 Post by gas »

Ocelot wrote:Just some points:
1) Do not use renpy.loader.transfn, or anything from renpy.loader. It is not supported. Use renpy.file instead of open+transfn.
2) I prefer JSON over XML. It is more terse, its data structures translates directly to Python ones, there is builtin library to handle it.
Hey, thank you for that hint, I'll try both and I'll see the one I find more comfortable with.
If you want to debate on a reply I gave to your posts, please QUOTE ME or i'll not be notified about. << now red so probably you'll see it.

10 ? "RENPY"
20 GOTO 10

RUN

User avatar
PyTom
Ren'Py Creator
Posts: 16096
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: Parsing XML [SOLVED o_o]

#6 Post by PyTom »

Honestly, unless you have some really special need, just generate a Ren'Py file. You can define objects that are sensible for your game, and use them directly, rather than first having to parse JSON into the format you actually want. (XML is right out.)
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

Post Reply

Who is online

Users browsing this forum: Alex