I am trying to compare XML reponse of 2 REST API's. I wanted to compare few fields from Response1 to Response2.
Response1:
<d:site_name>Bondi Junction</d:site_name>
<d:country>AU</d:country>
<d:regio>NSW</d:regio>
<d:contact>123456789</d:contact>
Response2:
<d:country>AU</d:country>
<d:region>NSW</d:region>
I have created a Collection which will have both API's and I wanna run both API's and compare the available columns ( I don't want to compare completely).
Could you please guide me an approach to done.
Thanks,
Sekar
enter image description here
As far as I know, there's no easy way to compare values of xml attributes and elements without converting the xml to json.
Below is a working example(xml converted to json) for 2 use-cases (test)
Using the function difference(), you can test if the 2 xmls have any differences.
Using json path to extract the value of xml elements you can also compare specific field values
function difference(object, base) {
function changes(object, base) {
return _.transform(object, function(result, value, key) {
if (!_.isEqual(value, base[key])) {
result[key] = (_.isObject(value) && _.isObject(base[key])) ? changes(value, base[key]) : value;
}
});
}
return changes(object, base);
}
var xml1 = `<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<envelope>
<body>
<d:site_name>Bondi Junction</d:site_name>
<d:country>AU</d:country>
<d:regio>NSW</d:regio>
<d:contact>123456789</d:contact>
</body>
</envelope>
`;
var xml2 = `<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<envelope>
<body>
<d:country>AU</d:country>
<d:region>NSW</d:region>
</body>
</envelope>
`;
var jsonObject1 = xml2Json(xml1);
var jsonObject2 = xml2Json(xml2);
var diff = difference(jsonObject1, jsonObject2);
console.log(diff);
pm.test("Test for any differences in the 2 payloads", function() {
//diff object will be empty in case of no differences
pm.expect(_.isEmpty(diff)).to.eql(true);
});
pm.test("Test for the value of country in 2 payloads ", function() {
pm.expect(jsonObject1.envelope.body.country).to.eql(jsonObject2.envelope.body.country);
});
Related
I am calling a web-service method through a web-service client generated by the netbeans IDE.
private String getCitiesByCountry(java.lang.String countryName) {
webService.GlobalWeatherSoap port = service.getGlobalWeatherSoap();
return port.getCitiesByCountry(countryName);
}
So i call this method inside my program,
String b = getWeather("Katunayake", "Sri Lanka");
and it will give me a string output which contains xml data.
String b = getWeather("Katunayake", "Sri Lanka"); = (java.lang.String) <?xml version="1.0" encoding="utf-16"?>
<CurrentWeather>
<Location>Katunayake, Sri Lanka (VCBI) 07-10N 079-53E 8M</Location>
<Time>Jun 22, 2015 - 06:10 AM EDT / 2015.06.22 1010 UTC</Time>
<Wind> from the SW (220 degrees) at 10 MPH (9 KT):0</Wind>
<Visibility> greater than 7 mile(s):0</Visibility>
<SkyConditions> partly cloudy</SkyConditions>
<Temperature> 86 F (30 C)</Temperature>
<DewPoint> 77 F (25 C)</DewPoint>
<RelativeHumidity> 74%</RelativeHumidity>
<Pressure> 29.74 in. Hg (1007 hPa)</Pressure>
<Status>Success</Status>
</CurrentWeather>
How may i get the value of <Location>,<SkyConditions>,<Temperature>.
You can go for XPath if you need only these 3 values. Otherwise, DOM reads the entire document. It is very easy to write XPath expressions those directly fetch the node to read values.
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
String xml = ...; // <-- The XML SOAP response
Document xmlDocument = builder.parse(new ByteArrayInputStream(xml.getBytes()));
XPath xPath = XPathFactory.newInstance().newXPath();
String location = xPath.compile("/CurrentWeather/Location").evaluate(xmlDocument);
String skyCond = xPath.compile("/CurrentWeather/SkyConditions").evaluate(xmlDocument);
String tmp = xPath.compile("/CurrentWeather/Temperature").evaluate(xmlDocument);
If, you need to fetch many XML nodes and frequently, then go for DOM.
One way is using a DOM parser, using http://examples.javacodegeeks.com/core-java/xml/java-xml-parser-tutorial as a guide:
String b = getWeather("Katunayake", "Sri Lanka");
InputStream weatherAsStream = new ByteArrayInputStream(b.getBytes(StandardCharsets.UTF_8));
DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = fac.newDocumentBuilder();
org.w3c.dom.Document weatherDoc = builder.parse(weatherAsStream);
String location = weatherDoc.getElementsByTagName("Location").item(0).getTextContent();
String skyConditions = weatherDoc.getElementsByTagName("SkyConditions").item(0).getTextContent();
String temperature = weatherDoc.getElementsByTagName("Temperature").item(0).getTextContent();
This has no exception handling and might break if there are more than one elements with the same name, but you should be able to work from here.
I need your help about CouchDB reduce function.
I have some docs like:
{'about':'1', 'foo':'a1','bar':'qwe'}
{'about':'1', 'foo':'a1','bar':'rty'}
{'about':'1', 'foo':'a2','bar':'uio'}
{'about':'1', 'foo':'a1','bar':'iop'}
{'about':'2', 'foo':'b1','bar':'qsd'}
{'about':'2', 'foo':'b1','bar':'fgh'}
{'about':'3', 'foo':'c1','bar':'wxc'}
{'about':'3', 'foo':'c2','bar':'vbn'}
As you can seen they all have the same key, just the values are differents.
My purpse is to use a Map/Reduce and my return expectation would be:
'rows':[ 'keys':'1','value':{'1':{'foo':'a1', 'at':'rty'},
'2':{'foo':'a2', 'at':'uio'},
'3':{'foo':'a1', 'at':'iop'}}
'keys':'1','value':{'foo':'a1', 'bar','rty'}
...
'keys':'3','value':{'foo':'c2', 'bar',vbn'}
]
Here is the result of my Map function:
'rows':[ 'keys':'1','value':{'foo':'a1', 'bar','qwe'}
'keys':'1','value':{'foo':'a1', 'bar','rty'}
...
'keys':'3','value':{'foo':'c2', 'bar',vbn'}
]
But my Reduce function isn't working:
function(keys,values,rereduce){
var res= {};
var lastCheck = values[0];
for(i=0; i<values.length;++i)
{
value = values[i];
if (lastCheck.foo != value.foo)
{
res.append({'change':[i:lastCheck]});
}
lastCheck = value;
}
return res;
}
Is it possible to have what I expect or I need to use an other way ?
You should not do this in the reduce function. As the couchdb wiki explains:-
If you are building a composite return structure in your reduce, or only transforming the values field, rather than summarizing it, you might be misusing this feature.
There are two approaches that you can take instead
Transform the results at your application layer.
Use the list function.
Lists functions are simple. I will try to explain them here:
Lists like views are saved in design documents under the key lists. Like so:
"lists":{
"formatResults" : "function(head,req) {....}"
}
To call the list function you use a url like this
http://localhost:5984/your-database/_design/your-designdoc/_list/your-list-function/your-view-name
Here is an example of list function
function(head, req) {
var row = getRow();
if (!row){
return 'no ingredients'
}
var jsonOb = {};
while(row=getRow()){
//construct the json object here
}
return {"body":jsonOb,"headers":{"Content-Type" : "application/json"}};
}
The getRow function is of interest to us. It contains the result of the view. So we can query it like
row.key for key
row.value for value
All you have to do now is construct the json like you want and then send it.
By the way you can use log
to debug your functions.
I hope this helps a little.
Apparently now you need to use
provides('json', function() { ... });
As in:
Simplify Couchdb JSON response
I have a JSON file that has a key value pairs as shown below
{
"parameters": "<FieldLabel Type='Something'><Label><![CDATA[Click on this number to initiate call <a href='tel:123456' parameter='DialMe,100.200.3000'>100.200.3000'>100.200.3000'>100.200.3000'>tel:1002003000'>100.200.3000</a> or<a href='tel:911'parameter='dial911,911'>911'>911'>911'>tel:911'>911</a> ]]></Label><Description><![CDATA[]]></Description></FieldLabel>"
}
I want to replace
parameter='DialMe,100.200.3000' with my-url-click='DialMe,null,null,100.200.3000'
and
parameter='dial911,911' with my-url-click='dial911,null,null,911'
before I can render it on as HTML using Angular's ng-bind-html and $sce.trustAsHtml.
The catch is the JSON has many such key value pairs and each of them has different values for the parameter like parameter=dial108,108.So normal string replacement is not possible.How shall I do it for each of them?
Try this:
var str = "<FieldLabel Type='Something'><Label><![CDATA[Click on this number to initiate call <a href='tel:123456' parameter='DialMe,100.200.3000'>100.200.3000'>100.200.3000'>100.200.3000'>tel:1002003000'>100.200.3000</a> or<a href='tel:911'parameter='dial911,911'>911'>911'>911'>tel:911'>911</a> ]]></Label><Description><![CDATA[]]></Description></FieldLabel>";
str = str.replace(/parameter=\'.*,/g, function(s){
return s.replace("parameter", "my-url-click") + "null,null,"
});
I am using a thirdparty webservice. I got the response in XML format.
Now, i have to show the XML node values in grid view.
The following code i tried so far.
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
// Get the response stream
StreamReader reader = new StreamReader(response.GetResponseStream());
//Label2.Text = reader.ReadToEnd();
XmlDocument xml = new XmlDocument();
xml.Load(reader);
XmlNamespaceManager ns = new XmlNamespaceManager(xml.NameTable);
ns.AddNamespace("ms", "http://webservices.amazon.com/AWSECommerceService/2005-10-05");
XmlNode image = xml.SelectSingleNode("//ms:URL", ns);
XmlNode FormattedPrice = xml.SelectSingleNode("//ms:FormattedPrice", ns);
Now, i want to show the values of XMLnode value in grid view.
Please inform me, if you need more information.
Thanks in advance.
You can use an XMLDataSourcefor that:
http://www.codeproject.com/Articles/10898/Introduction-to-XMLDataSource-control-in-ASP-NET-2
Best regards.
Solution:
Instead of Get the HttpResponse in xmldocument.
I used XNamespace and XDocument.
XNamespace ns = "http://webservices.amazon.com/AWSECommerceService/2005-10-05"; // Linq
XDocument xd = XDocument.Load(response.GetResponseStream());
Then Use Linq to Read all Vlaues. Example:
var Image = xd.Descendants(ns + "Items").Elements(ns + "Item").Select(img => img.Elements(ns + "MediumImage").Select(img1 => (string)img1.Element(ns + "URL")).FirstOrDefault() ?? "Null").ToList();
I am using tinymce editor for inserting contents to mysql.
I have changed wordpress gallery editor plugin according to my system.
If there is gallery code in content. I convert this code to a symbolic photo, so that user understand there is a gallery , in stead of seeing a code. Like wordpress does.
If there is only 1 gallery in content, i convert this code to image successfully, but if there is more than 1 gallery it fails.
How can i convert all {gallery} code into a symbolic image before saving to db and convert these photos back to {gallery} code again while inserting or updating into mysql.
I am so bad on regular expression.
I think do_gallery RegExp has mistake. How should i change this.
initalising editor like:
ed.onBeforeSetContent.add(function(ed, o) {
ed.dom.loadCSS(url + "/css/gallery.css");
o.content = t._do_gallery(o.content);
});
ed.onPostProcess.add(function(ed, o) {
if (o.get)
o.content = t._get_gallery(o.content);
});
My "do and get gallery" codes like that:
_do_gallery : function(co) {
return co.replace(/\{gallery([^\]]*)\}/g, function(a,b){
var image = '<img src="gallery.gif" class="wpGallery mceItem" title="gallery'+tinymce.DOM.encode(b)+'" />';
console.log(image);
return image;
});
},
_get_gallery : function(co) {
function getAttr(s, n) {
n = new RegExp(n + '="([^"]+)"', 'g').exec(s);
return n ? tinymce.DOM.decode(n[1]) : '';
};
return co.replace(/(?:<p{^>}*>)*(<img[^>]+>)(?:<\/p>)*/g, function(a,im) {
var cls = getAttr(im, 'class');
if ( cls.indexOf('wpGallery') != -1 )
return '<p>{'+tinymce.trim(getAttr(im, 'title'))+'}</p>';
return a;
});
}
If Content is:
<p>Blah</p>
<p>{gallery Name="gallery1" id="82" galeryID="15" sizeId="6" galery_type="list"}</p>
<p>test</p>
this is ok
<img src="gallery.gif" class="wpGallery mceItem" title="gallery Name="tekne1" id="82" galeryID="15" sizeId="6" galery_type="liste"" />
But, if content is:
<p>Blah</p>
<p>{gallery Name="gallery1" id="82" galeryID="15" sizeId="6" galery_type="list"}</p>
<p>test</p>
<p>{gallery Name="gallery2" id="88" galeryID="11" sizeId="1" galery_type="slide"}</p>
<p>test2</p>
it logs
<img src="gallery.gif" class="wpGallery mceItem" title="gallery Name="gallery1" id="82" galeryID="15" sizeId="6" galery_type="list"}</p> <p>test</p> <p>{gallery Name="gallery2" id="88" galeryID="11" sizeId="1" galery_type="slide"" />
I hope i could explain my problem
Thank you.
I suspect that your original regex is a typo, looks like a missing Shift when you hit the ]. Try this:
/\{gallery([^\}]*)\}/g
Then the ([^\}]*) part will (greedily) eat up any sequence of characters that aren't }; your original one would consume any sequence of character that didn't include a ] and the result is that you'd grab everything between the first { and the last } rather than just grabbing the text between pairs of braces.