namespaces in nested xml elements - actionscript-3

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.

Related

Details about the json:Array feature of Newtonsoft.JSON XML to JSON converter

Referencing this example of using "json:Array": Converting between JSON and XML
I have two questions:
Does the namespace have to be "json"? I.e. if ns2 matched back to
"xmlns:ns2='http://james.newtonking.com/projects/json'" would that work?
Can the namespace be omitted? Can I just put "Array='true'"?
I'm about to try to test by trial and error, but thought maybe somebody would know the answer, or someone in the future would like to know.
Not that it matters a whole lot, but my XML is being generated by BizTalk 2010 and I'm using a WCF CustomBehavior to call NewtonSoft as follows:
private static ConvertedJSON ConvertXMLToJSON(string xml)
{
// To convert an XML node contained in string xml into a JSON string
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
ConvertedJSON convertedJSON = new ConvertedJSON();
convertedJSON.JSONtext = JsonConvert.SerializeXmlNode(doc, Newtonsoft.Json.Formatting.None);
convertedJSON.rootElement = doc.DocumentElement.Name;
return convertedJSON;
}
Looks like the namespace has to be exactly what they provide:
string xmlToConvert2 = "<myRoot xmlns:json='http://james.newtonking.com/projects/json'><MyText json:Array='true'>This is the text here</MyText><Prices><SalesPrice>10.00</SalesPrice></Prices></myRoot>";
string strJSON2 = ConvertXMLToJSON(xmlToConvert2);
As with normal xml, the namespace prefix can be any value. The follow worked equally as well as the above.
string xmlToConvert3 = "<myRoot xmlns:abc='http://james.newtonking.com/projects/json'><MyText abc:Array='true'>This is the text here</MyText><Prices><SalesPrice>10.00</SalesPrice></Prices></myRoot>";
string strJSON3 = ConvertXMLToJSON(xmlToConvert3);

Getting all of the variables of a dart class for json encoding

I have a class
class Person{
String _fn, _ln;
Person(this._fn, this._ln);
}
Is there a way to get a list of variables and then serialize it? Essentially i want to make a toJson, but i wanted to have it generic enough such that key is the variable name, and the value is the value of the variable name.
In javascript it would be something like:
var myObject = {}; //.... whatever you want to define it as..
var toJson = function(){
var list = Object.keys(myObject);
var json = {};
for ( var key in list ){
json[list[key]] = myObject[list[key]] ;
}
return JSON.stringify(json);
}
Dart doesn't have a built in functionality for serialization. There are several packages with different strategies available at pub.dartlang.org. Some use mirrors, which is harmful for client applications because it results in big or huge JS output size. The new reflectable packages replaces mirrors without the disadvantage but I don't know if serialization packages are already ported to use it instead. There are also packages that use code generation.
There is a question with an answer that lists available solutions. I'll look it up when I'm back.

Parse a custom XML with as3

i have a XML and i didn't found a way how to parse it (read the data inside)
<Saa:Header>
<Saa:Message>
<Saa:SenderReference> data
</Saa:SenderReference>
</Saa:Message>
<Saa:Message>
<Saa:SenderReference> data
</Saa:SenderReference>
</Saa:Message>
</Saa:Header>
i use the as3 language
Going with your example in your question (which assumes your data is a string), you could do something along these lines:
//your xml needs a namespace identifier to be valid
//I've added one on the header node.
//Likely if you're consuming this xml from some third party, the namespace declaration will be on root node.
var myXML = <Saa:Header xmlns:Saa="urn:swift:saa:xsd:saa.2.0" >
<Saa:Message>
<Saa:SenderReference> data
</Saa:SenderReference>
</Saa:Message>
<Saa:Message>
<Saa:SenderReference> data
</Saa:SenderReference>
</Saa:Message>
</Saa:Header>;
//create a reference to the `Saa` namespace that prefixes all your xml nodes
var saaNamespace:Namespace = new Namespace("urn:swift:saa:xsd:saa.2.0");
//tell AS3 to use that namespace by default
default xml namespace = saaNamespace;
//basically an array of all the SenderReference nodes in the entire xml
var xmlList:XMLList = myXML..SenderReference;
//This line will give the same results as the line above, but if there were SenderReference nodes somewhere else in the document that weren't under Message nodes, they would not be included (unlike above)
xmlList = myXML.Message.SenderReference;
//iterate through all the SenderReference nodes
for(var i:int=0;i<xmlList.length();i++){
trace("Node #" + (i+1) + ":",xmlList[i]);
}
There are lots of ways to get the data you want from XML in AS3, a good article is this one from senocular.
The XML class is the way.
[it] contains methods and properties for working with XML objects. The XML class (along with the XMLList, Namespace, and QName classes) implements the powerful XML-handling standards defined in ECMAScript for XML (E4X) specification (ECMA-357 edition 2).

Can I create an instance of a class from AS3 just knowing his name?

Can I create an instance of a class from AS3 just knowing it's name? I mean string representation, like FlagFrance
Create instances of classes dynamically by name. To do this following code can be used:
//cc() is called upon creationComplete
private var forCompiler:FlagFrance; //REQUIRED! (but otherwise not used)
private function cc():void
{
var obj:Object = createInstance("flash.display.Sprite");
}
public function createInstance(className:String):Object
{
var myClass:Class = getDefinitionByName(className) as Class;
var instance:Object = new myClass();
return instance;
}
The docs for getDefinitionByName say:
"Returns a reference to the class object of the class specified by the name parameter."
The above code we needed to specify the return value as a Class? This is because getDefinitionByName can also return a Function (e.g. flash.utils.getTimer - a package level function that isn't in any class). As the return type can be either a Function or a Class the Flex team specified the return type to be Object and you are expected to perform a cast as necessary.
The above code closely mimics the example given in the docs, but in one way it is a bad example because everything will work fine for flash.display.Sprite, but try to do the same thing with a custom class and you will probably end up with the following error:
ReferenceError: Error #1065: Variable [name of your class] is not defined.
The reason for the error is that you must have a reference to your class in your code - e.g. you need to create a variable and specify it's type like so:
private var forCompiler:SomeClass;
Without doing this your class will not be compiled in to the .swf at compile time. The compiler only includes classes which are actually used (and not just imported). It does so in order to optimise the size of the .swf. So the need to declare a variable should not really be considered an oversight or bug, although it does feel hackish to declare a variable that you don't directly use.
Yes, use getDefinitionByName:
import flash.utils.getDefinitionByName;
var FlagFranceClass:Class = getDefinitionByName("FlagFrance");
var o:* = new FlagFranceClass();

Write XML Fragment with LINQ and Prefixes

I have a document created in a constructor, and during execution I'm filling
it in with fragments generated from Custom Business Objects.
When I'm outputting the fragments, I need to include namespace fragments, but
I'd like to avoid adding the namespace url to each fragment, since it's defined in the root.
Any thoughts?
_doc = new XDocument(
new XDeclaration("1.0", "UTF-8", "yes"),
new XElement(aw + "kml",
new XAttribute(XNamespace.Xmlns + "gx", "http://www.google.com/kml/ext/2.2"),
new XAttribute("xmlns", "http://www.opengis.net/kml/2.2"),
new XElement(aw+"Document",
That's how the doc starts, so the namespaces are there.
How do I go about building an XElement to add using the gx prefix?
Use the same URI for an XNamespace:
XNamespace gx = "http://www.google.com/kml/ext/2.2";
XElement foo = new XElement(gx + "foo");
LINQ to XML will use the appropriate prefix automatically, as I understand it.