Skip to content

Instantly share code, notes, and snippets.

@ebot
Created April 30, 2009 20:01
Show Gist options
  • Save ebot/104653 to your computer and use it in GitHub Desktop.
Save ebot/104653 to your computer and use it in GitHub Desktop.
Ideas for Professor: JavaScript XML Object

The Big Idea

  • Create a JavaScript class that converts an xml document into an object.
  • Inspired from Ruby’s XmlSimple.
var course = new JsXml('course.xml');
course.get('title'); // returns the title.
course.units[0].get('pages'); // returns the pages in the first unit
course.units[0].attributes.get('number'); // returns the attribute number
course.quiz.questions[0].get('answer'); //returns the answer to the first question
<?xml version="1.0" encoding="UTF-8"?>
<course>
<title>Peter's Test Course</title>
<version>v0001</version>
<author>Peter Frampton</author>
<copyrightyear>2009</copyrightyear>
<companyname>WorldWide Education USA</companyname>
<units>
<unit number="1">
<title>Lesson 1</title>
<pages>3</pages>
<firstpagepath>/course/unit01/page01.htm</firstpagepath>
<graphic>/core/images/imageu01_01.jpg</graphic>
</unit>
<unit number="2">
<title>Lesson 2</title>
<pages>3</pages>
<firstpagepath>/course/unit02/page01.htm</firstpagepath>
<graphic>/core/images/imageu02_01.jpg</graphic>
</unit>
</units>
<quiz>
<questions>
<question number="1">
<text>What is the meaning of life?</text>
<answer>42</answer>
</question>
<questions>
</quiz>
</course>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment