Validating XML against JSON - java

I am looking to automate validating JSON instances against XML instances. What would be a good way to achieve this? I was thinking of mapping fields but then again this would just solve the problem say for one instance and the script would need to be updated for each instance.
Are there any libraries in Java that can aid in this?

No, use XML Schema (XSD) for validating XML; use JSON Schema for validating JSON.
If you're looking to parse from XML to JSON (unmarshal) or serialize JSON to XML (marshal), see JSONIX.
You may be using validate in an unconventional sense which would be satisfied by parsing from XML to JSON using JSONIX for comparison with other JSON.

Related

How to convert JsonForm to Json?

I would like to extract only the data present in a Json to JsonForm using Java.
Which framework can I use to this operation?
I've found the answer. Don't let the ugly aspect of JsonForm deceive you. I don't know if this apply for all cases, but I just had to extract the Json object that contains only the data. This can be done using any common Json parser, as like as Gson.

Convert an XML file to Json using using Gson

I found that many of the tutorials on the net and here on SO also refer to net.sf.json library to convert an xml file to json object.
But, I want an alternative preferably using Gson. Is it possible? I don't have well defined Java Classes for the XML file. But, I just want to convert the xml file to com.google.gson.JsonObject. How to achieve it?
I've done the same using JAXB to convert my xml to an object, and passing the object to gson.
I know it takes one additional step, but that worked convenient for me.
Upon converting xml to jaxb see also:
Use JAXB to create Object from XML String

How to parse and deserialize an XML string with many elements if you do not have a corresponding Java object?

I got an XML string with many elements and I wonder is there a way to parse it into some kind of Java bean (Properties?) if I do not have a java bean defined? Mapping all the elements to java bean properties manually can be time-consuming, so I am looking for some kind of 'optimization' here .
Any thoughts?
If it's one level deep you can use jackson XmlMapper and map it to a Map'<'String,String>
post an example of the XML if you want more help.

XML input/output with Java fitnesse

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)

How to serialize Java class into XML with specified Schema

I have a Joo.java class and I want to serialize it into XML using the schema
JooSchema.xsd.
Is it possible with Smooks API, how to do it?
I think that is better achieved with JAXB.

Categories