as3corelib Tutorial:How to Use XMLUtil Class in Flex

Address-Book-128x128 XMLUtil class contains methods which encapsulate small pieces of codes to process XML. With xml element, we want to get the previous element or the next element sometimes. However, XML class only provides methods “parent ()” and “children ()” to help use. No such methods exist. Well, the methods “getNextSibling and getPreviousSibling” of XMLUtil provide us simple way to handle this. Another useful method is “isValidXML”, which is used to check whether a string is formatted as XML.

Search-256x256 Demo | DownloadDownload Full Project

Screenshot:

I parse the given XML as a tree. Click the node of the tree to display the current element on the right area. The previous element and next element will be displayed as well, if any.

You can modify the source XML on the left input area. If the XML is valid, the value of isVlidXML is “true”, else the value is “false” which is displayed at the top of the right area.

XMLUtil

Methods:

1. getNextSibling

public static function getNextSibling(x:XML):XML

Returns the next sibling of the specified node relative to the node’s parent.

Parameters:

x:XML The node whose next sibling will be returned.

Returns:

XML The next sibling of the node. null if the node does not have a sibling after it, or if the node has no parent.

2. getPreviousSibling()

public static function getPreviousSibling(x:XML):XML

Returns the sibling before the specified node relative to the node’s parent.

Parameters:

x:XML The node whose sibling before it will be returned.

Returns:

XML The sibling before the node. null if the node does not have a sibling before it, or if the node has no parent.

3. isValidXML()

public static function isValidXML(data:String):Boolean

Checks whether the specified string is valid and well formed XML.

Parameters:

data:String The string that is being checked to see if it is valid XML.

Returns:

Boolean A Boolean value indicating whether the specified string is valid XML.

The following is full source code of XMLUtilDemo.mxml:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application
	backgroundColor="#ffffff"
	xmlns:mx="http://www.adobe.com/2006/mxml"
	creationComplete="initApp()"
	>

	<mx:Script>
		<![CDATA[
			import com.adobe.utils.XMLUtil;		

			private var myXML:XML = XML(
				"<node>"+
					"<node label=\"group1\">"+
						"<node label=\"user1\"/>"+
						"<node label=\"user2\"/>"+
						"<node label=\"user3\"/>"+
					"</node>"+
					"<node label=\"group2\"/>"+
					"<node label=\"group3\"/>"+
				"</node>"
			);

			private function initApp():void
			{
				resetXML();
			}

			private function onXMLTextChange(event:Event):void
			{
				if(XMLUtil.isValidXML(xmlText.text))
				{
					xmlTree.dataProvider = XML(xmlText.text).children();
				}
				else
				{
					xmlTree.dataProvider = null;
				}
			}

			private function resetXML():void
			{
				xmlText.text = myXML.toString();
				xmlTree.dataProvider = XML(xmlText.text).children();
			}

		]]>
	</mx:Script>

	<mx:HBox width="100%" height="100%">
		<mx:TextArea id="xmlText" width="100%" height="100%" change="onXMLTextChange(event)"/>
		<mx:Tree selectedIndex="0" id="xmlTree" width="100%" height="100%" labelField="@label"/>
		<mx:VBox width="100%" height="100%">
			<mx:Text text="isValidXML:{ XMLUtil.isValidXML(xmlText.text) }"/>
			<mx:Button label="reset" click="resetXML()"/>
			<mx:Text text="getPreviousSibling:{ '\n'+XMLUtil.getPreviousSibling(XML(xmlTree.selectedItem)).toXMLString() }"/>
			<mx:Text text="current element:{'\n'+xmlTree.selectedItem.toXMLString()}"/>
			<mx:Text text="getNextSibling:{'\n'+XMLUtil.getNextSibling(XML(xmlTree.selectedItem)).toXMLString()}"/>
		</mx:VBox>
	</mx:HBox>

</mx:Application>
Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • Reddit
  • Technorati
  • StumbleUpon
  • Twitter
RSS Enjoy this Post? Subscribe to Ntt.cc

RSS Feed   RSS Feed     Email Feed  Email Feed Follow us Follow us
You can leave a response, or trackback from your own site.

2 Responses to “as3corelib Tutorial:How to Use XMLUtil Class in Flex”

  1. Jonnie says:

    Great example, I could not find any corelib examples until I found this site.
    -Jonnie

  2. [...] include ArrayUtil, DirectoryUtil, DateUtil, IntUtil, NumberFormatter, StringUtil and XMLUtil [...]

Leave a Reply