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)
Related
I'm working on a project which requires me to insert values into a predefined XML template. Till now I have been using the StringBuilder class to convert the XML file into a string and make the required changes. Now, I wanted to know whether using an XML Parser like DOM,JDOM, SAX etc would be more efficient compared to the alternative way I'm using.
Since there are no implementation issues, I don't think any piece of code needs to be shared.
Please see this it may help you:
https://www.mkyong.com/java/how-to-modify-xml-file-in-java-dom-parser/
Modifying an XML document by string replacements in general is a bad practice as you can accidentally make your XML invalid, so for your task I would rather use simple XSL transformation.
I have a Java object that uses XStream to convert the object from XML to a java object and vice versa.
I know how to convert the Java object to an XML document but i don't know how to do this without having to initialize the attributes in the XStream object. I.e. if the attribute is not initialised XStream does not generate the XML tag when it generates the XML.
What i would like to do is to generate an XSD from that Java object and to be able to do that i need an XML version of the object containing all the possible tags.
I cant really initialize each individual attribute because the XStream object has hundreds of attributes with several child attributes (i.e. a very complex structure).
Is it possible to generate the XML from an XStream object that contains all the possible tags (probably with randomized data) without having to initialize all the attributes in the XStream object?
It is probably programmable but i will have to iterate through each attribute, and each attribute's attribute and each attribute's attribute's attribute (you get the picture) to try and work out what all the tags are :)
Edit
I am also thinking that if it is possible to convert the XStream object to a JAXB object i might be able to generate the XSD as i understand it is easier to generate an XSD from JAXB. Is this the easier approach?
Xalan-J lets us create and manipulate java objects using it's extension mechanism. What I want to do is to pass an already created object from Java (from the JVM that invokes Xalan) and then manipulate this object from xslt.
For example, I would want to do something like this:
Transformer transformer = tFactory.newTransformer(new StreamSource(xsl));
HashMap mymap = ...
transformer.setParameter("MapToBeManipulatedByXsl", mymap);
transformer.transform(...)
String fromXSL = mymap.get("some-key-added-by-xsl");
Is this possible?
This isn't quite the same level of integration that you get with Xalan-J, but by far the simplest way of manipulating Java objects with XSL that I've dealt with was to use JAXB to turn the objects to/from XML.
JAXB lets you annotate your Java objects with tags specifying how you want them to appear in their equivalent XML document, then does all the work of translating Java-to-XML and XML-to-Java. Take your Java object, run it through JAXB, run your XSL over it, then run it back through JAXB to get the Java object back.
The benefits of this technique include:
You are not tied to a particular XSL environment or interpreter. Your
XSL only operates on XML.
The translation from Java to XML and back again is a core part of the JRE,
and does not require any third party libraries
You can unit test your XSLs independently of the Java
application environment, by passing in text files and verifying the
output
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.
If you have a Java object and an XML schema (XSD), what is the best way to take that object and convert it into an xml file in line with the schema. The object and the schema do not know about each other (in that the java classes weren't created from the schema).
For example, in the class, there may be an integer field 'totalCountValue' which would correspond to an element called 'countTotal' in the xsd file. Is there a way of creating a mapping that will say "if the object contains an int totalCountValue, create an element called 'countTotal' and put it into the XML".
Similarly, there may be a field in the object that should be ignored, or a list in the object that should correspond to multiple XML elements.
I looked at XStream, but didn't see any (obvious) way of doing it. Are there other XML libraries that can simplify this task?
I believe this can be achieved via JAXB using it's annotations. I've usually found it much easier to generate Objects from JAXB ( as defined in your schema) using XJC than to map an existing Java object to match my schema. YMMV.
I'm doing Object do XML serialization with XStream. What don't you find "obvious" with this serializer? Once you get the hang of it its very simple.
In the example you provided you could have something like this:
...
XStream xstream = new XStream(new DomDriver());
xstream.alias("myclass", MyClass.class);
xstream.aliasField("countTotal", MyClass.class, "totalCountValue");
String xml = xstream.toXML(this);
...
for this sample class:
class MyClass {
private int totalCountValue;
public MyClass() {
}
}
If you find some serializer more simple or "cool" than this please share it with us. I'm also looking for a change...
Check the XStream mini tutorial here
I use a java library called JiBx to do this work. You need to write a mapping file (in XML) to describe how you want the XML Schema elements to map to the java objects. There are a couple of generator tools to help with automating the process. Plus it's really fast.
I tried out most of the libraries suggested in order to see which one is most suited to my needs. I also tried out a library that was not mentioned here, but suggested by a colleague, which was a StAX implementation called Woodstox.
Admittedly my testing was not complete for all of these libraries, but for the purpose mentioned in the question, I found Woodstox to be the best. It is fastest for marshalling (in my testing, beating XStream by around 30~40%). It is also fairly easy to use and control.
The drawback to this approach is that the XML created (since it is defined by me) needs to be run through a validator to ensure that it is correct with the schema.
You can use a library from Apache Commons called Betwixt. It can map a bean to XML and then back again if you need to round trip.
Take a look at JDOM.
I would say JAXB or Castor. I have found Castor to be easier to use and more reliable, but JAXB is the standard