0

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.

6
  • 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? Commented 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. Commented Dec 21, 2013 at 17:38
  • I don't understand how converting XML to "numbers and Strings" will make development easier. Commented Dec 21, 2013 at 17:42
  • for example, I have to use xmlList.toString() a lot. The debugger will become faster. Commented 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 XML Commented Dec 21, 2013 at 17:46

2 Answers 2

1

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)!

Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

That is an AS2 class; whereas Flex is AS3. To quote from your link "If you need an AS3 version of this class, you can get it HERE (although the way AS3 handles XML makes this class largely unnecessary)". I'm not grokking what the original poster is trying to accomplish.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.