Parse a custom XML with as3 - actionscript-3

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).

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.

Is there a list of defined C# functions for userCSharp in XSLT?

I'm trying to debug a BizTalk map that has some custom XSLT in it that makes use of C#. I've found:
userCSharp:MathSubtract
userCSharp:MathAdd
userCSharp:StringSize
userCSharp:StringSubstring
and a few others but I'm finding it difficult to find some resources online defining all of the available predefined c# functions and their documentation.
The reason I ask is because it has a I have a "userCSharp:StringFind" which blows up saying StringFind() is an unknown XSLT function.
The xslt functions MathSubtract, MathAdd etc correspond to the predefined Functoids that your map uses (in the xmlns 'userCSharp').
Most of the functoids are just inline XSLT C# functions - BizTalk adds the C# script for the functoid at the bottom of the xslt when the map gets compiled. (I think some of the simple functoids can use xslt primitives as well). Your own script functoids will also be added to this block.
You can see what BizTalk is doing by compiling your assembly containing the maps, and then using the "Show all Files" command to look at the corresponding .btm.cs file to see what has been added.
BizBert site gives quite a good reference on the implementation of each of the functoids.
(The double "" escaping is because the XSLT is kept in a string constant)
private const string _strMap = #"<?xml version=""1.0"" encoding=""UTF-16""?>
<xsl:stylesheet xmlns:xsl=""http://www.w3.org/1999/XSL/Transform"" xmlns:msxsl=""urn:schemas-microsoft-com:xslt""
...
xmlns:userCSharp=""http://schemas.microsoft.com/BizTalk/2003/userCSharp"">
and then a script CDATA block at the bottom
<msxsl:script language=""C#"" implements-prefix=""userCSharp""><![CDATA[
public bool IsNumeric(string val)
{
if (val == null)
{
return false;
}
double d = 0;
return Double.TryParse(val, System.Globalization.NumberStyles.AllowThousands | System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out d);
}
public string MathAdd(string param0, string param1)
{
System.Collections.ArrayList listValues = new System.Collections.ArrayList();
... etc

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.

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.