JavaScript: get data from an xml file (like a Blogger backup file) and display it (or print it) - Part 1
I've been running around this post quite a lot. I know that it could contain valuable information for everyone and specifically for Bloggers, but there's so much into it that I don't know where to start.
Anyway... The point here is:
1) take the Blogger backup file (xml) - or any other properly formed xml file (an Atom feed as well);
2) clean it up - if needed;
3) build an HTML page that gets the data from the xml file (using JavaScript);
4) properly style the page.
As you can see the task is not that easy... or is it? Read on, and judge for yourself.
When we open a Blogger backup file, we can see that what we are looking for is contained in a parent node called, while its relevant children are:
The first part of the xml file is full of rubbish. While at the end you can find comments and pages. I decided to make a copy of the backup file and call it "thought.xml". If you are not using a Blogger backup file, you might want to manually clean your xml file - it's up to you.
As for the CSS, we can style the nodes the way we prefer. I just want to include my styles, so that you can play with them (not that there's much in them anyway).
tag will be used for the post title (the
Anyway... The point here is:
1) take the Blogger backup file (xml) - or any other properly formed xml file (an Atom feed as well);
2) clean it up - if needed;
3) build an HTML page that gets the data from the xml file (using JavaScript);
4) properly style the page.
As you can see the task is not that easy... or is it? Read on, and judge for yourself.
The backup file
If you backup your blog, you end up with an xml file that contains everything (posts, comments and pages). We are talking about Blogger here, but any xml file will do (RSS feeds or anything else). As you may already know, a xml file has a defined structure. It is basically made of closed nodes. Those nodes are normally nested, so that we have parent nodes and child nodes.When we open a Blogger backup file, we can see that what we are looking for is contained in a parent node called
The first part of the xml file is full of rubbish. While at the end you can find comments and pages. I decided to make a copy of the backup file and call it "thought.xml". If you are not using a Blogger backup file, you might want to manually clean your xml file - it's up to you.
The HTML and CSS
Now that we have the xml file ready, we need to work on the HTML page. Just create a new document with your favourite editor. The document should have a:
in the head. Just to be sure. As for the CSS, we can style the nodes the way we prefer. I just want to include my styles, so that you can play with them (not that there's much in them anyway).
I just styled the body and little other things. The tag will be used for the post title (the node).
Ok! Now we are almost ready for the magic.
Get the data!We will use Javascript to get the data from the xml file. From now on, we will work on the body of our HTML page.
For the moment I will just put the code. I will explain the code in the next post.
A place where I can share my thoughts on web development and programming. The web is such a big place...
Now you have three whole days to think about the above code. On Monday I will post an in depth explanation.
Just an important note: because we are using XMLHttpRequest, we should put the source xml file and the html page we are building on the same domain, otherwise you will get an Access Denied error from your browser. However, that is NOT always true: in my experience FireFox and Safari work perfectly locally, while Chrome and Internet Explorer need the files to be served by ISS on the same domain.
In the mean time, you can try the whole thing!
Happy coding.
Ok! Now we are almost ready for the magic.
Get the data!
We will use Javascript to get the data from the xml file. From now on, we will work on the body of our HTML page.For the moment I will just put the code. I will explain the code in the next post.
A place where I can share my thoughts on web development and programming. The web is such a big place...
Now you have three whole days to think about the above code. On Monday I will post an in depth explanation.Just an important note: because we are using XMLHttpRequest, we should put the source xml file and the html page we are building on the same domain, otherwise you will get an Access Denied error from your browser. However, that is NOT always true: in my experience FireFox and Safari work perfectly locally, while Chrome and Internet Explorer need the files to be served by ISS on the same domain.
In the mean time, you can try the whole thing!
Happy coding.