how to get value From XML object in action script - actionscript-3

I am new to flex development. I am trying to develop a web based application. somehow I feel comfortable. I have some queries related to xml documents.
In my webservice I perform some database operation and return some xml data in XmlDocument Object format.
I tried to parse it using below function
public function objectToXML1(obj:Object):XML {
var qName:QName = new QName("DATA");
var xmlDocument:XMLDocument = new XMLDocument();
var simpleXMLEncoder:SimpleXMLEncoder = new SimpleXMLEncoder(xmlDocument);
var xmlNode:XMLNode = simpleXMLEncoder.encodeValue(obj, qName, xmlDocument);
var xml:XML = new XML(xmlDocument.toString());
return xml;
}
but I don't know how to pull data from this XML
XML FORMAT
<REQUEST>
<STATUS>SUCCESS</STATUS>
<MESSAGE>BP Retrive </MESSAGE>
<DATA>
<BOM>
<BO>
<BusinessPartners>
<row>
<CardCode/>
<CardName/>
<Phone1/>
<Phone2/>
<Cellular/>
<EmailAddress/>
</row>
</BusinessPartners>
</BO>
</BOM>
</DATA>
</REQUEST>

If you have a predefined structure of the XML, you could use FlexXB to create an ActionScript Object of the XML. It is pretty easy, you create an ActionScript Object which looks exactly like the XML (that means it need to have the same properties) and you annotate these properties. So FlexXB will read the XML and create an ActionScript Object form it.
If you don't want to use the framework, you can do it by hand too. To read attributes, use xmlElement.#attributeName for getting the name of an object use e.g. xmlElement.name().localName. There are some functions, but since the autocompletion doesn't work for XML elements it is a little hard to find the right functions. Here is an article by Adobe which explains how to work with XML. Basicall you can just use the node names as property names. In your case you could write xmlElement.Phone1 and so on.
Hope it helps :)

Related

Converting json object with variable attributes into xml array

The Api response is {"Content":{"634331":["Product could not be found"],"634332":["Product could not be found"],"etc…
and I am having trouble to catch the values after content into xml :
<Content>
<__634331>Product could not be found</__634331>
<__634332>Product could not be found</__634332>
<__123104398>Product could not be found</__123104398>
The values are being interpreted as field names.
Is there a way to convert json object into xml array looking like:
<Content>
<res>
<key>634331</key>
<value>Product could not be found</value>
</res>
<res>
<key>634332</key>
<value>Product could not be found</value>
</res>
It's quite common in JSON for keys in a map (or "object") to represent data values rather than property names. Unfortunately this doesn't map at all well to XML, where the equivalent would usually be a structure like
<data key="__634331" value="Product could not be found"/>
No automatic converter is going to be able to recognise that this kind of conversion is appropriate.
My recommendation would be to do a custom conversion using XSLT 3.0 template rules. I would need to see more detail of your JSON and required XML to advise in more detail.

DataPower: Map Json response to xml using XSLTs

My SOR application is returning result set, I'm performing below binary-encode and decode to retrieve json
<xsl:variable name="response_json">
<xsl:copy-of select="dp:decode(dp:binary-encode($resp/result/binary/node()), 'base-64')"/>
</xsl:varibale>
response_json - {"code":"00", "description":"Success"}.
Now how do we parse through above response_json and assign "code"/"description" values to xsl:variables and context variables using XSLTs
You have two options, IMO one good and one bad...
Let's start with the bad one which you are already on as you are using XSLT for JSON data.
Even worse option is of course to use the JSON as text only and check for the value of code and description using XSLT String functions, otherwise you need to transform the JSON data into JSONx that can be handled in XSLT by using a convert of Query Params to XML.
To do this you need to pass your JSON to a new Action (Convert QP) and then into a new Transform.
You can then use DP's JSONx to handle any "JSON" data operations you'd like as XML in the XSLT stylesheet and when done call store://jsonx2json.xsl in a new Transform to transform it into "real" JSON.
I assume you are using a dp:url-open() as you get your response in a variable but if your service has Request/Resoonse as JSON the context VAR __JSONASJSONX will automatically hold a JSONx object for you that you can use as the INPUT to any XSLT Transform action.
That being said, let's move over to the good solution; use GWS!
GWS is added to DataPower to handle JSON (and anything other than XML) and you can call a GWS from your XSLT using dp:gatewayscript() as well. See this sample: https://github.com/ibm-datapower/datapower-samples/blob/master/gatewayscript/example-ext-func-dp-gwscript.xsl
If your Transform is not doing anything XML specific I'd rewrite it into GWS!

Convert XML file to JSON

In Angular 8 I have to send an XML contents to the server.
I prefer converting it to JSON and then it send with:
this.http.post (${BASE_URL},body).subscribe
Is it a wise step ?
Generally: How can I read a file in client side and copy its contents into body ?
Thank you in advance
There are many tools and libraries for converting XML to JSON, and they all do it differently. All of them have different strengths and weaknesses; they differ in what kinds of XML they handle well. If the JSON that you want to generate has already been defined by a third party, then you're unlikely to find a tool that generates precisely the desired format. In such cases you're better off writing the conversion rules yourself in XSLT.
You have to install "xml2js" dependency.I hope it will helps you.
import { parseString } from "xml2js";
let xml = `<book><title>Some title</title>
<description>some description </description>
<author>
<id>1</id>
<name>some author name</name>
</author>
<review>nice book</review>
<review>this book sucks</review>
<review>amazing work</review></book>
`;
parseString(xml, function(err, result) {
console.dir(result);
});
}
jsonxml should what you're looking for.
function xml2json(xml, // element or document DOM node
tab) // tab or indent string for pretty output formatting
// omit or use empty string "" to supress.
// returns JSON string

Create a List-type view dynamically from a Json object in MVC3

I have a controller that access a WCF service which returns a Json object (collection). All rows are of same type, but at different calls the row stricture is different (the return object comes from a user-built sql query, executed with executeReader and serialized as Json
So I don't know the row structure upfront.
What I need is an easy way to pass this Json string to something which will generate a view of type list on the fly for it. Doesn't matter formatting, etc, just should be output easily as a table.
Does anyone knows how can I accomplish this?
Another option might be to have something that generate the view on the fly for a IEnumerable of anonymous objects (since using this I could convert the json to a list of anonymous)
EDIT
I found something that does pretty much what I need, except it display metadata about passed object.
It is preetyPrint.js, and I integrated it in my page as below:
In my controller I set the result json object to ViewBag.Result, and in the view I used this code:
<script src="#Url.Content("~/Scripts/prettyprint.js")" type="text/javascript"> </script>
<div id="resultGrid"></div>
<script>
var resultObject = #Html.Raw(ViewBag.Result);
var ppTable = prettyPrint(resultObject);
document.getElementById('resultGrid').appendChild(ppTable);
</script>
Does anyone knows such script that actually "dump" the data instead of metadata?
Thanks.
You should create a class to deserialize to if you know the properties of the row. Then use the JavaScriptSerializer class to deserialize to a list of your new class you created. Then you can take a look at the WebGrid class to output the HTML, or just manually iterate over the property metadata in your view.
Creating a custom class will provide you the ability to use metadata to control formatting or other display attributes of the output.
If you cannot create a custom class, you can always use Json.NET or the JavaScriptSerializer to deserialize to a list of dictionary objects or ExpandoObject / Dynamic's or something. Then you would have to manually write something to iterate the keys I think. The ModelMetadataProvider in MVC may be able to handle these allowing you to just iterate the properties in your view code.

DataContractJsonSerializer not deserializing html entities

I am receiving data from a web service, and some of the strings have html entities in them, for example:
{"prop": "htmlentity - é"}
The é is not being parsed to é.
My question is twofold:
Is this even supposed to happen?
I looked through the JSON spec the best I could, but couldn't find any reference to html entities.
What is the right way to do this with a DataContractJsonSerializer?, if there is a right way?
You can call HttpUtility.HtmlDecode on the strings that contain HTML entities.
This is not the job of DataContractJsonSerializer, as the JSON spec only requires quotation mark, reverse solidus, and the control characters to be escaped.
This isn't a JSON serialization issue, this will be due to the data being sent over the web.
Serialization does not automatically encode HTML entities.
See:
var orig = new MyObj {prop = "htmlentity - é"};
var ser = new DataContractJsonSerializer(typeof(MyObj));
var ms = new MemoryStream();
ser.WriteObject(ms, orig);
var serialized = Encoding.UTF8.GetString(ms.GetBuffer(), 0, (int)ms.Length);
MessageBox.Show(serialized); // {"prop":"htmlentity - é"}
If you have control of the web service then you can verify this on the server side. If not, check with the provider of the web service.