如何通过属性访问子节点?

发布于 2024-12-03 15:27:42 字数 759 浏览 3 评论 0原文

我正在尝试创建一个图片库:

<card>
 <product catalog="Thread Works">
    <name>AK E001</name>    
    <price>45</price>
    <path>assets\cards\AK_E001.jpg</path>
 </product>

 <product catalog="Paper Work">
     <name>AK E001</name>   
    <price>45</price>
    <path>assets\cards\AK_PP003.jpg</path>
 </product>

<product catalog="Thread Works">
    <name>AK E002</name>    
    <price>50</price>
    <path>assets\cards\AK_E002.jpg</path>
 </product>

我可以访问该属性,但如果我选择“Thread Works”,我只想访问 带有子节点,用于将项目添加到数组集合任何帮助谢谢

I am trying to create an image gallery :

<card>
 <product catalog="Thread Works">
    <name>AK E001</name>    
    <price>45</price>
    <path>assets\cards\AK_E001.jpg</path>
 </product>

 <product catalog="Paper Work">
     <name>AK E001</name>   
    <price>45</price>
    <path>assets\cards\AK_PP003.jpg</path>
 </product>

<product catalog="Thread Works">
    <name>AK E002</name>    
    <price>50</price>
    <path>assets\cards\AK_E002.jpg</path>
 </product>

I can access the attribute, but if i am selecting "Thread Works" i want only access
<product catalog="Thread Works">
with children node , for additem to array collection any Help Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

那支青花 2024-12-10 15:27:42

请参阅 http://livedocs.adobe.com/flex/ 3/html/help.html?content=13_Working_with_XML_08.html

在您的情况下:

var x:XML; //用您的数据填充Xml

var threadWorks:XML = x.product.(@catalog=="Thread Works");

编辑:
警告:不要忘记 .产品和@catalog之间

Refer http://livedocs.adobe.com/flex/3/html/help.html?content=13_Working_with_XML_08.html

in your case:

var x:XML; //fill Xml with your data

var threadWorks:XML = x.product.(@catalog=="Thread Works");

EDIT:
Warning: Don't forget the . between product and @catalog

猫九 2024-12-10 15:27:42
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                       xmlns:s="library://ns.adobe.com/flex/spark" 
                       xmlns:mx="library://ns.adobe.com/flex/mx">
    <fx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
            import mx.events.FlexEvent;
            private var mainXML:XML = <card>
                                         <product catalog="Thread Works">
                                            <name>AK E001</name>    
                                            <price>45</price>
                                            <path>assets\cards\AK_E001.jpg</path>
                                         </product>

                                         <product catalog="Paper Work">
                                             <name>AK E001</name>   
                                            <price>45</price>
                                            <path>assets\cards\AK_PP003.jpg</path>
                                         </product>

                                        <product catalog="Thread Works">
                                            <name>AK E002</name>    
                                            <price>50</price>
                                            <path>assets\cards\AK_E002.jpg</path>
                                         </product>
                                        </card>;

            protected function fetchHandler(event:MouseEvent):void
            {
                var productAC:ArrayCollection = new ArrayCollection();
                var productList:XMLList = mainXML.product.(@catalog == attributeNameTxt.text);
                for each(var item:XML in productList)
                {
                    productAC.addItem({name:String(item.name[0]),
                        price:String(item.price[0]),
                        path:String(item.path[0])});
                }
                trace(productAC);
            }

        ]]>
    </fx:Script>
    <s:layout>
        <s:HorizontalLayout/>
    </s:layout>
    <s:TextInput id="attributeNameTxt" text="Thread Works"/>
    <s:Button click="fetchHandler(event);"/>
</s:WindowedApplication>

IMPORTANT CODE : 
=================
mainXML.product.(@catalog == "Thread Works")
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                       xmlns:s="library://ns.adobe.com/flex/spark" 
                       xmlns:mx="library://ns.adobe.com/flex/mx">
    <fx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
            import mx.events.FlexEvent;
            private var mainXML:XML = <card>
                                         <product catalog="Thread Works">
                                            <name>AK E001</name>    
                                            <price>45</price>
                                            <path>assets\cards\AK_E001.jpg</path>
                                         </product>

                                         <product catalog="Paper Work">
                                             <name>AK E001</name>   
                                            <price>45</price>
                                            <path>assets\cards\AK_PP003.jpg</path>
                                         </product>

                                        <product catalog="Thread Works">
                                            <name>AK E002</name>    
                                            <price>50</price>
                                            <path>assets\cards\AK_E002.jpg</path>
                                         </product>
                                        </card>;

            protected function fetchHandler(event:MouseEvent):void
            {
                var productAC:ArrayCollection = new ArrayCollection();
                var productList:XMLList = mainXML.product.(@catalog == attributeNameTxt.text);
                for each(var item:XML in productList)
                {
                    productAC.addItem({name:String(item.name[0]),
                        price:String(item.price[0]),
                        path:String(item.path[0])});
                }
                trace(productAC);
            }

        ]]>
    </fx:Script>
    <s:layout>
        <s:HorizontalLayout/>
    </s:layout>
    <s:TextInput id="attributeNameTxt" text="Thread Works"/>
    <s:Button click="fetchHandler(event);"/>
</s:WindowedApplication>

IMPORTANT CODE : 
=================
mainXML.product.(@catalog == "Thread Works")
扛起拖把扫天下 2024-12-10 15:27:42
function onXmlLoaded (e:Event):void 
{
    xml = XML(e.target.data);
    var xmlLength:Number  = xml.product.length();
    for(var i:uint = 0; i<xmlLength; i++)
    {
            dataContainer.push( {
            catalog : xml.product.@catalog,
            name :  xml..name,
            price : xml..price,
            path : xml..path
        });
    }
    for(i=0;i<dataContainer.length;i++)
    {
           if(dataContainer[i].catalog[0]== "your String"){
                 showName();
           }
    }
}
function onXmlLoaded (e:Event):void 
{
    xml = XML(e.target.data);
    var xmlLength:Number  = xml.product.length();
    for(var i:uint = 0; i<xmlLength; i++)
    {
            dataContainer.push( {
            catalog : xml.product.@catalog,
            name :  xml..name,
            price : xml..price,
            path : xml..path
        });
    }
    for(i=0;i<dataContainer.length;i++)
    {
           if(dataContainer[i].catalog[0]== "your String"){
                 showName();
           }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文