XML2Object
| Kind of class: | class |
|---|---|
| Inherits from: | XML |
| Author: | Arthur Clemens, Alessandro Crugnola |
| Classpath: | org.asapframework.util.xml.XML2Object |
| File last modified: | Thursday, 12 October 2006, 11:18:45 |
Returns a value object from an XML instance, that can be queried using dot syntax.
If a node has more than 1 child with the same name, an array is created with the children contents
The object created will have this structure:
obj {
nodeName :
attributes : an object containing the node attributes
data : an object containing the node contents
}Usage:
With the following xml file:
When you pass true as convertNumbers argument, the string will be converted to number:
Multiple child nodes of the same parent with the same name will be put into an array. However, there is no way of knowing at parse time whether a single node was meant to be an array. For that purpose, the function makeArray() can be used.
<?xml version="1.0" encoding="utf-8" ?> <days> <subscription num="3">8-8-2004</subscription> </days>
var xmlData:Object = XML2Object.parseXML( anXML ); var subscriptionText = xmlData.days.subscription.data; // returns 8-8-2004 var num = xmlData.days.subscription.attributes.num; // returns the string '3'
When you pass true as convertNumbers argument, the string will be converted to number:
var xmlData:Object = XML2Object.parseXML( anXML, true ); var num = xmlData.days.subscription.attributes.num; // returns the number 3
Multiple child nodes of the same parent with the same name will be put into an array. However, there is no way of knowing at parse time whether a single node was meant to be an array. For that purpose, the function makeArray() can be used.
Example:
XML:
<L1>
<L2>item 1</L2>
<L2>item 2</L2>
</L1>
<L3>
<L4>item 3</L4>
<L3>After parsing, L1.L2 will be an array with two items. L3.L4 will be a single Object. With XML2Object.makeArray(L3.L4) L3.L4 becomes a single item array.Class properties
xml
static xml:XML
(read)
The xml passed in the parseXML method.
Class methods
makeArray
static function makeArray (
inObj) : Array
Check whether the input item is an array, if not, make an array with the input item as single value.
Parameters:
inObj:
Object
Returns:
Array
parseXML
static function parseXML (
inXmlData:XML,
convertNumbers:Boolean) : Object
Creates an value object from an XML instance.
Parameters:
xmlData :
XML instance to parse
convertNumbers:
Set to true to automatically convert number strings to numbers (all text in xml is treated as text otherwise)
Returns:
an Object with the contents of the passed XML
Usage:
XML2Object.parseXML( theXMLtoParse );