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.
-
I'm not understanding your question. What would you expect this function to do? What are the problems you're having with the e4x APIs? Isn't XML, primarily, already untyped?JeffryHouser– JeffryHouser2013-12-21 17:24:06 +00:00Commented Dec 21, 2013 at 17:24
-
no, its XML and its elements are XMLList. I'd like them as Numbers and Strings. Its much slower to use and debug.csomakk– csomakk2013-12-21 17:38:26 +00:00Commented Dec 21, 2013 at 17:38
-
I don't understand how converting XML to "numbers and Strings" will make development easier.JeffryHouser– JeffryHouser2013-12-21 17:42:26 +00:00Commented Dec 21, 2013 at 17:42
-
for example, I have to use xmlList.toString() a lot. The debugger will become faster.csomakk– csomakk2013-12-21 17:43:54 +00:00Commented Dec 21, 2013 at 17:43
-
thetvdb.com/api/GetSeries.php?seriesname=How%20I%20Met this is the api im trying to work with. when i request a detailed view, there are 211 objects. Its pretty slow with XMLcsomakk– csomakk2013-12-21 17:46:10 +00:00Commented Dec 21, 2013 at 17:46
2 Answers
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)!
Comments
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.