PDA

View Full Version : [Off-Topic] Seeking Help Regarding JS + XML Tag Parsing



Kami
04-30-2014, 04:36 PM
I've now spent over ten hours trying to figure this out and it's driving me nuts. I know it shouldn't be this difficult but I don't have much programming experience (let alone with javascript).

Context: I'm trying to use the 'custom script' for the new version of the title plug-in for XSplit.

Anyhow, long story short, I'm hoping someone can provide a basic script I can stick into the Title plug-in. Essentially, I am trying to parse the following XML file to output: "Title - Artist (Album)" and have it auto-update at a configurable interval of time.


<now_playing playing="1" timestamp="2014-04-28T21:28:33-04:00">
<song timestamp="2014-04-28T21:28:33-04:00">
<title>
Antigravitiy ft. Jillian Aversa
</title>
<artist>
zircon
</artist>
<album>
Antigravity
</album>
<genre/>
<kind>
mp3
</kind>
<track/>
<year/>
<comments/>
<time>
270
</time>
<bitrate>
256
</bitrate>
<rating/>
<disc>
65531
</disc>
<numDiscs>
65531
</numDiscs>
<playCount/>
<compilation>
No
</compilation>
<composer/>
<grouping/>
<urlSource/>
<file>
C:\Users\User\Dropbox\Install and Tools\Streaming\Andrew_Aversa_Antigravity_feat_Jil lian_Aversa.mp3
</file>
<artworkID/>
</song>
</now_playing>

The XML file is stored at file location: file://localhost/C:/Program%20Files%20(x86)/Windows%20Media%20Player/plugins/Now%20Playing/now_playing.xml

Any help would be greatly appreciated.

Edit: Yes, I'm aware this is waaaay off-topic but it will come into play when I start streaming HEX again. :)

Werlix
04-30-2014, 05:05 PM
I don't know much about XSplit plugins... but is the language used for the scripts Javascript? Does this help at all:

http://stackoverflow.com/questions/7949752/cross-browser-javascript-xml-parsing#7951947

hacky
04-30-2014, 07:30 PM
I hate parsing XML, so first see if you can output your file as JSON instead. If you find this, then evaluating it in javascript is trivially easy. And if not JSON, a configurable plain text file is even preferable

Failing that, I would take the regular expression route - use a regex that will parse out one of the elements out of the node that you want, for example, the title node.

If you need it specifically parsed in its original schema, it may be easier just to find someone's Xml parsing class and apply that

Kiworrior
04-30-2014, 07:57 PM
So getElementsByTagName(nameString) is the javascript function to get elements within an xml tag.

So in your example you can have something like this

title = xmlDoc.getElementsByTagName("title")[0]; //This will set title equal to the first element in the title tag.
artist = xmlDoc.getElementsByTagName("artist")[0]; //This will set artist equal to the first element in the artist tag.
album = x=xmlDoc.getElementsByTagName("artist")[0]; //This will set album equal to the first element in the album tag.

Then, to format it the way you want, you can just have a string variable that uses the other variables, like this:

displayString = title + " - " + artist + "(" + album + ")";


As for the auto-updating, I assume you already have a script that changes the xml file?? If so, then just run the code again and it will update with the xml file.

Kami
04-30-2014, 10:05 PM
Thanks so much for your help guys!

Finally managed to hack together some code, using your suggestions and rewriting huge chunks of the original 'sample' code that XSplit provided (they used indexOf and substrings - was messy):



/*
This loads xml from [file]
An optional [refresh] in milliseconds may also be set to auto-update
*/
//local file: "file:///C:\\Program%20Files%20(x86)\\Windows%20Media%20Pla yer\\plugins\\Now%20Playing\\now_playing.xml"
var file = "https://localhost/now_playing.xml";
var refresh = 1000;
var timeout;
var oldSong;
function NowPlaying()
{
$.ajax({url: file,
type: "GET",
dataType: "xml",
complete: function()
{
if (refresh > 0)
timeout = setTimeout(function(){NowPlaying(); }, refresh);
},
success: function(newSong)
{
if(newSong.getElementsByTagName("now_playing")[0].getAttribute("playing")!=0)
{
var title = newSong.getElementsByTagName("title")[0].childNodes[0].nodeValue;
var artist = newSong.getElementsByTagName("artist")[0].childNodes[0].nodeValue;
var album = newSong.getElementsByTagName("album")[0].childNodes[0].nodeValue;
var song = title + " - " + artist + " (" + album + ")";
if(oldSong!=song)
{
SetText("false",song, "Location: " + file);
}
oldSong=song;
}
else
{
SetText("false","Now Playing (Offline)", "Location: " + file);
}
}});
}
if (timeout && timeout != null)
{clearTimeout(timeout);}
NowPlaying();

The only question I have left is how to load a locally stored file: file:///C:\\Program%20Files%20(x86)\\Windows%20Media%20Pla yer\\plugins\\Now%20Playing\\now_playing.xml

I'm not even sure I have it formatted correctly. The %20 are obviously spaces.

Kroan
05-01-2014, 12:50 AM
@hacky: Really? RegEx for reading xml? :P
@Kami: I think you have one "/" too many.

hacky
05-01-2014, 01:57 AM
@hacky: Really? RegEx for reading xml? :P

I don't work with javascript often enough to know that there's a built-in XML functions. Regex are awesome though! :D

I have been doing a lot with arbitrary and badly formed XML, most output that I work with, I try to stick to JSON. :)

Kroan
05-01-2014, 02:22 AM
Obviously JSON is a easy and compact way of sharing data. XML has some very powerful options, which are not doable in JSON though. Also it's (mis)used by a lot off older systems. Best is to stick to whatever the tool is outputting :)

Werlix
05-01-2014, 02:05 PM
Obviously JSON is a easy and compact way of sharing data. XML has some very powerful options, which are not doable in JSON though. Also it's (mis)used by a lot off older systems. Best is to stick to whatever the tool is outputting :)

Ugh... I shudder when I see APIs still using XML instead of JSON...

Kroan
05-01-2014, 02:09 PM
It's not XML's fault that people are misusing it left and right. XML was all the hype a couple of years back, and people who didn't know anything about it decided that systems should use it.