Sie sind nicht angemeldet.

1

Mittwoch, 16. Januar 2013, 18:46

Parsing XML parameters by dynamic created variable names

Hello,

I want to parse XML parameters by dynamic created variable names.

The following snippet code is from the tour.xml:

Quellcode

1
2
3
4
5
<map_settings viewpoint1_xy="30,180"
        viewpoint1_heading="0"
        viewpoint3_xy="110,180"
        viewpoint3_heading="0"
    />


I want to parse these parameters (e.g., viewpoint1_xy) in the vtourskin.xml with the following code: (one level deeper than tour.xml)

Quellcode

1
2
3
4
5
6
7
8
9
10
11
12
for(set(i,0), i LT 5, inc(i),
        
            <!-- map_settings.viewpoint[cnt]_xy -->
            add(cnt,get(i),1);
            txtadd(viewpointSetting,'map_settings.viewpoint',get(cnt),'_xy');
            
            trace(get(viewpointSetting));
        
            <!-- set viewpoint position -->
            indexoftxt(pos,get(viewpointSetting), ',');
            trace('pos: ',get(pos));
);


And the output of the debug console looks like this:

Quellcode

1
2
3
4
5
6
7
8
9
INFO: 30,180 INFO: pos: -1
INFO: map_settings.viewpoint2_xy
INFO: pos: -1
INFO: 110,180
INFO: pos: -1
INFO: map_settings.viewpoint4_xy
INFO: pos: -1
INFO: map_settings.viewpoint5_xy
INFO: pos: -1


The problem is, that indexoftxt always results in pos = -1, because it's not searching in the value of get(viewpointSetting) but in the variable name.

e.g., indexoftxt(pos,'map_settings.viewpoint1_xy', ','); NOT indexoftxt(pos,'30,180', ',');

Seems like the expression get(viewpointSetting) is not executed before indexoftxt?

Why?

Thanks!

Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von »bafu« (16. Januar 2013, 18:56)


toosten

Fortgeschrittener

Beiträge: 521

Wohnort: Berlin

Beruf: Software-Entwickler bei VR-Easy ( HTML, JS, PHP, krpano, C++, Java )

  • Nachricht senden

2

Donnerstag, 17. Januar 2013, 08:46

Quellcode

1
2
3
4
5
6
7
8
9
10
<map_settings name="viewpoint1_xy" value="30,180"
        viewpoint1_heading="0"
        viewpoint3_xy="110,180"
        viewpoint3_heading="0"
    />
<map_settings name="viewpoint2_xy" value="30,180"
        viewpoint1_heading="0"
        viewpoint3_xy="110,180"
        viewpoint3_heading="0"
    />


Quellcode

1
2
3
for(set(i,0), i LT 5, inc(i),
    trace( map_settings[get(i)].name, ', ', map_settings[get(i)].value);
);


P.S.

Quellcode

1
2
3
4
for(set(i,0), i LT 5, inc(i),
set( n, map_settings[get(i)].name);
trace( map_settings[get(i)].name, ', ', map_settings[get(n)].value);
);

3

Donnerstag, 17. Januar 2013, 09:08

Thank you very much toosten!!

4

Donnerstag, 17. Januar 2013, 10:24

Hi,

to clear up why the code in the first post wasn't working, here a copy of my answering-mail to the same question you had also sent by mail:

In this call:

Quellcode

1
indexoftxt(pos,get(viewpointSetting), ','); 

the get() will be resolved to:

Quellcode

1
indexoftxt(pos, map_settings.viewpoint2_xy, ','); 

That means the name of the variable itself will be searched and not the content of the variable itself.


To search the content of the variable, it must be first read-out/copied to an other variable (because a get(get(var)) is not possible).
e.g. this can be done by such code:

Quellcode

1
copy(varcontent, get(viewpointsetting)); 

After resolving the get(), the code will look like:

Quellcode

1
copy(varcontent, map_settings.viewpoint2_xy); 

And with that code, the content from 'map_settings.viewpoint2_xy' will be copied to 'varcontent'.
That new 'varcontent' variable can now by used to as get(varcontent) in the indexoftxt() call.


That means the final working code will look like this:

Quellcode

1
2
3
txtadd(viewpointsetting, 'map_settings.viewpoint', get(cnt), '_xy'); 
copy(varcontent, get(viewpointsetting)); 
indexoftxt(pos, get(varcontent), ',');

Best regards,
Klaus