storing data types other than string in XML Nodes using libxml2 - libxml2

Seemingly, libxml2 has only 4 datatypes namely xmlChar, xmlDoc, xmlNodePtr and xml-
Node. I want to keep integer and double in the nodes of XML, Do I have to cast them to string each time or Is there any other way to do it without any casting?

You must cast them to string. XML has no concept of "integet" or "float", it's all text. There are technologies that can layer these concepts on top (like XSD), but not XML itself.
Similarly, when you parse XML and get the content of nodes, you can only get strings that you must convert yourself.

Related

Parse JSON inside JSON attribute using JSONPath

I have a JSON list where one of the attributes of each element happens to be a JSON itself. It comes from a poor design upfront, but here it is.
I want to query the distinct attributes inside the JSON string contained in the elements.
Here is an example, just one item. I hand-wrote the code, but believe me that is valid JSON in production by the way it's generated
[{
"extraData": "{\"foo\":\"bar\"}"
}]
I would like to query for something like $.*.extraData.foo, but obviously such syntax does not work.
I am using IntelliJ IDEA Jsonpath evaluator.
The syntax should be something like parse($.*.extraData).*.foo
This article suggests me that no such operator is available to parse a JSON inside a JSON
It has to be JSONPath only for data analysis purposes. In Java, I use Jackson to parse the extraData object as a JsonNode, but my goal is to explore the large data set, and perhaps obtain some distinct values I want to use for enumeration purposes.
To JSON Path, the embedded JSON is just a string. The best you could do with a single operation is use a RegEx in the expression selector, but getting RegEx to identify JSON is really tricky, and that's if your implementation even supports RegEx.
I think the best option would be to use two paths:
The first path gets the embedded JSON. $.*.extraData
Parse that value
The second part gets the data you need. $.foo
This requires some extra code, but I think it's your only realistic option.

QMimeData encoding types

In my app I'm doing internal drag and drops with a QTreeView. Using the tutorial I can happily drag and drop a single leaf by encoding it into a string list using the mime type "application/vnd.text.list".
I then wanted to drag and drop a tree node that had some children and thought the best route to doing this would be to encode the pointer to the node and iterate through all the children in the dropMimeData method.
I declared a mime type in the mimeTypes() method:
QStringList toResultModel::mimeTypes() const {
QStringList types;
types << "text/plain";
types << "application/vnd.mypointerlist.list";
return types;
}
And tried to pass the same string list across, but the application crashes in the dropMimeData() method.
It seems the mime type "application/vnd.text.list" has some hidden meaning which I am unable to find.
I have found this source code: http://fossies.org/linux/tora/src/toresultmodel.cpp where the author sets up a custom coding type "application/vnd.tomodel.list" and also uses "application/vnd.int.list".
What are the rules in using encoding types?
Where are the built-in types strings defined?
Which type should I use for passing a pointer to a tree node?
Four years later...
From the information you give, if your method crashes, it's not related to Drag and Drop in particular, there is some error you need to find in the code. that said, let me clarify D&D in Qt, and answer your question about MIME types. While you have indeed solved the problem you had four years ago, this may be useful for other users today.
You may define your own type for the purpose of your application or reuse an existing one. How to choose?
Can you use an existing MIME type, like text/plain?
Think about your application being the target of a D&D operation initiated from another application. Can you accept an existing MIME type and retrieve your data from it?
Think about another application being the target of a D&D operation initiated from within your application. Could this application handle an existing MIME type?
If this answer is no to any of the questions, then you might have to use your own specific MIME type.
The format name itself is not important
The constraint is that it must be unique, so that you can't receive incorrectly formatted MIME data from another application, and other applications can identify the MIME type as one they cannot handle, and ignore it.
The exact MIME type name doesn't matter as you'll provide the encoder and the decoder into your data model (e.g. see this introduction to view/model for Qt), as well as other information about the MIME type(s) used.
In QAbstractItemModel::mimeTypes, list only the MIME types you are able to deal with. If you don't plan to accept or send MIME data from/to other applications, there is no need to allow more than your specific MIME type.
When your application is the source of a D&D operation, encode (serialize) the MIME data in QAbstractItemModel::mimeData(indexes). The result of the serialization must be a byte array, even when there are multiple indexes to be dragged. The internal format is yours. Include any information required to decode (de-serialize) MIME data. Note that you must provide encoded data in each of the MIME type you've listed in QAbstractItemModel::mimeTypes (see previous point).
When your D&D data are dragged over your application UI, QAbstractItemModel::canDropMimeData(self, mime_data, action, row, column, parent) is called to determine if this location is valid for a drop. You may determine here whether the drop should be allowed at this location. In particular, you may test the content of the MIME data provided, and use mime_data.hasFormat(mime_type) to check if the format you expect is found in the data about to be drooped. Returning false will prevent a drop at this location and a "not allowed here" indication will be provided to the user (this won't cancel the D&D operation itself, the user can continue to move the mouse elsewhere).
When the data is actually dropped, QAbstractItemModel::dropMimeData(mime_data, action, row, column, parent) is called. Get the MIME data format(s) used using QMimeData::hasFormat(mime_type). If you don't find the MIME type you expect, ignore the drop operation as you cannot decode the data provided (the D&D was initiated from another application). This shouldn't happen as prior to drop data, the application has called QAbstractItemModel::canDropMimeData as seen in the previous point. If everything is ok decode the MIME data, and update your model with the data received.
On the other hand, your tree leaf data may fit as path+name encoded in text/plain MIME data, so maybe you can just use this type too. However as other applications can generate text/plain data that don't contain a tree leaf description, you need, in this case, to have a mean to identify irrelevant data and ignore them. It's obvious such approach will need more code for verification of the validity of the drop action than when using a specific MIME type. However this allows to interact with other applications, and is indeed relevant to drag from well know applications like Excel (e.g. cell content) or Firefox (e.g. rich text or image), else we couldn't re-use information from these applications using D&D.
Do you need to use vnd prefix?
vnd in the MIME type means "vendor specific". This prefix is used to distinguish vendors created MIME types from those created by IANA authority. From RFC 6838:
Vendor-tree registrations will be distinguished by the leading facet
"vnd.". That may be followed, at the discretion of the registrant, by
either a media subtype name from a well-known producer (e.g.,
"vnd.mudpie") or by an IANA-approved designation of the producer's
name that is followed by a media type or product designation (e.g.,
vnd.bigcompany.funnypictures).
So in your Drag&Drop tutorial the application/vnd.text.list is a specific one supposedly created by some vendor for their own purpose. Same for application/vnd.mypointerlist.list
In contrast, text/plain is a standard MIME type defined by IANA in RFC 2046. This defines a human readable text:
Plain text does not provide for or allow formatting commands, font
attribute specifications, processing instructions, interpretation
directives, or content markup. Plain text is seen simply as a
linear sequence of characters, possibly interrupted by line breaks
or page breaks. Plain text may allow the stacking of several
characters in the same position in the text. Plain text in scripts
like Arabic and Hebrew may also include facilitites that allow the
arbitrary mixing of text segments with opposite writing directions.
For your type, you may want to use vnd followed by a subtype which is specific to your application, for consistency considerations. But as seen, the actual name is not important, as long as you know which one you use and you are not interacting with other applications in the D&D chain.

extracting array of objects from xml response to js

so I'm extracting variables from my xml response and trying to reformat the objects but I want to do that the most efficient way possible. so I want to load the xml array of same objects into a js array that I can cycle thru and output the new format. I found a reference to type="nodeset" when extracting the XPath but i could not find a reference to it on the documentation.
what is the best way to load the full xml objects into a js variable and cycle thru the objects and output the new format
Thanks for any help you can give me on this.
Best way to accomplish this is with the XMLToJSON policy, a JavaScript callout in which you can mediate your payload, and then transform back with JSONToXML if you need it.
For an XML array that doesn't need filtering, you can use XPATH with type="nodeset", just as you described. This allows you to pull a node and all child nodes in a particular XPATH. As I'm sure you noticed, you can't do this by just extracting as type="string". Just know that you will need to convert the extracted variable to string before you can use the XML nodes like you do every other string. You can then do JSON.parse to take the string and manipulate the object like an array. The string conversion is as simple as calling a JS callout with the following code (if someone else has a better way, I'm all ears):
var extractedNodeSet = context.getVariable("extractedNodeSet");
var extractedNodeSetString = String(extractedNodeSet);
For an XML array that needs filtering/manipulation, I recommend to use XSLT along with the trusted <xsl:for-each select=...> element. This will let you set conditions on the XML array nodes, manipulate tags/data, and extract the data, all in one step. The only concern is that this isn't a JS array, so if you absolutely must have a JS array, then you'll need to then do an XMLtoJSON and work with the data from there.

Is it possible to convert a result set from WebMatrix.Database.Query() to an XML string?

I'd like to consume the result set on the client side using a jQuery script, however I'm uncertain how to convert it to XML, other than building the XML DOM string manually, which I'd prefer to avoid. Is there an automatic way to convert it or obtain it as an XML string?
There are a number of APIs for generating XML, including Linq TO XML. However, I would serialize your server side data to JSON if you want to make it available to jQuery. The JSON helper is perfect for this.

Flex HTTPService Resultformat

What is the real difference between these resultformats for HTTPService in Flex :
text
object
xml
e4x
Especially, the last three seem pretty close to each other by their description.
from the manual of HTTPService:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/rpc/http/HTTPService.html#resultFormat
object: The value returned is XML and is parsed as a tree of ActionScript objects. This is the default.
array: The value returned is XML and is parsed as a tree of ActionScript objects however if the top level object is not an Array, a new Array is created and the result set as the first item. If makeObjectsBindable is true then the Array will be wrapped in an ArrayCollection.
xml: The value returned is XML and is returned as literal XML in an ActionScript XMLnode object.
flashvars: The value returned is text containing name=value pairs separated by ampersands, which is parsed into an ActionScript object.
text: The value returned is text, and is left raw.
e4x: The value returned is XML and is returned as literal XML in an ActionScript XML object, which can be accessed using ECMAScript for XML (E4X) expressions.
The classtype of the returned object differs.
text => String
object => A generic object that you can use like a hash
e4x => an object of type XML
xml => I forget... a String?
I recently had some issues with the "object" and "e4x" resultFormat.
I have a base WebService Class that I use for sending requests and receiving results. By default, all results come back as "object". However, sometimes Flex looks at the data, and converts it to an appropriate type. For instance, if you have an XML result that looks like the following, it will convert it to an Array Object (not sure why...but...):
<root>
<child>text</child>
<child>text text</child>
</root>
Now, an Array Object like this can easily be cast as XML, since, as a string it is also XML.
However, some XML documents are returned as an ObjectProxy, which cannot be cast as XML, when the resultFormat is "object".
I tried using "e4x", as it was suggested here, but then I ran into problems with namespaces not being preserved correctly.
I finally tried "xml", and I am getting the expected results. It's interesting that when you inspect the event result property using the Flex Debugger, it is said to be an Array, even when you specify a resultFormat of "xml". I guess this allows for easily casting to ArrayCollection...not sure....

Resources