开发者

working with namespaced XML

i got an xml structure :

<xml>
    <variable>
         <values>
               <enum>
                 <value>2</value>
                 <level>high</level>
               </enum>
               <enum>
                  <value>1</value>
                  <level>medium</level>
               </enum>
               <enum>
                  <value>0</value>
                  <level>low</level>
               </enum>
          </values>
      </variable>
</xml>

now, i passed this into a dataProvider:

namespace degro ="http://www.degro.开发者_StackOverflow社区org/td"; use namespace degro; //this is to bind the namespace

dg.dataProvider = new XMLListCollection(xml.variable.value.enum);

and the dataFields of the dg datagrid are value and level

but it wont show when i run the programme..

anyone experienced this before?


This is Useful link from Adobe Side Using XML namespaces

you may also need to read about QName an objects represent qualified names of XML elements and attributes

Hope that helps


The XML you posted does not identify a namespace. I'm not an actionscript guy, but in other programming languages if you don't specify the correct namespace the serializer is not going to be able to generate output.

You should probably either update your XML to include a namespace definition (i.e. <xml xmlns="http://www.degro.org/td"> or you should set-up your provider to use a blank/empty namespace.


This is how you should access a namespaced XML in flex

private var degroNS:NameSpace = new NameSpace("http://www.degro.org/td");
//
//
//
dg.dataProvider = new XMLListCollection(xml.degroNS::variable.degroNS::value.degroNS::enum);//Assuming xml is the variable name for the xml


This worked for me.Check it out and let me know if that helps

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
               creationComplete="application1_creationCompleteHandler(event)">
    <s:layout>
        <s:VerticalLayout/>
    </s:layout>
    <fx:Script>
        <![CDATA[

            import mx.collections.XMLListCollection;
            import mx.events.FlexEvent;

            private var ns:Namespace = new Namespace("http://www.degro.org/td");

            protected function application1_creationCompleteHandler(event:FlexEvent):void
            {
                dg.dataProvider = new XMLListCollection(dataXML.ns::variable.ns::values.ns::enum);
            }

            private function valueLabelFunction(item:Object,column:DataGridColumn):String
            {
                return item.ns::value+'';
            }

            private function levelLabelFunction(item:Object,column:DataGridColumn):String
            {
                return item.ns::level+'';
            }

        ]]>
    </fx:Script>
    <fx:Declarations>
        <fx:XML id="dataXML">
            <xml xmlns="http://www.degro.org/td">
                <variable>
                    <values>
                        <enum>
                            <value>2</value>
                            <level>high</level>
                        </enum>
                        <enum>
                            <value>1</value>
                            <level>medium</level>
                        </enum>
                        <enum>
                            <value>0</value>
                            <level>low</level>
                        </enum>
                    </values>
                </variable>
            </xml>
        </fx:XML>
    </fx:Declarations>
    <mx:DataGrid id="dg">
        <mx:columns>
            <mx:DataGridColumn headerText="Value" labelFunction="valueLabelFunction"/>
            <mx:DataGridColumn headerText="Level" labelFunction="levelLabelFunction"/>
        </mx:columns>
    </mx:DataGrid>
</s:Application>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜