Hi Kengan,
here is a simple example how to parse the XML-Response:
Code:
import QtQuick 1.1
Rectangle {
width: 360
height: 360
function parseSoundResponse(lyricsNode) {
var lyricsItemData = {}
for(var ii = 1;ii<lyricsNode.length;ii+=2)
{
var resource = lyricsNode[ii].childNodes
var o = new Object()
for(var ik = 1;ik<resource.length;ik+=2)
{
if(resource[ik].nodeName === "Artist")
{
lyricsItemData.artist = resource[ik].firstChild.nodeValue
}
if(resource[ik].nodeName === "Song")
{
lyricsItemData.song = resource[ik].firstChild.nodeValue
}
if(resource[ik].nodeName === "SongUrl")
{
lyricsItemData.song_url = resource[ik].firstChild.nodeValue
}
}
if(lyricsItemData.artist)
lyricsResultListModel.append(lyricsItemData)
}
}
function doRequest(artist, song) {
lyricsResultListModel.clear()
var doc = new XMLHttpRequest();
doc.onreadystatechange = function() {
if (doc.readyState === XMLHttpRequest.HEADERS_RECEIVED) {
} else if (doc.readyState === XMLHttpRequest.DONE) {
var a = doc.responseXML.documentElement;
parseSoundResponse(a.childNodes)
}
}
doc.open("GET", "http://api.chartlyrics.com/apiv1.asmx/SearchLyric?artist="+artist+"&song="+song)
doc.send()
}
ListModel {
id:lyricsResultListModel
}
Rectangle {
id:searchBox
width:parent.width
height:64
anchors.top:parent.top
color:"darkgreen"
border.width:2
border.color:"green"
Row {
spacing:4
anchors.margins: 4
anchors.centerIn: parent
Rectangle {
width:searchBox.width/3
height:48
radius:16
TextInput {
text:"Artist"
id:artistInput
font.pixelSize: 24
width:parent.width
anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter
}
}
Rectangle {
width:searchBox.width/3
height:48
radius:16
TextInput {
text:"Song"
id:songInput
font.pixelSize: 24
width:parent.width
anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter
}
}
Rectangle {
radius:16
color:"gray"
border.width:1
border.color:"darkgray"
width:searchLabel.width
height:48
Text {
id:searchLabel
anchors.centerIn: parent
font.pixelSize: 24
text:"search"
}
MouseArea {
anchors.fill:parent
onClicked: {
doRequest(artistInput.text, songInput.text)
}
}
}
}
}
ListView {
clip:true
width:parent.width
anchors.bottom:parent.bottom
anchors.top:searchBox.bottom
model:lyricsResultListModel
delegate: Item {
width:parent.width
height:2*64
Rectangle { border.width:1;border.color:"black"
anchors.fill:parent
Column {
Text { text:"<b>" + model.artist+"<b>"}
Text { text:"<a href=\"" +model.song_url+"\">"+model.song+"</a>";textFormat:Text.RichText
onLinkActivated: {
Qt.openUrlExternally(link)
}
}
}
}
}
}
}