This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Getting XML with QtScript (and a collection question)

Tags: None
(comma "," separated)
skerit
Registered Member
Posts
2
Karma
0
Hi everyone,

I'm wondering how I can use QtScript to fetch XML pages from a website (like last.fm)

I know Javascript has some new built-in XML functions, like XMLHttpRequest, but I'm not sure if these work. I haven't been able to try it. Qt also has a special xml binding (com.trolltech.qt.xml) but I'm not sure if that's able to fetch information or if it's just able to process it.

I also need to iterate through an entire Amarok collection. I used to do this through SQL in a simple Ruby script, but I don't think Amarok lets you access the database like that any more. Is there another way?

Thank you for your time.
skerit
Registered Member
Posts
2
Karma
0
I've been able to answer the first part of my question, downlaoding and processing XML seems a lot easier in QtScript then in Ruby.

For anyone interested, this is the small part which fetches the an xml, cycles through every "chart" element and prints the "from" attribute of that element.


Importer.loadQtBinding( "qt.core" );
Importer.loadQtBinding( "qt.xml" );

user = "skerit"
xmlUrl = new QUrl( "http://ws.audioscrobbler.com" );
http  = new QHttp;
data  = new QIODevice;
doc    = new QDomDocument("doc");

weeks = new QDomNodeList;
tracks  = new QDomNodeList;

qurl = new QUrl( xmlUrl + "/2.0/user/" + "skerit" + "/weeklychartlist.xml" );
a = new Downloader( qurl , xmlDownloadResult );

function xmlDownloadResult( reply ) {
  try {
    doc.setContent( reply );

    weeks = doc.elementsByTagName( "chart" );
    print ("Got " + weeks.length() + " total weeks scrobbled");

    var j = 0;
    for ( ; j <= weeks.length(); j++ ) {
print ("Vanaf: " + weeks.item(j).toElement().attribute("from")); /* Returns the from attribute */
    }

  }
  catch( err ) {
    Amarok.debug( err );
  }
  xmlFetched == 1;
}


But now I'm looking for a way to *save* that XML in a file, on the disk. I've been looking up on QFile and QTextStream, but I can't make heads or tails of it.


Bookmarks



Who is online

Registered users: Bing [Bot], gfielding, Google [Bot], markhm, Sogou [Bot], Yahoo [Bot]