Reputation: 5497
Anyone knows about something like JSON.parse() for XML? Its very annoying to work with XML API-s without a good parse function. I found a reference to SimpleXML, but couldnt find it anywhere. What I need is basically to build up an untyped object from the XML of an API.
Upvotes: 0
Views: 130
Reputation: 412
We had some luck on a recent project using the mx.rpc.xml.XMLDecoder with a custom Schema.
Our dynamic domain object would extend ObjectProxy. Notice that we have not defined a 'firstAired' Date property.
package com.sample.model
{
import mx.utils.ObjectProxy;
[Bindable("propertyChange")]
public dynamic class Series extends ObjectProxy
{
public var seriesId : String;
public var name : String;
public var overview : String;
}
}
We would define our XML Schema (In our project the schema was returned with the results):
var schemaXML : XML = <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="data">
<xs:complexType>
<xs:sequence>
<xs:element name="series" type="series" maxOccurs="unbounded" minOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="series">
<xs:sequence>
<xs:element name="seriesId" type="xs:string"/>
<xs:element name="name" type="xs:string"/>
<xs:element name="firstAired" type="xs:date"/>
<xs:element name="overview" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>;
A sample XML result would look like:
var seriesXML : XML = <data>
<series>
<seriesId>75760</seriesId>
<name>How I Met Your Mother</name>
<firstAired>2005-09-19</firstAired>
<overview>It all started when Ted's best friend...</overview>
</series>
</data>;
As results were received we put the Decoder to work:
var schema : Schema = new Schema(schemaXML);
var schemaManager : SchemaManager = new SchemaManager();
schemaManager.addSchema(schema);
var decoder : mx.rpc.xml.XMLDecoder = new mx.rpc.xml.XMLDecoder();
decoder.schemaManager = schemaManager;
var schemaTypeRegistry : SchemaTypeRegistry = SchemaTypeRegistry.getInstance();
schemaTypeRegistry.registerClass(new QName(schema.targetNamespace.uri, 'series'), Series);
var data : Array = decoder.decode(seriesXML, new QName(schema.targetNamespace.uri, 'data'));
Inspecting the 'data' Array notice the 'firstAired' property correctly typed as a Date:
(I'm new to StackOverflow and can't post images so you'll just have to believe me)!
Upvotes: 1
Reputation: 3385
Try this XMLParser link.
By using this Every node becomes an array with the same name. All attributes are also easily accessible because they become properties with the same name.
Upvotes: 0