0

I'm trying to make this XML file into an object in AS3.

<?xml version="1.0" encoding="utf-8"?>
<mimeTypes>
    <mimeType>
        <ext>.ico</ext>
        <type>image/x-icon</type>
    </mimeType>
    <mimeType>
        <ext>.txt</ext>
        <type>text/plain</type>
    </mimeType>
    <mimeType>
        <ext>.html</ext>
        <type>text/html</type>
    </mimeType>
</mimeTypes>

The problem is I'm trying to make the ext = the type. For example;

mimeTypes[".ico"] = "image/x-icon";
mimeTypes[".txt"] = "text/plain";
mimeTypes[".html"] = "text/html";

Is there anyway I can do this?

This is my code right now:

var mimeXML = new XML(e.target.data);
var len:uint = mimeXML.mimeType.length();
mimeT[mimeXML.mimeType.child("ext")] = mimeXML.mimeType.child("type");
for(var id:String in mimeT) {
    var value:Object = mimeT[id];
    trace(id + " = " + value);
}

but, it outputs:

<ext>.ico</ext>
<ext>.txt</ext>
<ext>.html</ext> = <type>image/x-icon</type>
<type>text/plain</type>
<type>text/html</type>

Any help would be appreciated, thanks!

2 Answers 2

1

mimeTypes[String(myXMLNode)] = myXMLNode;

in your case:

var xml:XML = 
<mimeTypes>
    <mimeType>
        <ext>ico</ext>
        <type>image/x-icon</type>
    </mimeType>
    <mimeType>
        <ext>txt</ext>
        <type>text/plain</type>
    </mimeType>
    <mimeType>
        <ext>html</ext>
        <type>text/html</type>
    </mimeType>
</mimeTypes>

var myObject:Object = new Object;
for(var i:int = 0; i < xml.mimeType.length(); i++)
{
    var type:Object = new Object;
    type[String(xml.mimeType[i].ext)] = xml.mimeType[i].type;
    myObject[String(xml.mimeType[i].ext)] = type;
    trace(type[String(xml.mimeType[i].ext)]);
}

but you will have to remove the dot from the ext node. it wont work with it...

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

4 Comments

It doesn't seem to work, it outputs the same as before. I need it so more mimeTypes can be added in the XML file, and have the ext equal the type.
var mimetypes:Object = new Object(); is your object you can create a danymic property on objects by casting them as String. So, myObject[String(myXMLNode)] = myXMLNode Where myXMLNode is the property name in the object and you assign it to the contents of the node.
mimeTypes[String(mimeXML.mimeType.child("ext"))] = mimeXML.mimeType.child("type"); ?
Yes I have, it outputs <ext>.ico</ext> <ext>.txt</ext> <ext>.html</ext> = <type>image/x-icon</type> <type>text/plain</type> <type>text/html</type>, the .html equals all three of the types, I want the exts to equal their own types.
0

Have you considered using e4x to get the XML? That would return an object structure that's easily traversed and manipulated.

1 Comment

I'm not sure how I would do this. I just need a simple way to put the ext value with the type value.

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.