Skip to content

Instantly share code, notes, and snippets.

@abhi9bakshi
Last active January 17, 2017 11:28
Show Gist options
  • Save abhi9bakshi/6e742da5735c1b734eaace452bd45db1 to your computer and use it in GitHub Desktop.
Save abhi9bakshi/6e742da5735c1b734eaace452bd45db1 to your computer and use it in GitHub Desktop.
Multi Level XML & JSON parsing using Javascript

#Chapter 1

#Chapter 2

##Lesson 1

##Lesson 2

##Lesson 3

#Chapter 3

##Lesson 1

###Page 1

###Page 2

###Page 3

##Lesson 2

#Requirements:

  1. If XML tree has only chapter, don't show dropdown.
  2. If XML tree has chapter with lessons but no pages, don't show dropdown for pages.
  3. If XML tree has chapters, with lessons having pages, show dropdown for both chapters and pages.
{
"course": {
"title": "Multi Level XML & JSON parsing using Javascript",
"chapter": [
{
"id": "1",
"title": "Chapter 1",
"url": "https://gist.github.com/abhi9bakshi/6e742da5735c1b734eaace452bd45db1#chapter-1"
},
{
"id": "2",
"title": "Chapter 2",
"url": "https://gist.github.com/abhi9bakshi/6e742da5735c1b734eaace452bd45db1#chapter-2",
"lesson": [
{
"id": "1",
"title": "Lesson 1",
"url": "https://gist.github.com/abhi9bakshi/6e742da5735c1b734eaace452bd45db1#lesson-1"
},
{
"id": "2",
"title": "Lesson 2",
"url": "https://gist.github.com/abhi9bakshi/6e742da5735c1b734eaace452bd45db1#lesson-2"
},
{
"id": "3",
"title": "Lesson 3",
"url": "https://gist.github.com/abhi9bakshi/6e742da5735c1b734eaace452bd45db1#lesson-3"
}
]
},
{
"id": "3",
"title": "Chapter 3",
"url": "https://gist.github.com/abhi9bakshi/6e742da5735c1b734eaace452bd45db1#chapter-3",
"lesson": [
{
"id": "1",
"title": "Lesson 1",
"url": "https://gist.github.com/abhi9bakshi/6e742da5735c1b734eaace452bd45db1#lesson-1-1",
"page": [
{
"id": "1",
"title": "Page 1",
"url": "https://gist.github.com/abhi9bakshi/6e742da5735c1b734eaace452bd45db1#page-1"
},
{
"id": "2",
"title": "Page 2",
"url": "https://gist.github.com/abhi9bakshi/6e742da5735c1b734eaace452bd45db1#page-2"
},
{
"id": "3",
"title": "Page 3",
"url": "https://gist.github.com/abhi9bakshi/6e742da5735c1b734eaace452bd45db1#page-3"
}
]
},
{
"id": "2",
"url": "https://gist.github.com/abhi9bakshi/6e742da5735c1b734eaace452bd45db1#lesson-2-1",
"title": "Lesson 2"
}
]
}
]
}
}
<?xml version="1.0" encoding="iso-8859-1"?>
<course>
<title>Multi Level XML &amp; JSON parsing using Javascript</title>
<chapter>
<id>1</id>
<title>Chapter 1</title>
<url>https://gist.github.com/abhi9bakshi/6e742da5735c1b734eaace452bd45db1#chapter-1</url>
</chapter>
<chapter>
<id>2</id>
<title>Chapter 2</title>
<url>https://gist.github.com/abhi9bakshi/6e742da5735c1b734eaace452bd45db1#chapter-2</url>
<lesson>
<id>1</id>
<title>Lesson 1</title>
<url>https://gist.github.com/abhi9bakshi/6e742da5735c1b734eaace452bd45db1#lesson-1</url>
</lesson>
<lesson>
<id>2</id>
<title>Lesson 2</title>
<url>https://gist.github.com/abhi9bakshi/6e742da5735c1b734eaace452bd45db1#lesson-2</url>
</lesson>
<lesson>
<id>3</id>
<title>Lesson 3</title>
<url>https://gist.github.com/abhi9bakshi/6e742da5735c1b734eaace452bd45db1#lesson-3</url>
</lesson>
</chapter>
<chapter>
<id>3</id>
<title>Chapter 3</title>
<url>https://gist.github.com/abhi9bakshi/6e742da5735c1b734eaace452bd45db1#chapter-3</url>
<lesson>
<id>1</id>
<title>Lesson 1</title>
<url>https://gist.github.com/abhi9bakshi/6e742da5735c1b734eaace452bd45db1#lesson-1-1</url>
<page>
<id>1</id>
<title>Page 1</title>
<url>https://gist.github.com/abhi9bakshi/6e742da5735c1b734eaace452bd45db1#page-1</url>
</page>
<page>
<id>2</id>
<title>Page 2</title>
<url>https://gist.github.com/abhi9bakshi/6e742da5735c1b734eaace452bd45db1#page-2</url>
</page>
<page>
<id>3</id>
<title>Page 3</title>
<url>https://gist.github.com/abhi9bakshi/6e742da5735c1b734eaace452bd45db1#page-3</url>
</page>
</lesson>
<lesson>
<id>2</id>
<url>https://gist.github.com/abhi9bakshi/6e742da5735c1b734eaace452bd45db1#lesson-2-1</url>
<title>Lesson 2</title>
</lesson>
</chapter>
</course>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment