views:

63

answers:

3

Hi,

I have a code which is written for read the xml data. and the xml file contains the optional values, so some time the elements are present there some times not. In this case how can i set a default value for that in action script 3.0.

When i tried to trace the value that area is skipping. So is there any other method to trace-out is that element is present or not something like that??

Edit:

private function xmlListener(evt:Event) {
    xmlValue = new XML(evt.target.data);
    _videoURL = xmlValue.videoUrl;
    _setWidth = xmlValue.setWidth;
    _setHeight = xmlValue.setHeight;
}

Suppose the set 'setHeight' element is not present in the XML then what would be the value of _setHeight ??

Edit 2:

<?xml version="1.0" encoding="UTF-8"?>
<settings>
    <videoUrl>videos.flv</videoUrl>
    <setWidth>500</setWidth>
</settings>
A: 
if(xmlElement != null){

// do stuff here if it has a set value

}else{
// if it is empty then give it a value like this:

xmlElement = defultvalue;
}
bob
I don't think the the null thing works, I think you need to check for undefined. or you can do what I do in my answer
invertedSpear
ps, I wasn't the downvote.
invertedSpear
the undefined and null are not working, you may please check by writting your own codes, I failed if conditions that is why am here :(
coderex
@coderex, did you try using my other answer?
invertedSpear
+1  A: 
_setHeight = (xmlValue.setHeight.length() >0)?xmlValue.setHeight:0;
invertedSpear
A: 

When trying to access XML nodes that may not exist you should try using the child(), attribute() and element() functions from the XML class. Either of these returns an XMLList and if the specified node doesn't exist the XMLList is simply empty (with a length of 0) instead of the Flash Player immediately barfing up an Error.

From Flex Livedocs - Traversing XML structures :

If you try to filter on attributes or elements that may not exist, Flash® Player and Adobe® AIR™ will throw an exception. For example, the final line of the following code generates an error, because there is no id attribute in the second p element:

var doc:XML = 
            <body>
                <p id='123'>Hello, <b>Bob</b>.</p>
                <p>Hello.</p>
            </body>;
trace(doc.p.(@id == '123'));

Similarly, the final line of following code generates an error because there is no b property of the second p element:

var doc:XML = 
            <body>
                <p id='123'>Hello, <b>Bob</b>.</p>
                <p>Hello.</p>
            </body>;
trace(doc.p.(b == 'Bob'));

To avoid these errors, you can identify the properties that have the matching attributes or elements by using the attribute() and elements() methods, as in the following code:

var doc:XML = 
            <body>
                <p id='123'>Hello, <b>Bob</b>.</p>
                <p>Hello.</p>
            </body>;
trace(doc.p.(attribute('id') == '123'));
trace(doc.p.(elements('b') == 'Bob'));

You can also use the hasOwnProperty() method, as in the following code:

var doc:XML = 
            <body>
                <p id='123'>Hello, <b>Bob</b>.</p>
                <p>Hello.</p>
            </body>;
trace(doc.p.(hasOwnProperty('@id') && @id == '123'));
trace(doc.p.(hasOwnProperty('b') && b == 'Bob'));
Baelnorn