s c h e m a t i c s : c o o k b o o k

/ Cookbook.XMLRecipeXMLToData

This Web


WebHome 
WebChanges 
TOC (with recipes)
NewRecipe 
WebTopicList 
WebStatistics 

Other Webs


Chicken
Cookbook
Erlang
Know
Main
Plugins
Sandbox
Scm
TWiki  

Schematics


Schematics Home
Sourceforge Page
SchemeWiki.org
Original Cookbook
RSS

Scheme Links


Schemers.org
Scheme FAQ
R5RS
SRFIs
Scheme Cross Reference
PLT Scheme SISC
Scheme48 SCM
MIT Scheme scsh
JScheme Kawa
Chicken Guile
Bigloo Tiny
Gambit LispMe
GaucheChez

Lambda the Ultimate
TWiki.org

Parsing XML into Data Structures

Problem

You want a list that corresponds to the structure and content of an XML file.

Solution

For example you have this example:

<?xml version="1.0" standalone="yes"?>
<rhythmdb version="1.0">
  <entry type="song">
    <title>Never Let Me Down Again</title>
    <genre>Pop/Rock</genre>
    <artist>Depeche Mode</artist>
    <album>Music For The Masses</album>
    <track-number>1</track-number>
    <duration>287</duration>
    <file-size>6533368</file-size>
    <location>file:///home/hector/ogg/depeche_mode/music_for_the_masses/never_let_me_down_again.ogg</location>
    <mtime>1079831032</mtime>
    <play-count>1</play-count>
    <last-played>1083552958</last-played>
    <mimetype></mimetype>
  </entry>
</rhythmdb>

We will use SSAX library to parse this XML file in to a SXML abstract syntax tree. In specific we will use the ssax:xml->sxml function that takes a stream and a list of (user-prefix . uri-string) that assigns user prefixes to certain namespaces identified by particular URIs. In this example the file is a config named rhythmdb.xml and the XML namespace is the default so we put the empty list.

(require (lib "ssax.ss" "ssax"))
(ssax:xml->sxml (open-input-file "rhythmdb.xml") '())

This outputs a SXML abstract syntax tree that for all situations can be handled like a list:

> (ssax:xml->sxml (open-input-file "rhythmdb.xml") '())
(|*TOP*|
 (|*PI*| xml "version=\"1.0\" standalone=\"yes\"")
 (rhythmdb
   (@ (version "1.0"))
   (entry
    (@ (type "song"))
    (title "Never Let Me Down Again")
    (genre "Pop/Rock")
    (artist "Depeche Mode")
    (album "Music For The Masses")
    (track-number "1")
    (duration "287")
    (file-size "6533368")
    (location "file:///home/hector/ogg/depeche_mode/music_for_the_masses/never_let_me_down_again.ogg")
    (mtime "1079831032")
    (play-count "1")
    (last-played "1083552958")
    (mimetype))))

If we want to parse the rhythmdb.xml from a string, we use:

(require (lib "ssax.ss" "ssax"))
(ssax:xml->sxml (open-input-string rhythmdb-string) '())

And the output is the same from parsing the rhythmd.xml file.

Discussion

Discussion here

See Also


Comments about this recipe

Shortening the location element to about 40 characters would make Web and print layout easier.

-- NeilVanDyke - 20 May 2004

Contributors

-- HectorEGomezMorales - 19 May 2004

CookbookForm
TopicType: Recipe
ParentTopic: XmlRecipes
TopicOrder: 010

 
 
Copyright © 2004 by the contributing authors. All material on the Schematics Cookbook web site is the property of the contributing authors.
The copyright for certain compilations of material taken from this website is held by the SchematicsEditorsGroup - see ContributorAgreement & LGPL.
Other than such compilations, this material can be redistributed and/or modified under the terms of the GNU Lesser General Public License (LGPL), version 2.1, as published by the Free Software Foundation.
Ideas, requests, problems regarding Schematics Cookbook? Send feedback.
/ You are Main.guest