parsing xml from web service call in java - c#

I apologise if this is a duplicate thread...
I have a c# web service where I return an xml object that I need to parse into a Hashmap in my android application, the returned object from the service is .Net type Dictionary
most of the other calls are returning json which I can parse into a JsonObject with the response string but this particular call needs to return xml for other apps
could anyone please advise whats the simplest way to go out parsing xml returned from a web service into java objects or at least be able to maybe create an xml document from the response and pull values out of that?
many thanks in advance

There are mainly two ways and based on your requirement you need to choose one.
1) SAX parser: Mainly useful if you want to read all elements in XML document and create java objects. Here is a referene. SAXParsing. This is less memory consuming approach because entire xml file won't be cached while processing.
2) DOM parser: This is useful when you want to read specific parts of the xml (you can use this to parse entire file, but I use more when I need to play with part of the xml). This is more memory consuming approach, because entire xml file will be cached while processing. Here is an example. Dom parsing
You may use Sun inbuilt API for these parsing mechanisms (or) there are other third party APIs like Apache etc.,

Try JAXB or SimpleXML

Related

Sending XML over WCF?

I am developing a WCF SOA(ish) architecture application that I needs to receive and return XML.
I have scoured the net for best practices. The only thing I know is sending raw xml as a string is going to cause problems.
I was therefore looking at an XmlTextReader type object that could perhaps be more elegantly marshalled from a to b and then back.
I get an error when i try and call my service that takes a XmlTextReader as a type and frankly it is confusing the hell out of me.
Bottom line it needs to accept and recieve large amounts of xml and I can't/don't want to use my own definded types.
Any help?
One answer without deeper analysis : you could maybe convert XML to Base64 encoded string and send it like that.
You could use XElement - personally have only used this on relatively small chunks of Xml - YMMV.

Should I parse XML and JSON separately or convert one to the other first?

I am collecting data from various web services, some return XML, some JSON. I've looked into JSON.Net as a parser for JSON, and it looks like it works well to extract the items I need, specifically using Linq.
Question is, should I go ahead and use JSON.Net's conversion methods, and just turn all the XML I receive into JSON before processing, meaning I only need to write parsing methods for JSON.
Can anyone think of an advantage of having separate XML parsing code (quering it with Linq probably). Are there disadvantages to using JSON if the data structures start to get complex?
The only real difference I can see for using XML would be validating one's parse to a schema, although I have no idea as to JSON.NET's support for JSON Schema (currently an IETF draft).
Both formats can represent object graphs effectively so I don't think data structure complexity is a problem.
The only disadvantage of JSON that I have found is that XML is navigable. You can walk through the structure of an XML serialized object graph without actually creating the corresponding object graph. With JSON, unless I have missed something, you will need to create the object graph to navigate the structure.
I am not 100% sure but it would seem that Linq would acutally require the graph to be created in memory anyway. If that is true then the disadvantage disappears as a factor.

Sending XML format messages through TCP in C#

I have a C# TCP chat program. Currently, I have formatted the messages sent using strings i.e, a "login" message starts with a "3" then followed by a "U:" then the username etc.
I think this method is very crude in a way that it's not really readable and not standardized. In early research, I have read that I can format my messages using XML but I dont know where to start exactly. Do I just make a string builder and append it tags like .append("<Login>"+message)?
The most common approach for dealing with a problem like this is to use serialization. Serialization is the process of converting an in-memory object into a format that can be easily streamed "over the wire," and de-serialization is the reverse process of converting the serialized format back into an object. .NET has good support for XML and binary serialization out-of-the-box, but there are other ways to implement this. Here's a link to get you started:
http://msdn.microsoft.com/en-us/library/7ay27kt9(VS.71).aspx
You can send whatever you like over the connection - as long as it's just for your program it doesn't really matter what you choose. Xml might give you some benefits as it lends itself to some kind of more structured messages and there are many classes and tools and knowledge around on the net regarding XML. JSon format might be another option - it will make it potentially easier creating a JavaScript client for it in case you want to go web based.
Unless there is a reqirement that 3rd parties be able to read these messages then I would probably favour binary serialisation, as it has a more compact format.
That said, I'd probably just use WCF rather than uisng TCP directly.
If you want to know more about XML serialisation then the most commonly used methods are:
Generating a stronly typed C# object decorated with attributes to control XML serialisation using XSD.exe, and then using XmlSerializer to serialise and deserialise XML. (recommended)
Using the XmlDocument class
You can write our XML yourself as a string, but its better to use the serialisation methods made available in the .Net framework as it makes things considerably easier and reduces the chance that you will make a mistake and inadvertantly start working with invalid xml.

LINQ to JSON in .NET

Is there such a thing as a JSON file? That is, *.json?
Can JSON be used in C# code without any JavaScript stuff, sort of as a replacement for XML?
And is there any official LINQ to JSON stuff around for C#?
I did find one website for my last question, but it took me to a page to download JSON.NET, and that page doesn't seem to mention anything about LINQ.
Yes, there is such a thing as a *.json file. The MIME type is application/json (source). JSON is a text-based format though, so you could hypothetically store JSON-formatted data in a text file with whatever extension you choose.
JSON can absolutely be used independently of JavaScript. In some cases, it's probably better suited to representing your data than XML. JSON.org has a great comparison page between JSON and XML.
JSON.org lists several JSON libraries for C# (for example, JSON.NET which you have already discovered), and most (if not all) of the collections that these libraries use should support LINQ. JSON.NET definitely does offer support for it. See here or here.
Everyone tends to stick with the JavaScriptSerializer (from the System.Web.Extensions library) when working with JSON in .NET. The handy part about this is the ability to create a custom JavaScriptConverter that will take custom objects and serialize them the way you chose. Likewise, you can make a deserialization method to receive in custom JSON formatting.
Though this of course depends on your application. Given that it's a Windows Forms application, is there any particular reason you'd chose JSON over storing the information natively or just use the XML format? If your application communicates with webpages, the JavaScriptSerializer is probably the best bet, though if you're using it to store/retrieve settings I'd use XML. And, if it's necessary to synchronize your application with a web-based one, just serialize to JSON when the time is ready.
You can deserialize your JSON file into C# objects. After that, you can query with LINQ on these objects.

How to use c# dataset in java?

I am using webservice. I am getting data to Java in the form of C# dataset how to retrieve values from dataset in Java.
I've handled this by using XPath with Xalan to query the DOM document that Axis hands to me. The schema of the serialized DataSet is pretty clear and easy to query. I just had to be sure to set up Xalan with the proper namespaces to perform the queries.
I ran into one problem handing the data this way. The serialized DataSet doesn't contain an element for data that is null. If you know what data you are expecting then this isn't a problem. However, if you don't know exactly what data to expect, you can extract the fields that are in the DataSet from the XML document.
Does this mean you are using the web service to transport the actual DataSet object?
Just convert the C# data set to xml or a formatted string on the C# side before it gets sent to the web service. Then you can easily re-parse the web service output into whatever java object you want. (I would just go with a formatted string, I havent used any Java XML libraries that come CLOSE to being as good as C#'s XDoc).
A dataset is serialized as XML. You will have to create an XML object on the
Java side and consume it by binding the XML to items in your application.
For simple report output, XSLT would help to simply the work.
or
Try to stay away from anything that's .NET-specific. Don't return
a DataSet, for instance. Stick to using simple types, arrays and structs of
simple types, and you'll be fine.
Happy coding
It depends on the format of the webservice. If it's a SOAP service look to a prior SO answer: consuming-a-web-service-in-java
If it's restful. I'd suggest using JSON if possible because the JSON serializer on the .Net side generally gives you a more agnostic serialization. Then, Java-side httpclient gives you a robust way to connect, and Json-lib lets you parse, rock, and roll.

Categories