A
A
Alex Mustdie2015-06-06 14:56:14
ActionScript
Alex Mustdie, 2015-06-06 14:56:14

Why doesn't the DataProvider want to parse XML?

Good day
From this XML, you need to parse only "users":

<response>
<table>
<name>Какая-то строка</name>
</table>
<links>
<xml>
Какая-то ссылка
</xml>
</links>
<users>
<user>
<id>1</id>
</user>
</users>
</response>

To reproduce my mistake:
1. Drag the DataGrid object to the stage
2. In the frame code, enter:
stop();

import fl.controls.DataGrid;
import fl.controls.dataGridClasses.DataGridColumn;
import fl.data.DataProvider;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.*;

var myXML: XML;
var dp: DataProvider;
var dg: DataGrid;
var urlReq: URLRequest;
var myLoader: URLLoader;

myXML = XML("<response><table><name>Какая-то строка</name></table><links><xml>Какая-то ссылка</xml></links><users><user><id>1</id></user></users></response>");

var ids: DataGridColumn = new DataGridColumn("id");
ids.headerText = "id";
ids.width = 100;

dp = new DataProvider(myXML.users);

dg = new DataGrid();
addChild(dg);

dg.x = 0;
dg.y = 0;
dg.width = 550;
dg.height = 454;
dg.headerHeight = 40;
dg.rowHeight = 30;
dg.columns = [ids];
dg.dataProvider = dp;
dg.rowCount = dp.length;

dg.setSize(550, 400);

When compiling, I get the following error:
LuQMwcP.png
Moreover, if the XML looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<response>
<table>
<name>Какая-то строка</name>
</table>
<links>
<xml>
Какая-то ссылка
</xml>
</links>
<user>
<id>1</id>
</user>
</response>

and dp = new DataProvider(myXML), then there are no errors and the result looks like this:
hX1NV8S.png
There are 2 extra rows ("table" and "links")
What to do with it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
copal, 2015-06-06
@alexmustdie

You have a lot of discrepancies in your code, first some values ​​then others, so I did everything at random and if something goes wrong, blame yourself. And also I will not comment on the code, if something is not clear, then look in the help.

stop();

import fl.controls.DataGrid;
import fl.controls.dataGridClasses.DataGridColumn;
import fl.data.DataProvider;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.*;

var myXML: XML;
var dp: DataProvider;
var dg: DataGrid;
var urlReq: URLRequest;
var myLoader: URLLoader;

myXML = <response>
      <table>
        <name>2</name>
      </table>
      <links>
        <xml>6</xml>
      </links>
      <users>
        <user><id>1</id></user>
        <user><id>3</id></user>
      </users>
    </response>;
    
var xml:XML = XML(myXML.users);
trace(xml)

var ids: DataGridColumn = new DataGridColumn("id");
ids.headerText = "id";
ids.width = 100;

dp = new DataProvider(xml);

dg = new DataGrid();
addChild(dg);

dg.x = 0;
dg.y = 0;
dg.width = 550;
dg.height = 454;
dg.headerHeight = 40;
dg.rowHeight = 30;
dg.columns = [ids];
dg.dataProvider = dp;
dg.rowCount = dp.length;

dg.setSize(550, 400);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question