0

I have seen afew answers to the problem i am having now but mine is that of a nested one.

I have an xml looking like this:

> 
<em>
<type xmlns="http://www.sitcom-project.org/sitcom" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <model>
    <implemented_with>comoco</implemented_with>
    <implemented_in>perl</implemented_in>
    <cash_flow>casual</cash_flow>
    <interaction>conventional</interaction>
  </model>
</type>
</em>

now, how do i access the element of the implemented_with node?

ofc i could access the xmlList this way: namespace ns = www.sitcom-project.org/sitcom; type.ns::model; but now, how do i access the implemented_with node in the model xmlList? i tried type.ns::model.implemented_with, but didn't work. Anyone got any idea? thanks

1 Answer 1

1

There are a couple ways to do this, but the best way is to use a namespace prefix before each dot access. In your case the first thing you want to do is to isolate the namespace. You can do this by hard coding the namespace into a new namespace object... ie;

var ns:Namespace = new Namespace( "http://www.sitcom-project.org/sitcom" );

Or a better way is to just extract it from the appropriate node. In the following code I am getting all the namespaces (as an array) declared on the type node, and just targeting the first one in the list. Because I don't know the namespace beforehand, I have to retrieve it using the children() method.

var emNode:XML = _yourXML.em[0];
var typeRoot:XML = emNode.children()[0]; 
var ns:Namespace = typeRoot.namespaceDeclarations()[0];

Once you have accomplished this, you can use namespace syntax to dig into your model.

var impWith:String = typeRoot.ns::model.ns::implemented_with;

This can be a little verbose, so you can set the default namespace using the following syntax. I don't like this personally, but it does work.

default xml namespace = ns;
var impWith:String = typeRoot.model.implemented_with;
default xml namespace = null;

A simple one-liner could be.

var ns:Namespace = new Namespace("http://www.sitcom-project.org/sitcom");
var imp:String = _yourXML.em[0].ns::type.ns::model.ns::implemented_with;

Using the default syntax

default xml namespace = new Namespace("http://www.sitcom-project.org/sitcom");
var imp:String = _yourXML.em[0].type.model.implemented_with;
default xml namespace = null;

Hope this helps.

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

2 Comments

ActionScript has a catch with default xml namespace. When you finished with it, you must reset it to empty namespace, or runtime errors may occur.
Edited my answer to reflect your comment

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.