I have a string of kml coming in on a request object. I have used xjc to create the kml java objects. I am looking for an easy way to create the kml nested java objects from this string. I could parse the string and create each object in the tree by hand but wouldn't it be cool if there was a library or something that would create the java objects for me? Something like KmlType type = parseKML(mykmlStringFromTheRequest); Then type would be a Tree of kml objects. Thanks for the help all.
JAK http://labs.micromata.de/display/jak/Home will help
Related
We have a Java object with data that we want to use for template rendering - is there an efficient way of converting Java object to SoyData?
Currently we are using gson to convert it to tree and recursively populate SoyData object, but maybe there is something that wouldn't require gson conversion.
So in the end I added a constraint that the data can be only in a form of a map and I was able to use a library object com.google.template.soy.data.SoyMapData to do the work.
There is a .net version of this question answered here: XML input/output with Fitnesse
I need to do that with the Java version of Fitnesse and there doesn't seem to be anything similar. I have some tests that accept XML and return XML so I need both the input and the expected output to handle XML in the fields. I've tried various html escaping, but that doesn't seem to get the job done on the comparison side of things. The XML is arbitrary, so I can't parse it into domain objects and compare individual fields (well, I could, but there'd be tons of different object mappings).
Anyone find a way to deal with this issue in a generic way? A custom Java equivalent of the .net solution that exists somewhere?
Not sure if this is best solution. You may want to use some XML frameworks e.g. XStream which has very simple API to convert XML into JAVA objects. Using this, you may want to convert both XML files in JAVA and use JAVA comparison.
Simple steps would be:
Define simple Java object (POJO) e.g. MyXMLObj to map the XML attributes in Java attributes.
Create/generate equals method in the above POJO class MyXMLObj.
Convert your XMLs in Java as :
XStream xstream = new XStream();
XStream xstream = new XStream(new DomDriver());
//or
//XStream xstream = new XStream(new StaxDriver());
MyXMLObj myXMLObj1= (MyXMLObj)xstream.fromXML(xml1);
MyXMLObj myXMLObj2= (MyXMLObj)xstream.fromXML(xml2);
Simply compare the two objects as:
if(myXMLObj1.equals(myXMLObj2)){
System.out.println("Matching");
}else{
System.out.println("Not Matching");
}
I encountered something similar where xml response could change in future or doesn't confirm to a particular style all the time, or xml is not returned as an xml object rather a string is returned etc.
I wrote a generic fixture that would take an xml string and parse it to give us an xml doc object. I would also capture the xml element path and expected value from the wiki and I confirm if expected value is what is stored in the xml or not using xpath.
!|ValidateXmlValue |
|xmlString|xmlElem |getXMLvalue? |
|$respBody|/root/childnode1/ |${variable} |
This way users have the flexibility to query node value by simply giving the full path to that node and compare it against an input variable etc. Even if something changes, only test will need to change (you can store path to each xml element in a variable as well, as part of set-up step)
I'm developing a plugin that has node(computer) objects with attributes like:
String name
String description
String labels
Launcher computerLauncher
...
I can convert the node(computer) object to an XML-formated String like:
String xml = jenkins.instance.toXML(node);
Which gives me a string:
<name>Computer1</name>
<description>This is a description</description>
<labels>label1 label2</labels>
<launcher>windows.object.launcher.12da1</launcher>
Then I can go the other way back:
Node node = jenkins.instance.fromXML(xml);
I have no methods for changing attributes in a Node so I want to convert it to XML, change som attributes and then make it a Node again.
I see two options
Manipulate the XML with some String methods to replace everything in between the <> tags.
Try to cast the XML string to something like a real Object and manipulate it that way.
Not sure what would be the best approach.
Why invent something new when there already is support for all that using Java's DOM (Document Object Model) API?
Use a DocumentBuilderFactory to get a DocumentBuilder and create a Document instance. With this you can create the 'Node' objects (please note that the example you posted is actually not valid XML, it's missing a root node) in your toXML method, serializing the Document to a String could be done by using a Transformer.
With the DOM API you can also modify the attributes of your existing elements.
Parsing the Document instance from an XML string is realized again with the help of the DocumentBuilder, using DocumentBuilder#parse.
If your DOM operations are not too complex this should be a nice, quick way to accomplish your goal.
It makes sense to me to use a DOM-like approach. But don't use DOM itself: there are much better alternatives like JDOM and XOM that have much friendlier APIs.
recenty I had to manipulate large XML files (my software had to create some XML files dynamically and get input data from some other XML files). To do this I've used JAXB, which is a very neat API that marshalls XML files into Java objects and Java objects into XML files automatically.
However to do this I had to create a XSD file to specify the XMLs that I would need to read and write from.
Therefore JAXB requires more work to set up than DOM, so if your needs are simple I suggest that you use DOM, however if your needs are more complex, then I would suggest JAXB.
Hello Im trying to pass an array of objects from javascript to java , but how can this be done..??
I've found in some posts that they do this using a hidden input. Is this the only way?
I'm a bit confused. Please tell me what do I need to do to pass my array to the server? and which javascript files and jars do I need to add?
Thanks in advance.
You seem to want a completely baked-in solution. Not sure I can provide that, but here's what I'd do.
Indeed use a hidden input field in a form, where the value of the field is a valid JSON string. Send the form to your server, and in your servlet use a JSON Java library to parse the JSON string.
Here json-lib, gson or Jackson would do. In your case, I'd say json-lib would seem the easiest to use.
To generate the JSON string on the client-side, either use a framework or custom solution. For instance, jQuery has a serialize() function to serialize a form's fields to a JSON object directly, which you can then convert to string. Other frameworks provide similar functions.
To learn more about JSON, be sure to read the JSON Wikipedia entry and to visit the official JSON page (which also gives you a Java implementation of the JSON data-interchange format, though maybe not the most efficient one for processing a lot of data). To make sure your generated JSON is valid, you can use JSONLint.
If the objects are simple enough, you can encode your array as a JSON string. Java has libraries to encode and decode JSON.
I want to read data from the database, convert it to docs (JSON) using Java.
Thanks
GSON is a Java library from Google to convert Java objects to JSON. You can simply pass a Java object to the library function and it will return a JSON string.
Download: http://code.google.com/p/google-gson/
Example: http://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/
I have used the library from http://www.json.org, but the whole thing seems to be tedious to me. GSON is simple to use IMHO.
I would use JSONObject class:
http://www.json.org/javadoc/org/json/JSONObject.html
or you can build the string.