I have an enum which stores numerous xml strings like this
enum1("<?xml version='1.0'?><!DOCTYPE FOO><FOO><HEADER><MESSAGE-ID>1</MESSAGE-ID><MESSAGE-TYPE>RQ</MESSAGE-TYPE></HEADER>[...someXMLstructure...]</FOO>")
enum2("<?xml version='1.0'?><!DOCTYPE FOO><FOO><HEADER><MESSAGE-ID>1</MESSAGE-ID><MESSAGE-TYPE>Q</MESSAGE-TYPE></HEADER>[...someOTHERXMLstructure...]</FOO>")
the string is accessed via myEnum.getTag()
My application takes user input for some of the nodes represented in this string stored as a Map<String, String> with the key of the map being the element of the XML doc, and the value being the value.
Essentially i want to copy the XML from the enum and replace any corresponding elements found in the map, then send this message as a String over via some third party, unrelated interface. For example the user could pass in MESSAGE-ID=3 and USER-ID=360 so we would find the MESSAGE-ID element of the string and replace that, then find USER-ID in some unknown generic location and replace that. If USER-ID isn't found in the XML, then it is simply discarded.
EDIT:
If there is a better way to do this i.e. convert the XML into some data structure and modify this then convert back to string, I'd be happy to hear it, however I couldn't find much on doing this generically in my searches.
Thanks for your help
Related
I know that this topic has been talked about, and the use of a LinkedHashMap is a 'hacky' way to maneuver this, but if I'm given thousands of JSON strings as input, and eventually want to output them back in their original form, is there anyway to preserve the order without manually constructing LinkedHashMaps.
For example a string like this
{"key":1,"surname":"Reed","given":"Ryan","address":{"state":"CA","postal":"90210"},"gender":"M"}
Right now if I parse the object like so:
JSONObject jsonObject = (JSONObject) parser.parse(str);
System.out.println(jsonObject);
My output will look like this:
{"surname":"Reed","gender":M,"address":{"postalCode":"90210","state":"CA"},"key":1,"given":"Ryan"}
Is there anyway I can get the output to match exactly like the given input?
In Json property structure, order does not matter. but if you have specific order in your mind you can use Jackson to order them in you desirable way, both in your server and client apps.
https://www.baeldung.com/jackson
http://www.davismol.net/2016/10/24/jackson-json-using-jsonpropertyorder-annotation-to-define-properties-serialization-order/
I think it is impossible by default.
You can refer to this RFC https://www.ietf.org/rfc/rfc4627.txt
An array is an ordered sequence of zero or more values.
If you want to hack it you can override the data structure and use the data structure which preserves the order.
I have the following problems: I've got 20 files, that I need to fill based on user input. I have the list of strings, which is the list fields name used in all of the 20 files. Now, what I would like to do, is to run each of those strings against each form, so that if field exist, I can fill it in, based on expression I provide.
So for example, I would have something like that (pseudo code):
for all files
for all strings in sting list
if field with name string exists, use SOME METHOD on given object
Now, as of know, I store the list of the strings in the database.
My question is: how can I assign a method (like User.getFirstName()), to each of the string in array, so that described pesudo code would run?
Thanks
The simpliest way to assign Value to Key is Map.
Map<String, Function<T,R>> mapping;
I've got an API endpoint that is defined as:
GET https://api-server.com/something/{id_or_ids}
where ids can be a single object id or a comma separated list of ids.
e.g.
https://api-server.com/something/abcd1234
https://api-server.com/something/abcd1234,abcd4567,gdht64332
if a single id is given (and a matching object is found) I get back a json object:
{ "blah" : "blah" }
If multiple ids are given, I get the response in a json array:
[{"blah1":"bleh"}, {"blah2":"meh"}, {"blah3":"blah"}]
I'm currently thinking that I should implement this as two methods (can it be done in one?):
one that takes a single id and returns a single object:
#GET("/something/{id}")
void getObject (#Path("id") String objectId, Callback<MyObject> callback)
and
one that takes multiple ids and returns an array of objects.
#GET("/something/{ids}")
void getObject (Callback<MyObject[]> callback,#Path("ids") String ... objectIds)
Is there a way to feed the 2nd method varargs and concatenate them in the id field?
Thanks
Retrofit can't know how you want to join the strings in the path. While commas seem obvious, there's no reason why someone might want pipes (|) or colons (:) or whatever.
Because of this fact, we don't do anything and rely on you to choose.
There's two solutions to this:
Use String as the argument type and join at the call site. For example:
foo.getObject(Joiner.on(',').join(things));
Use a custom object whose toString() method deals with returning the correct format for one or many objects.
I'm trying to get a value from the databae.
My Database query:
String GroupID1="select idCompanies from companies where Company_Name='ACME';";
here I'm calling to a javabeans which give back an ArrayLIst with one element
ArrayList<?> IdGroup1=oper.getList(GroupID1);
then, I print the result:
System.out.println(IdGroup1);
The query works fine, however I'm getting as a result:
[javabeans.ListOneElement#609f6e68]
Instead of the real value. How can I convert the java object to the real value?
you are printing the ArrayList object IdGroup1,You need to iterate to get the alues
This code will retrieve the first (and only) item from the list:
System.out.println(IdGroup1.get(0).toString());
Adding the following will prevent a nullPointerException:
if (!IdGroup1.isEmpty())
System.out.println(IdGroup1.get(0).toString());
-Added .toString() to get the value of the object
Consider what type of Object oper.getList(GroupID1) will return.
You need to accommodate for whatever object that is and then convert it to String.
You need to:
Unpackage your list (that is a list contains, and is expected by java to possibly contain multiple objects, so it doesn't automatically 'unpack' it for you if you have a list of 1 object)
Extract your string. Here java might cleverly convert a number (int, float, etc. ) to a string for you.
For part two, look at what you expect the object to be by finding the JavaDocs for whatever package is handling your database queries. Then see how to extract your string.
It might be as simple as System.out.println(IdGroup1.get(0).toString());
Where get(0) unpackages the object from the list, and toString() extracts the string.
If you still get back javabeans.ListOneElement#41ccdc4d try other methods to extract your string....toValue() perhaps? It depends on the packages you're using.
How can I convert a List to a Json String?
I have managed to do it the other way round, but not this way.
I also don't know how I can specify the names of the keys then..
You can use the Gson Library.
List<String> list;
String json = new Gson().toJson(list);
Edited:
Just to have the complete answer here: The problem is that you are converting the json String into a List<String>. This way you are losing the relation key-value. The correct should be convert the json string into a HashMap<>.
It seems like your real problem is that when you originally turned the JSON string into a List, you threw away the keys. And that is not surprising, a List is not a natural representation of a JSON object (i.e. a thing with key - value pairs). You probably should have represented it as a Map.
But anyway, if you threw away the keys you've go a problem. You need to either you change your data structure to not throw the keys away, or reconstruct the JSON by inferring what the keys should be based on (for instance) their position in the list. (The latter could be a bit dodgy because the order of the name/value pairs in the original JSON should not signify anything ... and could be "random" or "implementation dependent".