---
title: "flex    xml"
author: "yaoweinan"
date: "2011-03-18T11:46:39Z"
tags: []
categories: ["Web前端"]
slug: "flex-xml"
draft: false
lastmod: "2011-03-18T11:46:39Z"
id: 966227
url: "https://ilyon.iteye.com/blog/966227"
---

<?xml version="1.0" encoding="utf-8"?>  
<mx:Application  
    xmlns:mx="http://www.adobe.com/2006/mxml"  
    viewSourceURL="src/XMLQuery/index.html"  
    width="450" height="290"     
    initialize="initializeHandler();"  
>  
    <mx:Script>  
        <!--  
            private var myBooks:XML =  
                <books>  
                    <book ISBN="1590595181">  
                        <title>Foundation ActionScript Animation: Making Things Move</title>  
                        <author>Keith Peters</author>  
                        <amazonUrl>http://tinyurl.com/npuxt</amazonUrl>  
                        <pageCount>470</pageCount>  
                    </book>  
                    <book ISBN="1582346194">  
                        <title>Send in the Idiots: Stories from the Other Side of Autism</title>  
                        <author>Kamran Nazeer</author>  
                        <amazonUrl>http://tinyurl.com/lo5ts</amazonUrl>  
                        <pageCount>500</pageCount>  
                    </book>  
                    <book ISBN="1590592212">  
                        <title>Flash 3D Cheats Most Wanted</title>  
                        <author>Aral Balkan et. al.</author>  
                        <amazonUrl>http://tinyurl.com/gsd7b</amazonUrl>  
                        <pageCount>256</pageCount>  
                    </book>  
                </books>  
             
            private function initializeHandler():void  
            {  
                // Line length to truncate strings at when  
                // displaying them  
                var lineLength:uint = 50;  
                 
                //  
                // Find books with more than 300 pages.                 
                //  
                var resultA:XMLList;  
                resultA = myBooks.book.(pageCount > 300);  
                 
                // Display the found books using a for each..in  
                // loop.  
                var tempString:String = "<ul>";  
                for each (var book:XML in resultA)  
                {  
                    tempString += "<li>" + truncate(book.title, lineLength) + "</li>";  
                }  
                tempString += "</ul>";  
                aText.htmlText = tempString;  
                 
                //  
                // Find book with "Idiots" in the title.          
                //  
                var resultB:XMLList;  
                resultB = myBooks.book.(title.toString().search("Idiots")>-1);  
                 
                // Display the title of the found book.  
                bText.htmlText = "<ul><li>" + truncate(resultB.title, lineLength) + "</li></ul>";  
                 
                //  
                // Get the titles of all the books.  
                //  
                var resultC:XMLList;  
                resultC = myBooks.book.title;  
                 
                // Display the titles using a for..in loop  
                tempString = "<ul>";  
                for (var bookTitle:String in resultC)  
                {  
                    tempString += "<li>" + truncate(resultC, lineLength) + "</li>";  
                }  
                tempString += "</ul>";  
                cText.htmlText = tempString;  
            }  
             
            // Helper method: Truncate a string at a given character count. Tries  
            // to do this intelligently by truncating at a space if one exists in  
            // the string (so that words are not truncated in the middle).  
            private function truncate ( str:String, numChars:uint, symbol:String = "..." ):String  
            {  
                // Don't do anything if the string is shorter than the maximum value.  
                if (str.length <= numChars) return str;  
                 
                // Search backward for a space in the string, starting with  
                // the character position that was requested.  
                var charPosition:uint = numChars-1;  
                while (str.charAt(charPosition) != " " && charPosition != 0)  
                {  
                    charPosition--;  
                }  
                var truncateAt:uint = charPosition == 0 ? numChars : charPosition;  
                 
                // If the space is right before a punctuation mark, crop the  
                // punctuation mark also (or else it looks weird).  
                var charBefore:String = str.charAt(truncateAt-1);  
                if (charBefore == ":" || charBefore == ";"  
                    || charBefore == "." || charBefore == ",")  
                {  
                    truncateAt--;  
                }  
                 
                // Truncate the string.  
                var newString:String = str.substr(0, truncateAt);  
                newString += symbol;  
                 
                // Return the truncated string.  
                return newString;  
            }  
        ]]-->  
    </mx:Script>  
     
    <!-- User interface -->  
    <mx:Panel  
        title="XML lookup results" width="100%"  
        paddingBottom="10" paddingLeft="10" paddingRight="10" paddingTop="10">  
        <mx:Label text="Query A - Books found:" fontWeight="bold"/>  
        <mx:Text id="aText" width="100%"/>  
        <mx:Label text="Query B - Books found:" fontWeight="bold"/>         
        <mx:Text id="bText" width="100%"/>  
        <mx:Label text="Query C - Books found:" fontWeight="bold"/>  
        <mx:Text id="cText" width="100%"/>  
    </mx:Panel>  
</mx:Application>