user3536228
user3536228

Reputation: 73

getting data from xml tags that have : (colon in them)

I am using AIR to build an application that will search and show the thumbnails from deviantART the request URL for getting resuts in form of RSS feed i get the following for simplicity i am showing the only code in which i have my question

we can use this for tag

trace(myxml.channel.item[0].title);

but what to do for tags like media:content

trace(myxml.channel.item[0].media:content);

but it does not work The colons are confusing me

Upvotes: 2

Views: 1058

Answers (3)

subrat71
subrat71

Reputation: 348

first of all if there are colons in xml that is known namespace otherwise it is simple tag. so to pasre data from namespace its little different from simple tags. i can show you some example but if you share the xml format then i can write the appropreate code for you.

here is the sample code:

xmlData.ignoreWhitespace=true;
var awsNS:Namespace = new Namespace("http://www.aws.com/aws");
xmlDataVO.state=xmlData..awsNS::["city-state"];
xmlDataVO.country=xmlData..awsNS::["country"];
xmlDataVO.sunriseHour=xmlData..awsNS::sunrise..awsNS::["hour"].@number;
xmlDataVO.sunriseMinute=xmlData..awsNS::sunrise..awsNS::["minute"].@number;

my xml format was like this

<aws:weather xmlns:aws="http://www.aws.com/aws">
    <aws:city-state citycode="64469">Delhi,  IN</aws:city-state>
    <aws:country>India</aws:country>


    <aws:sunrise>
          <aws:hour number="7" hour-24="07"/>
          <aws:minute number="13"/>
       </aws:sunrise>
 </aws:weather>

Upvotes: 3

helderdarocha
helderdarocha

Reputation: 23637

The colon is used in XML to separate a namespace prefix from a local name in a fully qualified XML name. You can search your documentation for examples on how to deal with XML namespaces.

XML namespaces are declared somewhere in your document using the xmlns attribute. If there are prefixed elements (like media:content) should have an xmlns:prefix='namespace' declaration (like xmlns:media="some-namespace-uri") in the tag you are reading or in an ancestor tag (usually, but not always, the root).

You can probably make it work by reading the namespace from your file by its prefix, and assigning it to a variable. Then you use the double-colon to prefix your selector:

var media:Namespace = myxml.namespace('media');
trace(myxml.channel.item[0].media::content); 

Upvotes: 0

varadig
varadig

Reputation: 82

Try to use XML.elements(name:Object):XMLList

package { import flash.display.Sprite;

public class XmlExample extends Sprite {        
    public function XmlExample() {
        var employees:XML =
            <employees>
                <employee ssn="123-123-1234">
                    <name first="John" last="Doe"/>
                    <address>
                        <street>11 Main St.</street>
                        <city>San Francisco</city>
                        <state>CA</state>
                        <zip>98765</zip>
                    </address>
                </employee>
                <employee ssn="789-789-7890">
                    <name first="Mary" last="Roe"/>
                    <address>
                        <street>99 Broad St.</street>
                        <city>Newton</city>
                        <state>MA</state>
                        <zip>01234</zip>
                    </address>
                </employee>
            </employees>;

        trace(employees.employee[0].address.zip);   // 98765

        trace(employees.employee[1].@ssn);          // 789-789-7890

        trace(employees.employee.name);             // <name first="John" last="Doe"/>
                                                    // <name first="Mary" last="Roe"/>

        trace(employees..zip[0]);                   // 98765

        trace(employees..@ssn[1]);                  // 789-789-7890

        trace(employees..name);                     // <name first="John" last="Doe"/>
                                                    // <name first="Mary" last="Roe"/>

        trace(employees.employee[0].address.*);     // <street>11 Main St.</street>    
                                                    // <city>San Francisco</city>
                                                    // <state>CA</state>
                                                    // <zip>98765</zip>
        var node:String = "zip";
        trace(employees.employee[0].address[node]); // 98765

        var attribute:String = "ssn";
        trace(employees.employee[1].@[attribute]);  // 789-789-7890

        for each (var num:XML in employees..@ssn) {
            trace(num);                             // 123-123-1234
        }                                           // 789-789-7890

        var ssnToFind:String = "789-789-7890";
        trace(employees.employee.(@ssn == ssnToFind).toXMLString());
                                                    // <employee ssn="789-789-7890">
                                                    // <name first="Mary" last="Roe"/>
                                                    // <address>
                                                    // <street>99 Broad St.</street>
                                                    // <city>Newton</city>
                                                    // <state>MA</state>
                                                    // <zip>01234</zip>
                                                    // </address>
                                                    // </employee>
    }
}

}

Upvotes: -1

Related Questions