开发者

namespaces in nested xml elements

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜