XML di Flex

Akhirnya saya bisa meluangkan waktu untuk menulis blogs lagi, banyak sekali kegiatan yang saya lakukan setelah lulus kuliah...senang rasanya bisa menulis kembali di blogs FlexIndonesia. Btw, sudah lebih dari 6 bulan blogs ini tidak saya update..Padahal sekarang Perkembangan FLEX dan AIR semakin canggih..tadinya cman belajar amfPHP dan WebORB, eh skrang sudah ada BLazeDS buat backbone Java...Eh terus skrang udah ada Flex 4....oke sekian dulu ceritanya...

XML di Flex mengikuti aturan ECMAScript for XML (E4X) di helpnya sech dibahas bahwa tujuan menggunakan format E4X, yaitu:

  • Simplicity--Where possible, E4X makes it easier to write and understand code for working with XML data.
  • Consistency--The methods and reasoning behind E4X are internally consistent and consistent with other parts of ActionScript.
  • Familiarity--You manipulate XML data with well-known operators, such as the dot (.) operator.

contoh penggunaan XML pada Flex

var ids:Array = [121, 122, 123]; 
var names:Array = [["Murphy","Pat"], ["Thibaut","Jean"], ["Smith","Vijay"]]
var x:XML = new XML("<employeeList></employeeList>");

for (var i:int = 0; i < 3; i++)
{
var newnode:XML = new XML();
newnode =
<employee id={ids[i]}>
<last>{names[i][0]}</last>
<first>{names[i][1]}</first>
</employee>;

x = x.appendChild(newnode)
}




Mungkin contoh diatas masih simpel, coba lihat code berikut

[Bindable]
private var MenuList:XML=new XML("" +
"<root>" +
"<menuitem label='File' id='file'>" +
<menuitem label='Exit' id='Exit'/>"+
</menuitem>" +
"</root>);



Pertanyaan???


1. Bagaimana menambah menuitem setelah label exit?? supaya hasilnya menjadi...

      <root>
<menuitem label="File" id="file">
<menuitem label="Exit" id="Exit"'/>
<menuitem label="Open" id="Open"/>
</menuitem>
</root>)



Jawab:


untuk bisa menambahkan child setelah label exit kita bisa menggunakan XML method insertChildAfter()

 .....
var newNode:XML = new XML("<menuitem label='Open' id='Open'/>");
var contentNode:XML = MenuList.menuitem.menuitem[0];
var parentNode:XML = MenuList.menuitem[0];
parentNode.insertChildAfter(contentNode, newNode[0] );
....


 




XML methods


The following methods allow you to work with the hierarchical structure of XML objects:


  • appendChild()
  • child()
  • childIndex()
  • children()
  • descendants()
  • elements()
  • insertChildAfter()
  • insertChildBefore()
  • parent()
  • prependChild()

The following methods allow you to work with XML object attributes:


  • attribute()
  • attributes()

The following methods allow you to you work with XML object properties:


  • hasOwnProperty()
  • propertyIsEnumerable()
  • replace()
  • setChildren()

The following methods are for working with qualified names and namespaces:


  • addNamespace()
  • inScopeNamespaces()
  • localName()
  • name()
  • namespace()
  • namespaceDeclarations()
  • removeNamespace()
  • setLocalName()
  • setName()
  • setNamespace()

The following methods are for working with and determining certain types of XML content:


  • comments()
  • hasComplexContent()
  • hasSimpleContent()
  • nodeKind()
  • processingInstructions()
  • text()

The following methods are for conversion to strings and for formatting XML objects:


  • defaultSettings()
  • setSettings()
  • settings()
  • normalize()
  • toString()
  • toXMLString()

There are a few additional methods:


  • contains()
  • copy()
  • valueOf()
  • length()


0 komentar:

Posting Komentar