在E4X中,查询XML数据有几种方式。下边的例子展示了一下几种方式:
- 使用逻辑操作搜索数字范围(例如,所有大于300页的图书)
- 在元素或属性上查询字符串(例如,标题包含ldiots的图书)
- 从重复的节点请求元素(例如,从books中title元素)
当你拥有了查询结果,你可以使用for..in和 for each..in循环来遍历结果。
<?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> <!--[CDATA[ // Model: XML structure describing // some of the books in my collection. [Bindable] 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[bookTitle], 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>
1057

被折叠的 条评论
为什么被折叠?



