flex xml

<?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>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值