1. #1
    Sencha User
    Join Date
    Feb 2012
    Posts
    21
    Answers
    3
    Vote Rating
    1
    swardson is on a distinguished road

      0  

    Default Unanswered: Read nested XML with Model and Store

    Unanswered: Read nested XML with Model and Store


    Hey all,

    I have an xml file I am reading. Here it is:

    Code:
    <?xmk version="1.0" encoding="UTF-8"?>
    <course>
    <title></title> <pages>
    <page>
    <id>01</id> <type>content</type> <title>Title 1</title> <url>path/to/file.html</url> <options>
    <option>option1</option> <option>option2</option> <option>option3</option>
    </options>
    </page> <page>
    <id>02</id> <type>content</type> <title>Title 2</title> <url>path/to/file.html</url> <options>
    <option>option1</option> <option>option2</option> <option>option3</option>
    </options>
    </page>
    </pages>
    </course>
    Currently I am reading the xml with an xml proxy reader in my store by setting the root to 'pages' and the record to 'page' However, I want to read in the options elements as well. I thought about creating another model for options and a store that uses options as root and option as record. but I need that information tied to the page record.

    I tried setting the data type for options to be 'auto' in my model but it's not reading the data. Is this a job for associations? or is there a more simple way to pull the data in?

    Thanks!

  2. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,022
    Answers
    3491
    Vote Rating
    849
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    The issue probably is that it doesn't know what fields to look for in the <options> tag
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.

  3. #3
    Sencha User
    Join Date
    Feb 2012
    Posts
    21
    Answers
    3
    Vote Rating
    1
    swardson is on a distinguished road

      0  

    Default


    Okay, after doing some more research I am pretty sure I need associated models to achieve this. After writing some more code toward this end I have run into an interesting wall. I am expecting it's a simple mistake.

    First of all I changed the <options> and <option> tags in the xml to <answers> and <answer> to prevent confusion with some other variables I was using in my Controller.

    Here is the new model structure:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <course>
    <title></title> <pages>
    <page>
    <id>01</id> <type>content</type> <title>Title 1</title> <url>path/to/file.html</url> <answers>
    <answer>Answer 1</answer> <answer>Answer 2</answer> <answer>Answer 3</answer> <answer>Answer 4</answer>
    </options>
    </page> <page>
    <id>02</id> <type>content</type> <title>Title 2</title> <url>path/to/file.html</url> <answers>
    <answer>Answer 1</answer> <answer>Answer 2</answer> <answer>Answer 3</answer> <answer>Answer 4</answer>
    </options>
    </page>
    </pages>
    </course>
    I created a new model with the fields needed to read the answers and I had to create a proxy for it because the data wasn't being read in otherwise. (This might be my problem, I am not sure). I also read in another thread that you need to create a field in the parent Model that matches the associated model. (This also could be a problem, not sure).

    PHP Code:
    app.models.answers Ext.regModel('answers', {
        
    fields: [
            {
    name'answer'type'string' },
        ],
        
    proxy: {
            
    type'ajax',
            
    url 'assets/xml/course.xml',
            
            
    reader: {
                
    type'xml',
                
    //root: 'answers',
                
    record'answer'
            
    },
        },
    });

    app.models.pages Ext.regModel('pages', {
        
    fields: [
            {
    name'id'type'string' },
            {
    name'type'type'string' },
            {
    name'title'type'string' },
            {
    name'url'type'string' },
            {
    name'question'type'string' },
            {
    name'answers'type'auto' },
            {
    name'correct'type'int' },
            {
    name'tries'type'int' },        
        ],
        
        
    hasMany: {model'answers'name'answers'},
    }); 
    Now in my controller I create a new answers object using the record.answers() getter function that is auto generated. when I look at the new answers.data it's an array of objects. If I drill down to the data with answers.data.items[0] I can see the answer field but it is blank text, but the pages_id shows the correct id from the <page> object. However, if I drill to answers.data.items[0].raw it returns the raw xml properly. <answer>Answer 1</answer>. What this tells me is the data is being read properly, but the answer field isn't being populated with the text in the <answer> tag.

    Any help out there?

  4. #4
    Sencha User
    Join Date
    Feb 2012
    Posts
    21
    Answers
    3
    Vote Rating
    1
    swardson is on a distinguished road

      0  

    Default


    Anything?

    It would seem that reading nested xml structure would be more common and easier to accomplish given the fact that most of the power of xml is in nesting structures.

    Thanks!

Thread Participants: 1

Tags for this Thread