How to add xml file data into ArangoDb? - java

<InputParameters>
<Textbox>
<Text>ABCD</Text>
<Text>EFGH</Text>
<Text>HIJK</Text>
</Textbox>
</InputParameters>
Suppose i have to add this xml file data into arangodb. How would one able to do so ?

Since version 2.7.1 the aragodb-java-driver supports writing (createDocumentRaw(...)) and reading (getDocumentRaw(...)) of
raw strings.
Example:
arangoDriver.createDocumentRaw("testCollection", "{\"test\":123}",
true, false);
With JsonML you can convert a XML string into a JSON string and store it into ArangoDB:
// writing
String xml = "<recipe name=\"bread\" prep_time=\"5 mins\"</recipe> ";
JSONObject jsonObject = JSONML.toJSONObject(string);
DocumentEntity<String> entity = arangoDriver.createDocumentRaw(
"testCollection", jsonObject.toString(), true, false);
String documentHandle = entity.getDocumentHandle();
// reading
String json = arangoDriver.getDocumentRaw(documentHandle, null, null);
JSONObject jsonObject2 = new JSONObject(str);
String xml2 = JSONML.toString(jsonObject2));
You can find more examples in the arangodb-java-driver git repository.

There are two proper solutions.
One is to put the whole XML into an attribute of a document. This will then in term probably be not good for doing AQL queries on the payload of the xml.
Another possible approach could be to use jsonml to translate your xml into structured json documents, and store them using their java library. I don't know how well this scales on complex XML like SOAP though.
You could then create AQL queries to work on that collection and FILTER for attributes of the source XML.

Related

Interpolate JSON values into a string

I am writing an application/class that will take in a template text file and a JSON value and return interpolated text back to the caller.
The format of the input template text file needs to be determined. For example: my name is ${fullName}
Example of the JSON:
{"fullName": "Elon Musk"}
Expected output:
"my name is Elon Musk"
I am looking for a widely used library/formats that can accomplish this.
What format should the template text file be?
What library would support the template text file format defined above and accept JSON values?
Its easy to build my own parser but there are many edge cases that needs to be taken care of and I do not want to reinvent the wheel.
For example, if we have a slightly complex JSON object with lists, nested values etc. then I will have to think about those as well and implement it.
I have always used org.json library. Found at http://www.json.org/.
It makes it really easy to go through JSON Objects.
For example if you want to make a new object:
JSONObject person = new JSONObject();
person.put("fullName", "Elon Musk");
person.put("phoneNumber", 3811111111);
The JSON Object would look like:
{
"fullName": "Elon Musk",
"phoneNumber": 3811111111
}
It's similar to retrieving from the Object
String name = person.getString("fullName");
You can read out the file with BufferedReader and parse it as you wish.
Hopefully I helped out. :)
This is how we do it.
Map inputMap = ["fullName": "Elon Musk"]
String finalText = StrSubstitutor.replace("my name is \${fullName}", inputMap)
You can try this:
https://github.com/alibaba/fastjson
Fastjson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object. Fastjson can work with arbitrary Java objects including pre-existing objects that you do not have source-code of.

Android - Accessing JSON children from a URL

I'm in the process of converting my website to an Android app and one of the pages' data currently is populated via JSON in my website. The way it works is that the URL generates a different JSON data with the same structure based on the passed ID. I already have the logic for passing the ID to the URL. Now I want to read the data through Java code and parse the JSON children and its values in it.
I have a URL that leads to the JSON file in textual form, but I'm not sure how to go about reading the data from it and accessing the child nodes based on the JSON key.
So I guess what I'm asking is what is the usual approach for this procedure? I see a lot of different examples, but none of which are applicable to my problem.
Anyone have any suggestions as to how I should approach this?
JSONObject = new JSONObject(yourjsonstring);
Now you have your Json Object...
If your Json start with array use this:
JSONArray = new JSONArray(yourjsonarray);
You can use existing libraries to parse JSON, gson or Moshi are two solutions.
The way you go about parsing the JSON is as followed
First you need to make pojo's with the same structure as the JSON file.
then you can parse it to java code via the fromJSON() method, this will make new objects and fill it with the data from the JSON.
gson example for clarification:
Gson gson = new Gson();
Response response = gson.fromJson(jsonLine, Response.class);
where jsonLine = your json file and the Response.Class the pojo in which you want to json to load.
Now you have the JSON values as Java classes in response.
If you're using Retrofit and OkHTTP to perform the network calls i suggest you use Moshi as it's also from Square and claimed to work faster and better than gson. (if you want to know why you can leave a comment).
I think what you're trying to do is this
on post execute method do the following
#Override
protected void onPostExecute(String result) {
String status = "";
String message = "";
String tag = "";
String mail = "";
try {
JSONObject jsonResult = new JSONObject(result);
status = jsonResult.optString("status");
message = jsonResult.optString("message");
tag = jsonResult.optString("tag");
mail = jsonResult.optString("mail");
} catch (JSONException e) {
e.printStackTrace();
}
of course your json array contains different keys
Just reolace them with yours

Giving incorrect values when converting JSON string to XML and XML back to JSON string

I am using org.json for converting the json string to xml and xml back to json. I am getting the expected response for json string which has only key value pairs, but incase of json string contains of jsonarray objects the return value is different from the input?
Below the code i have used,
import org.json.JSONObject;
import org.json.XML;
String jsonStr ="........";
System.out.println("The JSON Raw Text :"+ jsonStr);
JSONObject jObject = new JSONObject(jsonStr);
String xml = org.json.XML.toString(jObject);
JSONObject xmlJSONObj = XML.toJSONObject(xml);
String jsonConvertString = xmlJSONObj.toString();
System.out.println("JSON text converted : "+jsonConvertString);
What is the error or what is the best way to convert json string to xml and back to json ?
Looks like you are facing problem as explained in this blog.
In short: XML.toString() does NOT produce an XML document. When you use a JSONArray the produced xml string from this method has NO root element.
Therefore, if you are planning to write the contents to an XML file, I'll recommend you use a more stringent API such as org.w3c.dom.*. This will produce a VALID xml document that can be dumped to a file.
Or an easier way would be use something like "<root>" + org.json.XML.toString(jArray) + "</root>" so that you have a root element. This might be parsed pretty well (and as you expect) by XML.toJSONObject().

Simplest way to read from a JSON file in Java?

I'm relatively new to using JSON and all I really need to do read in a few key value pairs from a JSON file on the file system.
What I figured I would do is read in the file as a string and then parse it that way but it seems kind of redundant that way.
Here's what my file will be like:
{
"username" : "myname"
"domain" : "mydomain"
}
So essentially I need some help making an easy and efficient block of code to read in the key/value pairs. I've been trying to use GSON for the most part and haven't had much luck with examples I've found.
Thanks everyone
One other alternative is JSON.org, in which creating a JSON object from a JSON string requires only one line:
JSONObject jsonObject = new JSONObject(someJSONString);
When you need to access its value, use the functions that the JSONObject provides. For example,
String userName = jsonObject.getString("username");
String domainName = jsonObject.getString("mydomain");

Need to parse String in XML format to obtain Request Information

I have got a String request in the XML format , which i need to parse it to obtain the Request data from it .
The XML String would be conssiting of a lot of subtags within it , and data is appended in it in the form of CDATA as well as there are >&lt , ini t.
I want to use STAX approach for this .
Please sugesst if theer are any cons wtth this ??
i need this inside a Java Webservice
You can parse your XML file with native PHP function.
EX :
$path = '/my/file/xml.xml';
$file = file_get_contents($path);
$xml = new SimpleXMLElement($file);
And get nodes with :
$value1 = $xml->node[0]->value;
$value2 = $xml->node[1]->value;
There would not be any issues in using DOM parser based STAX API.

Categories

Resources