Skip to content

Instantly share code, notes, and snippets.

@gordonbisnor
Created March 22, 2024 19:13
Show Gist options
  • Save gordonbisnor/0cc81eb052cf6965004d4eca26b7b4ff to your computer and use it in GitHub Desktop.
Save gordonbisnor/0cc81eb052cf6965004d4eca26b7b4ff to your computer and use it in GitHub Desktop.
Rafflebox XML to Javascript POJO example (in Ember.js)
import Component from '@glimmer/component';
import fetch from 'fetch';
import { tracked } from '@glimmer/tracking';
export default class HomeBanner extends Component {
url = 'https://ticker.rafflebox.ca/xml?raffleId={id from rafflebox url goes here}';
@tracked draw;
constructor() {
super(...arguments);
fetch(this.url).then((response) => response.text())
.then((str) => (new window.DOMParser()).parseFromString(str, "text/xml"))
.then((data) => {
this.draw = this.parseXMLToPOJO(data);
})
.catch((error) => {
console.error('Error fetching XML:', error);
});
}
parseXMLToPOJO(xmlString) {
const draw = xmlString.getElementsByTagName("draw")[0];
const pojo = {
name: draw.getAttribute("name"),
drawId: draw.getAttribute("drawid"),
startDate: new Date(draw.getElementsByTagName("startdate")[0].textContent),
endDate: new Date(draw.getElementsByTagName("enddate")[0].textContent),
total: draw.getElementsByTagName("total")[0].textContent,
jackpot: draw.getElementsByTagName("jackpot")[0].textContent,
formattedJackpot: draw.getElementsByTagName("formattedjackpot")[0].textContent,
winningTicket: draw.getElementsByTagName("winningticket")[0].textContent,
raisedToDate: draw.getElementsByTagName("raisedtodate")[0].textContent,
totalToDate: draw.getElementsByTagName("totaltodate")[0].textContent,
status: draw.getElementsByTagName("status")[0].textContent,
};
// Handling empty elements to ensure they're represented correctly in the POJO
Object.keys(pojo).forEach(key => {
if (pojo[key] === "") {
pojo[key] = null;
}
});
return pojo;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment