PDA

View Full Version : XmlReader repeating node to array in model



Ocelyn
9 May 2012, 2:20 PM
Hello,

A new day, a new question :

I have my XML who looks like this :




<?xml version="1.0" encoding="utf-8" ?>
<data>
<news>
<title><![CDATA[Hello]]></title>
<author><![CDATA[Ocelyn]]></author>
<content><![CDATA[Lorem ipsum]]></content>
<gallery>
<item><![CDATA[<img src="http://www.ocelyn.com/medias/img/iko/iko-1.jpg"/>]]></item>
<item><![CDATA[<img src="http://www.ocelyn.com/medias/img/ag/1.jpg"/>]]></item>
<item><![CDATA[<img src="http://www.ocelyn.com/medias/img/ag/1.jpg"/>]]></item>
</gallery>
</news>
<news>
...



I want to get all my items and insert them inside of an array

So in my model I did something like that :




fields: [
'title',
'author',
'content',
{
name: 'gallery',
convert: function(value, record) {
var arrayItem;
for( var i = 0; i < record.get('gallery').length; i++){
arrayItem[i] = record.get('gallery')[i];
}


return arrayItem;
}
}
]



But it's not working, is it the good way to do it ?

When I'll try to access to my array in my model should I do something like :

record.get('gallery') ?

Thank you for your help

Ocelyn

Ocelyn
10 May 2012, 9:35 AM
I'm still stuck is someone has an hint on how to do it? There is barely nothing as information about this problem on the whole internet.

Thank you :)

Ocelyn
11 May 2012, 5:06 AM
Finally I foud the solution with the help of a friend, so here is the answer for people who could face the same question :




fields: [
'title',
'imgHome',
'date',
'content',
{
name: 'galleryData',
mapping : 'gallery',
convert: function(value, record) {
var nodes = record.raw.querySelectorAll('gallery item');
var arrayItem = [];
var l = nodes.length;
for( var i = 0; i < l; i++){
var node = nodes[i];
arrayItem.push(nodes[i].textContent);
console.log(nodes[i].textContent);
};


return arrayItem;
}
}
]



Cheers :)

ludm
2 Nov 2012, 5:32 PM
Thank you for the solution, but how to get the data in the arrayItem returned ?