Flex 处理数据 访问 XML 数据

 
  1. <!-- 处理数据 访问 XML 数据 -->
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <mx:Application 
  4.     xmlns:mx="http://www.adobe.com/2006/mxml" 
  5.     width="440" height="400"    
  6.     initialize="initializeHandler();"
  7. >
  8.     <mx:Script>
  9.         <![CDATA[
  10.         
  11.             [Bindable] public var a:XMLList; 
  12.             [Bindable] public var b:XMLList;
  13.             [Bindable] public var c:XMLList;
  14.             [Bindable] public var d:XMLList;
  15.             // Model: XML structure describing 
  16.             // some of the books in my collection.    
  17.             [Bindable]
  18.             private var myBooks:XML = 
  19.                 <books>
  20.                     <book ISBN="1590595181">
  21.                         <title>Foundation ActionScript Animation: Making Things Move</title>
  22.                         <author>Keith Peters</author>
  23.                         <amazonUrl>http://tinyurl.com/npuxt</amazonUrl>
  24.                         <pageCount>470</pageCount>
  25.                     </book>
  26.                     <book ISBN="1582346194">
  27.                         <title>Send in the Idiots: Stories from the Other Side of Autism</title>
  28.                         <author>Kamran Nazeer</author>
  29.                         <amazonUrl>http://tinyurl.com/lo5ts</amazonUrl>
  30.                         <pageCount>500</pageCount>
  31.                     </book>
  32.                 </books>
  33.             
  34.             private function initializeHandler():void
  35.             {
  36.                 // An XML list that contains both book nodes.                
  37.                 a = myBooks.book; 
  38.                 
  39.                 // Keith Peters         
  40.                 b = myBooks.book[0].author;
  41.                 
  42.                 // 470
  43.                 c = myBooks.book.(@ISBN=="1590595181").pageCount;
  44.                 // Delete the first book node.
  45.                 delete myBooks.book[0];
  46.                 
  47.                 // Send in the Idiots...
  48.                 d = myBooks.book[0].title; 
  49.             }
  50.         ]]>
  51.     </mx:Script>
  52.     
  53.     <!-- User interface -->
  54.     <mx:Panel 
  55.         title="XML lookup results"
  56.         paddingBottom="10" paddingLeft="10" paddingRight="10" paddingTop="10">
  57.         <mx:Text text="{'a: ' + a}" width="300"/>
  58.         <mx:Label text="{'b: ' + b}"/>        
  59.         <mx:Label text="{'c: ' + c}"/>
  60.         <mx:Label text="{'d: ' + d}"/>
  61.     </mx:Panel>
  62. </mx:Application>
  63. <!--  
  64. @不但可以读取而且可以赋值
  65. 对XML元素与属性赋值
  66. 使用@和点( . )操作符不只可以从XML结构中读取数据的值,也可以为其赋值。
  67. 在下边的例子中,创建一个XML结构master-detail视图。master视图包含一个DataGrid 组件,用来显示书的列表。detail视图包含控件,用来编辑master视图中当前选中的图书。
  68. master 和detail视图使用数据绑定和E4X来读取和更新XML中的数据。
  69. 下边的例子通过一下方法使用E4X:
  70. myBooks.book引用book元素的XMLList对象。 
  71. myBook.book[selectedBookIndex]引用当前被选中的book元素。 
  72. myBook.book[selectedBookIndex].title引用当前被选中book元素的子元素title 
  73. 要使用当前被选中图书的title更新TestInput控件tiltleInput,需要绑定TestInput控件tiltleInput的text属性到myBooks.book[selectedBookIndex].title。类似地,要使用用户最终输入更新XML,当TestInput控件tiltleInput的text属性改变时,将TestInput控件tiltleInput的text属性的值赋值给myBooks.book[selectedBookIndex].title。
  74. detail视图中的另一个组件使用同样的方法正确的工作。
  75. -->
  76. <?xml version="1.0" encoding="utf-8"?>
  77. <mx:Application 
  78.     xmlns:mx="http://www.adobe.com/2006/mxml" 
  79.     width="500" height="470"
  80.     creationComplete="myDataGrid.selectedIndex=0;"
  81. >
  82.     <mx:Script>
  83.         <![CDATA[        
  84.             // Model: XML structure describing 
  85.             // some of the books in my collection.    
  86.             [Bindable]
  87.             private var myBooks:XML = 
  88.                 <books>
  89.                     <book ISBN="1590595181">
  90.                         <title>Foundation ActionScript Animation: Making Things Move</title>
  91.                         <author>Keith Peters</author>
  92.                         <amazonUrl>http://tinyurl.com/npuxt</amazonUrl>
  93.                     </book>
  94.                     <book ISBN="1582346194">
  95.                         <title>Send in the Idiots: Stories from the Other Side of Autism</title>
  96.                         <author>Kamran Nazeer</author>
  97.                         <amazonUrl>http://tinyurl.com/lo5ts</amazonUrl>
  98.                     </book>
  99.                 </books>
  100.         ]]>
  101.     </mx:Script>
  102.     
  103.     <!-- Keep track of the currently selected book -->
  104.     <mx:Number id="selectedBookIndex">{myDataGrid.selectedIndex}</mx:Number>
  105.     
  106.     <!-- User interface -->
  107.     <mx:Panel 
  108.         title="Assigning XML data"
  109.         paddingBottom="10" paddingLeft="10" paddingRight="10" paddingTop="10"
  110.     >
  111.         <!-- Master view: Display all books -->
  112.         <mx:DataGrid id="myDataGrid" dataProvider="{myBooks.book}">
  113.             <mx:columns>
  114.                 <mx:DataGridColumn dataField="@ISBN" headerText="ISBN" width="85"/>
  115.                 <mx:DataGridColumn dataField="title" headerText="Title"/>
  116.                 <mx:DataGridColumn dataField="author" headerText="Author"/>
  117.                 <mx:DataGridColumn dataField="amazonUrl" headerText="Web site">
  118.                     <mx:itemRenderer>
  119.                         <mx:Component>
  120.                             <mx:LinkButton
  121.                                 label="Visit"
  122.                                 click="navigateToURL(new URLRequest(data.amazonUrl), 'blank');"
  123.                             />                                    
  124.                         </mx:Component>
  125.                     </mx:itemRenderer>                        
  126.                 </mx:DataGridColumn>
  127.             </mx:columns>
  128.         </mx:DataGrid>
  129.         
  130.         <!-- Detail view: Display currently selected book for editing -->
  131.         <mx:Form width="100%" autoLayout="false">
  132.             <mx:FormHeading label="Edit book details"/>
  133.             <mx:FormItem label="ISBN:" width="100%">
  134.                 <mx:TextInput 
  135.                     id="isbnInput" 
  136.                     width="100%"
  137.                     text="{myBooks.book[selectedBookIndex].@ISBN}"
  138.                     change="{myBooks.book[selectedBookIndex].@ISBN = isbnInput.text}"
  139.                 />
  140.             </mx:FormItem>
  141.             <mx:FormItem label="Title:" width="100%">
  142.                 <mx:TextInput 
  143.                     id="titleInput" 
  144.                     width="100%"
  145.                     text="{myBooks.book[selectedBookIndex].title}"
  146.                     change="{myBooks.book[selectedBookIndex].title = titleInput.text}"
  147.                 />
  148.             </mx:FormItem>
  149.             <mx:FormItem label="Author:" width="100%">
  150.                 <mx:TextInput 
  151.                     id="authorInput"
  152.                     width="100%"
  153.                     text="{myBooks.book[selectedBookIndex].author}"
  154.                     change="{myBooks.book[selectedBookIndex].author = authorInput.text}"
  155.                 />
  156.             </mx:FormItem>
  157.             <mx:FormItem label="Amazon Url" width="100%">
  158.                 <mx:TextInput 
  159.                     id="amazonUrlInput"
  160.                     width="100%"
  161.                     text="{myBooks.book[selectedBookIndex].amazonUrl}"
  162.                     change="{myBooks.book[selectedBookIndex].amazonUrl = amazonUrlInput.text}"
  163.                 />
  164.             </mx:FormItem>        
  165.         </mx:Form>
  166.     </mx:Panel>
  167. </mx:Application>
  168. <!-- 
  169. 自己创建xml
  170. 通过传递数据引用创建XML对象
  171. 前边介绍XML的例子展示了使用XML文档初始化XML对象的方法。当创建一个XML文档时,你也可以通过引用(来自其他变量的引用)传递数到XML对象中,通过大括号扩起来的变量值引用。
  172. 如果你创建的XML结构不是有效的XML,你会看到类型错误的运行时错误。
  173. 下边的例子动态的创建了一个XML对象。它基于用户提供的变量来创建属性名,属性值,标签名和标签内容。例子检查了TypeError的情况,通过使用try…catch块环绕XML初始化代码。
  174. -->
  175. <?xml version="1.0" encoding="utf-8"?>
  176. <mx:Application 
  177.     xmlns:mx="http://www.adobe.com/2006/mxml" 
  178.     width="500" height="400"    
  179.     initialize="createXML();"
  180. >
  181.     <mx:Script>
  182.         <![CDATA[
  183.             [Bindable]
  184.             public var xml:XML
  185.                         
  186.             private function createXML():void
  187.             {
  188.                 try 
  189.                 {
  190.                     // Create the XML object using the values provided
  191.                     // by the user for the tag name, attribute name,
  192.                     // attribute value and the tag's contents. 
  193.                     xml = 
  194.                         <{tagName.text} 
  195.                             {attributeName.text}={attributeValue.text}
  196.                         >
  197.                             {content.text}
  198.                         </{tagName.text}>;
  199.                 }
  200.                 catch (e:TypeError)
  201.                 {
  202.                     // Type error encountered while trying to create the
  203.                     // XML object. The form must not be valid. Inform 
  204.                     // the user.
  205.                     xml = <note>Fill the form to see the tag here.</note>;
  206.                 }
  207.             }
  208.         ]]>
  209.     </mx:Script>
  210.     <!-- User interface -->
  211.     <mx:Panel 
  212.         title="Passing XML data by reference"
  213.         layout="horizontal"
  214.     >
  215.         <mx:Form>
  216.             <mx:FormItem label="Tag name:">
  217.                 <mx:TextInput id="tagName" change="createXML();"/>                
  218.             </mx:FormItem>
  219.             <mx:FormItem label="Attribute name:">
  220.                 <mx:TextInput id="attributeName" change="createXML();"/>
  221.             </mx:FormItem>
  222.             <mx:FormItem label="Attribute value:">
  223.                 <mx:TextInput id="attributeValue" change="createXML();"/>                
  224.             </mx:FormItem>
  225.             <mx:FormItem label="Tag content:">
  226.                 <mx:TextInput id="content" change="createXML();"/>                
  227.             </mx:FormItem>
  228.             
  229.             <mx:HRule width="100%"/>
  230.             
  231.             <!-- Display the resulting XML -->
  232.             <mx:TextArea 
  233.                 editable="false" 
  234.                 width="300" height="50"
  235.                 text="{xml.toXMLString()}" 
  236.             />
  237.         </mx:Form>
  238.     </mx:Panel>
  239. </mx:Application>
  240. <!-- 
  241. 装配与改变XML对象
  242. 出可以对XML元素与对象赋值外,你也可以通过使用E4X组装与传递XML对象。
  243. 下边的例子中,你可以使用prependChild()和append()方法来向XML对象的列表中的前边或后边增减一个属性。类似的,使用insertChildBefore()方法和insertchildAfter()方法来增加属性在指定的属性之前或之后。要删除元素,使用delete()方法从XML中移除节点。
  244. insertChildBefore
  245. insertChildAfter
  246. prependChild
  247. appendChild
  248. -->
  249. <?xml version="1.0" encoding="utf-8"?>
  250. <mx:Application 
  251.     xmlns:mx="http://www.adobe.com/2006/mxml" 
  252.     width="500" height="600"
  253.     creationComplete="myDataGrid.selectedIndex=0; validateForm();"
  254. >
  255.     <mx:Script>
  256.         <![CDATA[        
  257.             
  258.             import mx.controls.Alert;
  259.         
  260.             // Constants
  261.             private const SELECTED_ITEM:uint = 0;
  262.             private const THE_LIST:uint = 1;
  263.             private const BEFORE:uint = 0;
  264.             private const AFTER:uint = 1;
  265.                 
  266.             // Flag: is the form valid?
  267.             [Bindable]
  268.             private var formIsValid:Boolean;
  269.             
  270.             // Flag: does a selection exist in the data grid?
  271.             [Bindable]
  272.             private var selectionExists:Boolean;
  273.             
  274.             // Holds the index of the next item in the 
  275.             // data grid following the deletion of a book item.
  276.             private var newSelectedIndex:int;
  277.         
  278.             // Model: XML structure describing 
  279.             // some of the books in my collection.    
  280.             [Bindable]
  281.             private var myBooks:XML = 
  282.                 <books>
  283.                     <book ISBN="1590595181">
  284.                         <title>Foundation ActionScript Animation: Making Things Move</title>
  285.                         <author>Keith Peters</author>
  286.                         <amazonUrl>http://tinyurl.com/npuxt</amazonUrl>
  287.                     </book>
  288.                     <book ISBN="1582346194">
  289.                         <title>Send in the Idiots: Stories from the Other Side of Autism</title>
  290.                         <author>Kamran Nazeer</author>
  291.                         <amazonUrl>http://tinyurl.com/lo5ts</amazonUrl>
  292.                     </book>
  293.                 </books>
  294.             
  295.             // Add a new book. 
  296.             private function addBookHandler():void
  297.             {
  298.                 // Create a new XML Object from the form information
  299.                 var newBook:XML = 
  300.                     <book ISBN={isbnInput.text}>
  301.                         <title>{titleInput.text}</title>
  302.                         <author>{authorInput.text}</author>
  303.                         <amazonUrl>{amazonUrlInput.text}</amazonUrl>
  304.                     </book>;
  305.                     
  306.                 // Save the selected book's index so it can be reselected
  307.                 // in the grid once the new book is added.
  308.                 var selectedBookIndex:uint = myDataGrid.selectedIndex;
  309.                                                     
  310.                 // Does the user want to add the new
  311.                 // book relative to the selected book in the
  312.                 // data grid or relative to the list itself?
  313.                 if (addRelativeTo.selectedIndex == SELECTED_ITEM)
  314.                 {
  315.                     // Add the new item relative to the selected item.
  316.                     var selectedBook:XML = myBooks.book[selectedBookIndex];
  317.                                         
  318.                     // Does the user want to add it before or after
  319.                     // the selected item?
  320.                     if (addPosition.selectedIndex == BEFORE)
  321.                     {    
  322.                         // Add new item before selected item 
  323.                         myBooks = myBooks.insertChildBefore(selectedBook, newBook);
  324.                     }
  325.                     else
  326.                     {
  327.                         // Add new item after selected item
  328.                         myBooks = myBooks.insertChildAfter(selectedBook, newBook);
  329.                     }
  330.                 }
  331.                 else
  332.                 {
  333.                     // Add the new item relative to the whole list of books.
  334.                     if (addPosition.selectedIndex == BEFORE)
  335.                     {
  336.                         // Add new item at the start of the list. 
  337.                         myBooks = myBooks.prependChild(newBook);
  338.                     }
  339.                     else
  340.                     {
  341.                         // Add new item at the end of the list.
  342.                         myBooks = myBooks.appendChild(newBook);
  343.                     }                    
  344.                 }
  345.                 // Select the previously selected item in the grid
  346.                 // so the user doesn't lose their position. (If a new book was
  347.                 // added before the currently selected item, that item's
  348.                 // new index will be one greater, so select that.)
  349.                 var newSelectedIndex:uint = (addPosition.selectedIndex == BEFORE) ? selectedBookIndex + 1: selectedBookIndex;
  350.                 myDataGrid.selectedIndex = newSelectedIndex;
  351.                 
  352.             }    
  353.             
  354.             // Delete selected book
  355.             private function deleteBookHandler():void
  356.             {
  357.                 // Save the currently selected index.
  358.                 var selectedBookIndex:int = myDataGrid.selectedIndex;
  359.                                                 
  360.                 // Delete the currently selected book.
  361.                 delete (myBooks.book[selectedBookIndex]);
  362.                 
  363.                 // Reselect the next logical item in the data grid.
  364.                 newSelectedIndex = (selectedBookIndex==0) ? 0 : selectedBookIndex - 1;
  365.                 // Change the selected index of the data grid
  366.                 // at a later frame. See note on changeDataGridIndex()
  367.                 // method for more details on this workaround. 
  368.                 callLater ( changeDataGridIndex );            
  369.             }  
  370.             // This is a workaround for a known issue with
  371.             // List-based components where deleting an item 
  372.             // from the control's dataProvider leaves the
  373.             // selectedIndex at an incorrect value. The workaround
  374.             // is to reassign the data provider at least a 
  375.             // frame later and to change the index there. 
  376.             private function changeDataGridIndex ():void
  377.             {
  378.                 // Reassign the data grid's data provider.
  379.                 myDataGrid.dataProvider = myBooks.book;
  380.                 // Set the selected index.
  381.                 myDataGrid.selectedIndex = newSelectedIndex;
  382.                 // Validate the form to make sure that there
  383.                 // is actually a selection (that the grid is 
  384.                 // not empty).
  385.                 validateForm();
  386.             }
  387.             
  388.             // Perform simple form validation.
  389.             private function validateForm():void
  390.             {
  391.                 // Is the form valid?
  392.                 formIsValid = 
  393.                     isbnInput.text != "" 
  394.                     && titleInput.text != ""
  395.                     && authorInput.text != ""
  396.                     && amazonUrlInput.text != "";
  397.                 
  398.                 // Does a selection exist in the data grid?    
  399.                 selectionExists = myDataGrid.selectedIndex != -1;
  400.             }              
  401.         ]]>
  402.     </mx:Script>
  403.         
  404.     <!-- User interface -->
  405.     <mx:Panel 
  406.         title="Assigning XML data"
  407.         paddingBottom="10" paddingLeft="10" paddingRight="10" paddingTop="10"
  408.     >
  409.         <!-- List of books -->
  410.         <mx:DataGrid 
  411.             id="myDataGrid" 
  412.             dataProvider="{myBooks.book}"
  413.             change="validateForm();"
  414.         >
  415.             <mx:columns>
  416.                 <mx:DataGridColumn dataField="@ISBN" headerText="ISBN" width="85"/>
  417.                 <mx:DataGridColumn dataField="title" headerText="Title"/>
  418.                 <mx:DataGridColumn dataField="author" headerText="Author"/>
  419.                 <mx:DataGridColumn dataField="amazonUrl" headerText="Web site">
  420.                     <mx:itemRenderer>
  421.                         <mx:Component>
  422.                             <mx:LinkButton
  423.                                 label="Visit"
  424.                                 click="navigateToURL(new URLRequest(data.amazonUrl), 'blank');"
  425.                             />                                    
  426.                         </mx:Component>
  427.                     </mx:itemRenderer>                        
  428.                 </mx:DataGridColumn>
  429.             </mx:columns>
  430.         </mx:DataGrid>
  431.         
  432.         <!-- New book form. Prepopulated with a book for easier testing. -->
  433.         <mx:Form width="100%" autoLayout="false">
  434.             <mx:FormHeading label="New book details"/>
  435.             <mx:FormItem label="ISBN:" width="
  436.     100%">
  437.                 <mx:TextInput 
  438.                     id="isbnInput" 
  439.                     width="100%"
  440.                     text="1590596196"
  441.                     change="validateForm();"
  442.                 />
  443.             </mx:FormItem>
  444.             <mx:FormItem label="Title:" width="100%">
  445.                 <mx:TextInput 
  446.                     id="titleInput" 
  447.                     width="100%"
  448.                     text="Object Oriented Actionscript for Flash 8"
  449.                     change="validateForm();"
  450.                 />
  451.             </mx:FormItem>
  452.             <mx:FormItem label="Author:" width="100%">
  453.                 <mx:TextInput 
  454.                     id="authorInput"
  455.                     width="100%"
  456.                     text="Peter Elst"
  457.                     change="validateForm();"
  458.                 />
  459.             </mx:FormItem>
  460.             <mx:FormItem label="Amazon Url" width="100%">
  461.                 <mx:TextInput 
  462.                     id="amazonUrlInput"
  463.                     width="100%"
  464.                     text="http://tinyurl.com/qxon2"
  465.                     change="validateForm();"
  466.                 />
  467.             </mx:FormItem>        
  468.         </mx:Form>
  469.                 
  470.         <mx:TextArea id="deb" width="100%"/>        
  471.         
  472.         <mx:ControlBar>
  473.             <mx:Button 
  474.                 label="Add book"
  475.                 click="addBookHandler();"
  476.                 enabled="{formIsValid}"
  477.             />
  478.             <mx:ComboBox id="addPosition" enabled="{formIsValid}">
  479.                 <mx:String>before</mx:String>
  480.                 <mx:String>after</mx:String>
  481.             </mx:ComboBox>
  482.             <mx:ComboBox 
  483.                 id="addRelativeTo" 
  484.                 enabled="{formIsValid}"
  485.             >
  486.                 <mx:String>selected item.</mx:String>
  487.                 <mx:String>the list.</mx:String>
  488.             </mx:ComboBox>
  489.             <mx:VRule height="15"/>
  490.             <mx:Button 
  491.                 label="Delete book"
  492.                 click="deleteBookHandler();"
  493.                 enabled="{selectionExists}"
  494.             />
  495.         </mx:ControlBar>
  496.     </mx:Panel>
  497. </mx:Application>
  498. <!-- 
  499. 查询XML数据
  500. 在E4X中,查询XML数据有几种方式。下边的例子展示了一下几种方式:
  501. 使用逻辑操作搜索数字范围(例如,所有大于300页的图书) 
  502. 在元素或属性上查询字符串(例如,标题包含ldiots的图书) 
  503. 从重复的节点请求元素(例如,从books中title元素) 
  504. 当你拥有了查询结果,你可以使用for..in和 for each..in循环来遍历结果。
  505. -->
  506. <?xml version="1.0" encoding="utf-8"?>
  507. <mx:Application 
  508.     xmlns:mx="http://www.adobe.com/2006/mxml" 
  509.     viewSourceURL="src/XMLQuery/index.html"
  510.     width="450" height="290"    
  511.     initialize="initializeHandler();"
  512. >
  513.     <mx:Script>
  514.         <![CDATA[
  515.             // Model: XML structure describing 
  516.             // some of the books in my collection.    
  517.             [Bindable]
  518.             private var myBooks:XML = 
  519.                 <books>
  520.                     <book ISBN="1590595181">
  521.                         <title>Foundation ActionScript Animation: Making Things Move</title>
  522.                         <author>Keith Peters</author>
  523.                         <amazonUrl>http://tinyurl.com/npuxt</amazonUrl>
  524.                         <pageCount>470</pageCount>
  525.                     </book>
  526.                     <book ISBN="1582346194">
  527.                         <title>Send in the Idiots: Stories from the Other Side of Autism</title>
  528.                         <author>Kamran Nazeer</author>
  529.                         <amazonUrl>http://tinyurl.com/lo5ts</amazonUrl>
  530.                         <pageCount>500</pageCount>
  531.                     </book>
  532.                     <book ISBN="1590592212">
  533.                         <title>Flash 3D Cheats Most Wanted</title>
  534.                         <author>Aral Balkan et. al.</author>
  535.                         <amazonUrl>http://tinyurl.com/gsd7b</amazonUrl>
  536.                         <pageCount>256</pageCount>
  537.                     </book>
  538.                 </books>
  539.             
  540.             private function initializeHandler():void
  541.             {
  542.                 // Line length to truncate strings at when
  543.                 // displaying them
  544.                 var lineLength:uint = 50;
  545.                 
  546.                 //
  547.                 // Find books with more than 300 pages.                
  548.                 //
  549.                 var resultA:XMLList;
  550.                 resultA = myBooks.book.(pageCount > 300); 
  551.                 
  552.                 // Display the found books using a for each..in
  553.                 // loop.
  554.                 var tempString:String = "<ul>";
  555.                 for each (var book:XML in resultA)
  556.                 {
  557.                     tempString += "<li>" + truncate(book.title, lineLength) + "</li>";
  558.                 }
  559.                 tempString += "</ul>";
  560.                 aText.htmlText = tempString;
  561.                 
  562.                 //
  563.                 // Find book with "Idiots" in the title.         
  564.                 //
  565.                 var resultB:XMLList;
  566.                 resultB = myBooks.book.(title.toString().search("Idiots")>-1);
  567.                 
  568.                 // Display the title of the found book.
  569.                 bText.htmlText = "<ul><li>" + truncate(resultB.title, lineLength) + "</li></ul>";
  570.                 
  571.                 //
  572.                 // Get the titles of all the books.
  573.                 //
  574.                 var resultC:XMLList;
  575.                 resultC = myBooks.book..title;
  576.                 
  577.                 // Display the titles using a for..in loop
  578.                 tempString = "<ul>";
  579.                 for (var bookTitle:String in resultC)
  580.                 {
  581.                     tempString += "<li>" + truncate(resultC[bookTitle], lineLength) + "</li>";
  582.                 }
  583.                 tempString += "</ul>";
  584.                 cText.htmlText = tempString;
  585.             }
  586.             
  587.             // Helper method: Truncate a string at a given character count. Tries
  588.             // to do this intelligently by truncating at a space if one exists in
  589.             // the string (so that words are not truncated in the middle).
  590.             private function truncate ( str:String, numChars:uint, symbol:String = "..." ):String 
  591.             {
  592.                 // Don't do anything if the string is shorter than the maximum value. 
  593.                 if (str.length <= numChars) return str;
  594.                 
  595.                 // Search backward for a space in the string, starting with 
  596.                 // the character position that was requested.
  597.                 var charPosition:uint = numChars-1;
  598.                 while (str.charAt(charPosition) != " " && charPosition != 0)
  599.                 {
  600.                     charPosition--; 
  601.                 }
  602.                 var truncateAt:uint = charPosition == 0 ? numChars : charPosition;
  603.                 
  604.                 // If the space is right before a punctuation mark, crop the
  605.                 // punctuation mark also (or else it looks weird).
  606.                 var charBefore:String = str.charAt(truncateAt-1);
  607.                 if (charBefore == ":" || charBefore == ";" 
  608.                     || charBefore == "." || charBefore == ",")
  609.                 {
  610.                     truncateAt--;
  611.                 }
  612.                 
  613.                 // Truncate the string.
  614.                 var newString:String = str.substr(0, truncateAt);
  615.                 newString += symbol;
  616.                 
  617.                 // Return the truncated string.
  618.                 return newString;
  619.             }
  620.         ]]>
  621.     </mx:Script>
  622.     
  623.     <!-- User interface -->
  624.     <mx:Panel 
  625.         title="XML lookup results" width="100%"
  626.         paddingBottom="10" paddingLeft="10" paddingRight="10" paddingTop="10">
  627.         <mx:Label text="Query A - Books found:" fontWeight="bold"/>
  628.         <mx:Text id="aText" width="100%"/>
  629.         <mx:Label text="Query B - Books found:" fontWeight="bold"/>        
  630.         <mx:Text id="bText" width="100%"/>
  631.         <mx:Label text="Query C - Books found:" fontWeight="bold"/>
  632.         <mx:Text id="cText" width="100%"/>
  633.     </mx:Panel>
  634. </mx:Application>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值