RestAssured - How to read an array element from the json response - java

#Test
public void getFirstRequestVerifyResponse()
{
String idToValidate= "5ea6fe53e4b09a8de7aeb19d";
System.out.println("Response :" + response.asString());
System.out.println("id : " + response.asString().contains(idToValidate));
}
I am getting the following response.
Response :[{"id":"5ea6fe53e4b09a8de7aeb19d","contents":[{"pageSize":6,"source":"BE","packageId":"ATV_PACKAGE_1538998610009"}],"name":"FEATURE BANNER","format":{"lsTy":"DEFAULT","showAll":false,"autoCarousel":false,"ty":"BANNER","action":{"source":"BE","contentId":"ATV_PACKAGE_1538998610009","t":"Play","st":"PLAY"},"contentAction":{"meta":{}}}},{"id":"5ac37644e4b03f23a4a705dc","contents":[],"name":"MastheadAd","format":{"lsTy":"DEFAULT","ty":"NATIVE_MASTHEAD_AD","action":{"source":"BE","st":"CUSTOM","sTy":"CUSTOM_AMAZON"},"contentAction":{"sTy":"CUSTOM_AMAZON","meta":{}},"lds":[],"adId":"/417241724/tv_native_masthead_plain_prod","tId":["11767767","11768950"]}},{"id":"5d63ca1de4b088013a6236b3","contents":[{"pageSize":50,"source":"RM","packageId":"dummy"}],"name":"Recommended Movies","format":{"lsTy":"DEFAULT","showAll":false,"ty":"MOVIE_NOLOGO","contentAction":{"meta":{"sourceName":"cf_movies_home"}}}},{"id":"5d63ca1de4b088013a6236b2","contents":[{"pageSize":50,"source":"RM","packageId":"dummy"}],"name":"Recommended TV Shows","format":{"lsTy":"DEFAULT","showAll":false,"ty":"MOVIE_NOLOGO","contentAction":{"meta":{"sourceName":"cf_tvshow_home"}}}},{"id":"5ae6b962e4b088c1fe893389","contents":[{"pageSize":20,"source":"MW","packageId":"ATV_PACKAGE_1524207345077","ty":"LIVE"}],"name":"LIVE NEWS","format":{"lsTy":"DEFAULT","showAll":false,"bgImgUrl":"","ty":"TVSHOW_LOGO_43","action":{"color":"#ff0000","pageId":"live_TV","t":"More","st":"LANDING"},"contentAction":{"meta":{}},"t":"LIVE NEWS"}},{"id":"5e74bffce4b0e60befa24bbb","contents":[{"pageSize":10,"source":"BE","packageId":"ATV_PACKAGE_1568185919253"}],"name":"Your Daily News in 30 Secs HIndi","format":{"lsTy":"DEFAULT","showAll":false,"bgImgUrl":"https://image.airtel.tv/pages/rails/5d2d6945e4b06e55de6b8cfe/editorji_highlightrail_background.jpg","ty":"TVSHOW_BIG_43","action":{"source":"BE","packageId":"ATV_PACKAGE_1568185919253","listingType":"TVSHOW_BIG_43","t":"More","st":"LISTING"},"contentAction":{"meta":{}},"t":"Your Daily News in 30 Secs"}},{"id":"5d9ec624e4b0499e024c99c6","contents":[{"pageSize":10,"source":"BE","packageId":"ATV_PACKAGE_1570605272423"}],"name":"Learn with Xstream","format":{"lsTy":"EXPLORE","ty":"CUSTOM","action":{"st":"DEFAULT"},"contentAction":{"source":"BE","pageId":"ATV_PACKAGE_1570605272423","st":"CUSTOM","sTy":"LISTING","meta":{}},"lds":[],"t":"Learn with Xstream"}},{"id":"5c78ce19e4b0d4a057339b17","contents":[{"pageSize":25,"source":"BE","packageId":"ATV_PACKAGE_1540027201907"}],"name":"MOST WATCHED HOLLYWOOD MOVIES","format":{"lsTy":"DEFAULT","showAll":false,"ty":"MOVIE_NOLOGO","action":{"color":"#a1ec00","source":"BE","packageId":"ATV_PACKAGE_1540027201907","listingType":"MOVIE_NOLOGO","t":"More","st":"LISTING"},"contentAction":{"meta":{}},"t":"Most Watched Hollywood Movies"}},{"id":"5e789319e4b032c54a8f0646","contents":[],"name":"Family Movie Card - English + Hindi","format":{"lsTy":"DEFAULT","ty":"CARD_NOTITILE_169","action":{"source":"MW","channelId":"MWTV_LIVETVCHANNEL_10000000060880000","t":"Play","st":"PLAY","meta":{"k":"1"},"ty":"LIVE"},"contentAction":{"meta":{}},"lds":[],"img":"https://image.airtel.tv/pages/rails/5e9083e8e4b0cff1aeeb2d58/Swami_Ramdev_New.jpg"}},{"id":"5d64b170e4b067b667714913","contents":[{"pageSize":12,"source":"BE","packageId":"ATV_PACKAGE_1566817268865"}],"name":"BEST HOLLYWOOD Series","format":{"lsTy":"DEFAULT","showAll":false,"ty":"MOVIE_NOLOGO","action":{"source":"BE","packageId":"ATV_PACKAGE_1566817268865","listingType":"MOVIE_NOLOGO","t":"More","st":"LISTING"},"contentAction":{"meta":{}},"t":"Best Hollywood Movie Series"}},{"id":"5e7892e6e4b032c54a8f0645","contents":[],"name":"Nostalgia Movie Card - English + Hindi","format":{"lsTy":"DEFAULT","ty":"CARD_NOTITILE_169","hIcon":"https://image.airtel.tv/pages/rails/5e7892e6e4b032c54a8f0645/ic_cplogo_hooq.png","action":{"source":"BE","packageId":"ATV_PACKAGE_1584939335512","listingType":"MOVIE_NOLOGO","t":"More","st":"LISTING","meta":{"A":"1"}},"contentAction":{"meta":{}},"lds":[],"img":"https://image.airtel.tv/pages/rails/5e7892e6e4b032c54a8f0645/NOSTALGIA-MOVIES-1032X576_(1).jpg"}},{"id":"5e99b557e4b0c3a92edf1ee7","contents":[],"name":"Wynk Music - Vishal Mishra - Live Concert Card Testing","format":{"lsTy":"DEFAULT","ty":"CARD_NOTITILE_169","action":{"source":"BE","contentId":"MWTV_LIVETVCHANNEL_10000000060880000","t":"Play","st":"PLAY","meta":{"k":"1"}},"contentAction":{"meta":{}},"lds":[],"img":"https://image.airtel.tv/pages/rails/5e99b010e4b03c81436668d3/1-Lohri-1032X576.jpg"}},{"id":"5ea70c25e4b0cea120315672","contents":[],"name":"Astha Gill Type Form Card Test","format":{"lsTy":"DEFAULT","ty":"PRODUCT_CARD","action":{"url":"https://wynkproduct.typeform.com/to/DesNW4","t":"Play","st":"WEBVIEW","meta":{"redirectType":"REDIRECT_TV","redirectDeeplink":"https://wynkproduct.typeform.com/to/DesNW4"}},"contentAction":{"meta":{}},"lds":[],"img":"https://image.airtel.tv/pages/rails/5ea70c25e4b0cea120315672/2-Badla-1032X576_(1).jpg"}},{"id":"5ea711a0e4b0cea120315674","contents":[],"name":"Aastha Gill - Card - Test","format":{"lsTy":"DEFAULT","ty":"CARD_NOTITILE_169","action":{"source":"MW","channelId":"MWTV_LIVETVCHANNEL_10000000060880000","t":"Play","st":"PLAY","ty":"LIVE"},"contentAction":{"meta":{}},"lds":[],"img":"https://image.airtel.tv/pages/rails/5ea711a0e4b0cea120315674/2-Badla-1032X576_(1).jpg"}},{"id":"5ea70fcfe4b0cea120315673","contents":[],"name":"test_card_ndtv","format":{"lsTy":"DEFAULT","ty":"CARD_NOTITILE_169","action":{"source":"MW","channelId":"MWTV_LIVETVCHANNEL_10000000060880000","t":"NDTV","st":"PLAY","ty":"LIVE"},"contentAction":{"meta":{}},"lt":"","ds":"","lds":[],"img":"https://image.airtel.tv/pages/rails/5ea70fcfe4b0cea120315673/photo-1549465220-1a8b9238cd48.jpeg"}}]
How can I get the "packageId" from the response for this idToValidate. Please help me on it.

You can use JsonPath
String res = "Response :[{"id":"5ea6fe53e4b09a8de7aeb19d","contents":[{"pageSize":6,"source":"BE","packageId":"ATV_PACKAGE_1538998610009"}],"name":"FEATURE BANNER","format":{"lsTy":"DEFAULT","showAll":false,"autoCarousel":false,"ty":"BANNER","action":{"source":"BE","contentId":"ATV_PACKAGE_1538998610009","t":"Play","st":"PLAY"},"contentAction":{"meta":{}}}},{"id":"5ac37644e4b03f23a4a705dc","contents":[],"name":"MastheadAd","format":{"lsTy":"DEFAULT","ty":"NATIVE_MASTHEAD_AD","action":{"source":"BE","st":"CUSTOM","sTy":"CUSTOM_AMAZON"},"contentAction":{"sTy":"CUSTOM_AMAZON","meta":{}},"lds":[],"adId":"/417241724/tv_native_masthead_plain_prod","tId":["11767767","11768950"]}},{"id":"5d63ca1de4b088013a6236b3","contents":[{"pageSize":50,"source":"RM","packageId":"dummy"}],"name":"Recommended Movies","format":{"lsTy":"DEFAULT","showAll":false,"ty":"MOVIE_NOLOGO","contentAction":{"meta":{"sourceName":"cf_movies_home"}}}},{"id":"5d63ca1de4b088013a6236b2","contents":[{"pageSize":50,"source":"RM","packageId":"dummy"}],"name":"Recommended TV Shows","format":{"lsTy":"DEFAULT","showAll":false,"ty":"MOVIE_NOLOGO","contentAction":{"meta":{"sourceName":"cf_tvshow_home"}}}},{"id":"5ae6b962e4b088c1fe893389","contents":[{"pageSize":20,"source":"MW","packageId":"ATV_PACKAGE_1524207345077","ty":"LIVE"}],"name":"LIVE NEWS","format":{"lsTy":"DEFAULT","showAll":false,"bgImgUrl":"","ty":"TVSHOW_LOGO_43","action":{"color":"#ff0000","pageId":"live_TV","t":"More","st":"LANDING"},"contentAction":{"meta":{}},"t":"LIVE NEWS"}},{"id":"5e74bffce4b0e60befa24bbb","contents":[{"pageSize":10,"source":"BE","packageId":"ATV_PACKAGE_1568185919253"}],"name":"Your Daily News in 30 Secs HIndi","format":{"lsTy":"DEFAULT","showAll":false,"bgImgUrl":"https://image.airtel.tv/pages/rails/5d2d6945e4b06e55de6b8cfe/editorji_highlightrail_background.jpg","ty":"TVSHOW_BIG_43","action":{"source":"BE","packageId":"ATV_PACKAGE_1568185919253","listingType":"TVSHOW_BIG_43","t":"More","st":"LISTING"},"contentAction":{"meta":{}},"t":"Your Daily News in 30 Secs"}},{"id":"5d9ec624e4b0499e024c99c6","contents":[{"pageSize":10,"source":"BE","packageId":"ATV_PACKAGE_1570605272423"}],"name":"Learn with Xstream","format":{"lsTy":"EXPLORE","ty":"CUSTOM","action":{"st":"DEFAULT"},"contentAction":{"source":"BE","pageId":"ATV_PACKAGE_1570605272423","st":"CUSTOM","sTy":"LISTING","meta":{}},"lds":[],"t":"Learn with Xstream"}},{"id":"5c78ce19e4b0d4a057339b17","contents":[{"pageSize":25,"source":"BE","packageId":"ATV_PACKAGE_1540027201907"}],"name":"MOST WATCHED HOLLYWOOD MOVIES","format":{"lsTy":"DEFAULT","showAll":false,"ty":"MOVIE_NOLOGO","action":{"color":"#a1ec00","source":"BE","packageId":"ATV_PACKAGE_1540027201907","listingType":"MOVIE_NOLOGO","t":"More","st":"LISTING"},"contentAction":{"meta":{}},"t":"Most Watched Hollywood Movies"}},{"id":"5e789319e4b032c54a8f0646","contents":[],"name":"Family Movie Card - English + Hindi","format":{"lsTy":"DEFAULT","ty":"CARD_NOTITILE_169","action":{"source":"MW","channelId":"MWTV_LIVETVCHANNEL_10000000060880000","t":"Play","st":"PLAY","meta":{"k":"1"},"ty":"LIVE"},"contentAction":{"meta":{}},"lds":[],"img":"https://image.airtel.tv/pages/rails/5e9083e8e4b0cff1aeeb2d58/Swami_Ramdev_New.jpg"}},{"id":"5d64b170e4b067b667714913","contents":[{"pageSize":12,"source":"BE","packageId":"ATV_PACKAGE_1566817268865"}],"name":"BEST HOLLYWOOD Series","format":{"lsTy":"DEFAULT","showAll":false,"ty":"MOVIE_NOLOGO","action":{"source":"BE","packageId":"ATV_PACKAGE_1566817268865","listingType":"MOVIE_NOLOGO","t":"More","st":"LISTING"},"contentAction":{"meta":{}},"t":"Best Hollywood Movie Series"}},{"id":"5e7892e6e4b032c54a8f0645","contents":[],"name":"Nostalgia Movie Card - English + Hindi","format":{"lsTy":"DEFAULT","ty":"CARD_NOTITILE_169","hIcon":"https://image.airtel.tv/pages/rails/5e7892e6e4b032c54a8f0645/ic_cplogo_hooq.png","action":{"source":"BE","packageId":"ATV_PACKAGE_1584939335512","listingType":"MOVIE_NOLOGO","t":"More","st":"LISTING","meta":{"A":"1"}},"contentAction":{"meta":{}},"lds":[],"img":"https://image.airtel.tv/pages/rails/5e7892e6e4b032c54a8f0645/NOSTALGIA-MOVIES-1032X576_(1).jpg"}},{"id":"5e99b557e4b0c3a92edf1ee7","contents":[],"name":"Wynk Music - Vishal Mishra - Live Concert Card Testing","format":{"lsTy":"DEFAULT","ty":"CARD_NOTITILE_169","action":{"source":"BE","contentId":"MWTV_LIVETVCHANNEL_10000000060880000","t":"Play","st":"PLAY","meta":{"k":"1"}},"contentAction":{"meta":{}},"lds":[],"img":"https://image.airtel.tv/pages/rails/5e99b010e4b03c81436668d3/1-Lohri-1032X576.jpg"}},{"id":"5ea70c25e4b0cea120315672","contents":[],"name":"Astha Gill Type Form Card Test","format":{"lsTy":"DEFAULT","ty":"PRODUCT_CARD","action":{"url":"https://wynkproduct.typeform.com/to/DesNW4","t":"Play","st":"WEBVIEW","meta":{"redirectType":"REDIRECT_TV","redirectDeeplink":"https://wynkproduct.typeform.com/to/DesNW4"}},"contentAction":{"meta":{}},"lds":[],"img":"https://image.airtel.tv/pages/rails/5ea70c25e4b0cea120315672/2-Badla-1032X576_(1).jpg"}},{"id":"5ea711a0e4b0cea120315674","contents":[],"name":"Aastha Gill - Card - Test","format":{"lsTy":"DEFAULT","ty":"CARD_NOTITILE_169","action":{"source":"MW","channelId":"MWTV_LIVETVCHANNEL_10000000060880000","t":"Play","st":"PLAY","ty":"LIVE"},"contentAction":{"meta":{}},"lds":[],"img":"https://image.airtel.tv/pages/rails/5ea711a0e4b0cea120315674/2-Badla-1032X576_(1).jpg"}},{"id":"5ea70fcfe4b0cea120315673","contents":[],"name":"test_card_ndtv","format":{"lsTy":"DEFAULT","ty":"CARD_NOTITILE_169","action":{"source":"MW","channelId":"MWTV_LIVETVCHANNEL_10000000060880000","t":"NDTV","st":"PLAY","ty":"LIVE"},"contentAction":{"meta":{}},"lt":"","ds":"","lds":[],"img":"https://image.airtel.tv/pages/rails/5ea70fcfe4b0cea120315673/photo-1549465220-1a8b9238cd48.jpeg"}}]";
Code :
JsonPath js = new JsonPath(res);
String idToValidate = "5ae6b962e4b088c1fe893389";
System.out.println("id : " + res.contains(idToValidate));
String packageId = js.get("find {it.id =='"+idToValidate+"'}.contents.packageId").toString();
System.out.println("PackageID : " + packageId);
Output :
ID : true
PackageID : [ATV_PACKAGE_1524207345077]
Alternative :
String output = given().when().get(url).then().extract().body().jsonPath().get("find {it.id =='"+idToValidate+"'}.contents.packageId").toString();
System.out.println(output);

Related

How to get the method to return and print in main? Java

I've written the whole program, but I still can't get the discount to apply to the second print.
Here is my code:
public static void main(String[] args) {
// create array
Book[] bookArray = new Book[6];
// use parameterized constructor to create books
bookArray[0] = new Book("Java Programming", "Liang", 1320, 145.00);
bookArray[1] = new Book("Horton Hears a Who!", "Dr. Suess", 72, 19.99);
bookArray[2] = new Book("The Hobbit", "Tolkien", 320, 9.25);
bookArray[3] = new Book("Born a Crime", "Noah", 304, 17.33);
// use no-arg constructor to create books
bookArray[4] = new Book();
bookArray[5] = new Book();
// foreach to print
for (Book book: bookArray) {
System.out.println(book);
}
System.out.println();
finishArray(bookArray);
System.out.println("Books after completing library and 40% discount");
for (Book book: bookArray) {
System.out.println(book);
}
System.out.println("Most Expensive book after the discounts:\n" + reduceBooks(bookArray));
System.out.println("Size of Library: " + bookArray.length + " books");
}
public static void finishArray(Book bookArray[]) {
Book book;
book = bookArray[4];
book.setTitle("Dark Territory");
book.setAuthor("Kaplan");
book.setPages(352);
book.setPrice(11.24);
book = bookArray[5];
book.setTitle("Born to Run");
book.setAuthor("Springsteen");
book.setPages(508);
book.setPrice(12.17);
}
public static Book reduceBooks(Book bookArray[]) {
Book most = bookArray[0];
for (int i = 0; i < bookArray.length; i++) {
Book book = bookArray[i];
// see if this book is more expensive than current most
if(book.getPrice() > most.getPrice()) {
most = book;
}
// reduce price
book.setPrice(book.getPrice() * 0.6);
}
return most; // return most expensive
}
}
Here's my output which as you can see the discount does not apply to the second printing:
Title: Java Programming
Author: Liang
Pages: 1320
Price: 145.00
Title: Horton Hears a Who!
Author: Dr. Suess
Pages: 72
Price: 19.99
Title: The Hobbit
Author: Tolkien
Pages: 320
Price: 9.25
Title: Born a Crime
Author: Noah
Pages: 304
Price: 17.33
Title: null
Author: null
Pages: 0
Price: 0.00
Title: null
Author: null
Pages: 0
Price: 0.00
Books after completing library and 40% discount
Title: Java Programming
Author: Liang
Pages: 1320
Price: 145.00
Title: Horton Hears a Who!
Author: Dr. Suess
Pages: 72
Price: 19.99
Title: The Hobbit
Author: Tolkien
Pages: 320
Price: 9.25
Title: Born a Crime
Author: Noah
Pages: 304
Price: 17.33
Title: Dark Territory
Author: Kaplan
Pages: 352
Price: 11.24
Title: Born to Run
Author: Springsteen
Pages: 508
Price: 12.17
Most Expensive book after the discounts:
Title: Java Programming
Author: Liang
Pages: 1320
Price: 87.00
Size of Library: 6 books
Also, these are the instructions for the reduceBooks(), everything must be done in that method-
In reduceBooks():
this method returns a Book instance.
use a loop (any type) to reduce the price of every book in the array by 40%.
determine the most expensive book after the discounts and return this book to main.
You should apply discount before second print. Also, do not forget to remove reducing price code from reduceBooks or else you'll reduce price twice.
Here is an example how you can do it:
// first print
applyDiscount(bookArray);
// second print
public static void applyDiscount(Book bookArray[]) {
for (int i = 0; i < bookArray.length; i++) {
Book book = bookArray[i];
// reduce price
book.setPrice(book.getPrice() * 0.6);
}
}

How to parse HL7 multiple segments(ORC/OBR/OBX) using HAPI Frame work?

How to parse HL7 multiple segments(ORC/OBR/OBX) using HAPI Framework.
I am also facing issue related to parsing the multiple lab order and also facing issue related to special character(MSH|^~\&#|) encoding .
MSH|^~\&|NIST Test Lab APP|NIST Lab Facility||NIST EHR Facility|20110531140551-0500||ORU^R01^ORU_R01|NIST-LRI-NG-RN-005.01|T|2.5.1|||AL|NE|||||LRI_Common_Component^^2.16.840.1.113883.9.16^ISO~LRI_NG_Component^^2.16.840.1.113883.9.13^ISO~LRI_RN_Component^^2.16.840.1.113883.9.15^ISO
PID|1||PATID1239^^^NIST MPI^MR||Smirnoff^Peggy^^^^^M||19750401|F||2106-3^White^HL70005^wh^white^L
ORC|RE|ORD448811^NIST EHR|R-511^NIST Lab Filler|||||||||1234567890^Fine^Larry^^^Dr.^^^NIST-AA-1^L^^^NPI
OBR|1|ORD448811^NIST EHR|R-511^NIST Lab Filler|HepABC Panel^Hepatitis A B C Panel^L|||20120628070100|||||||||1234567890^Fine^Larry^^^Dr.^^^NIST-AA-1^L^^^NPI||||||20120629132900-0500|||F
OBX|1|CWE|22314-9^Hepatitis A virus IgM Ab [Presence] in Serum^LN^HAVM^Hepatitis A IgM antibodies (IgM anti-HAV)^L||260385009^Negative (qualifier value)^SCT^NEG^NEGATIVE^L^^^Negative (qualifier value)||Negative|N|||F|||20120628070100|||||20120628100500||||Princeton Hospital Laboratory^^^^^NIST HCAA-1^XX^^^34D4567890|123 High Street^^Princeton^NJ^08540^USA^O^^34021|^Martin^Steven^M^^Dr.
OBX|2|CWE|20575-7^Hepatitis A virus Ab [Presence] in Serum^LN^HAVAB^Hepatitis A antibodies (anti-HAV)^L||260385009^Negative (qualifier value)^SCT^NEG^NEGATIVE^L^^^Negative (qualifier value)||Negative|N|||F|||20120628070100|||||20120628100500||||Princeton Hospital Laboratory^^^^^NIST HCAA-1^XX^^^34D4567890|123 High Street^^Princeton^NJ^08540^USA^O^^34021|^Martin^Steven^M^^Dr.
OBX|3|CWE|16933-4^Hepatitis B virus core Ab [Presence] in Serum^LN^HBVcAB^Hepatitis B core antibodies (anti-HBVc)^L||260385009^Negative (qualifier value)^SCT^NEG^NEGATIVE^L^^^Negative (qualifier value)||Negative|N|||F|||20120628070100|||||20120628100500||||Princeton Hospital Laboratory^^^^^NIST HCAA-1^XX^^^34D4567890|123 High Street^^Princeton^NJ^08540^USA^O^^34021|^Martin^Steven^M^^Dr.
OBX|4|SN|22316-4^Hepatitis B virus core Ab [Units/volume] in Serum^LN^HBcAbQ^Hepatitis B core antibodies (anti-HBVc) Quant^L||^0.40|[IU]/mL^international unit per milliliter^UCUM^IU/ml^^L|<0.50 IU/mL|N|||F|||20120628070100|||||20120628100500||||Princeton Hospital Laboratory^^^^^NIST HCAA-1^XX^^^34D4567890|123 High Street^^Princeton^NJ^08540^USA^O^^34021|^Martin^Steven^M^^Dr.
OBX|5|CWE|22320-6^Hepatitis B virus e Ab [Presence] in Serum^LN^HBVeAB^Hepatitis B e antibodies (anti-HBVe)^L||260385009^Negative (qualifier value)^SCT^NEG^NEGATIVE^L^^^Negative (qualifier value)||Negative|N|||F|||20120628070100|||||20120628100500||||Princeton Hospital Laboratory^^^^^NIST HCAA-1^XX^^^34D4567890|123 High Street^^Princeton^NJ^08540^USA^O^^34021|^Martin^Steven^M^^Dr.
OBX|6|CWE|5195-3^Hepatitis B virus surface Ag [Presence] in Serum^LN^HBVsAG^Hepatitis B surface antigen (HBsAg)^L||260385009^Negative (qualifier value)^SCT^NEG^NEGATIVE^L^^^Negative (qualifier value)||Negative|N|||F|||20120628070100|||||20120628100500||||Princeton Hospital Laboratory^^^^^NIST HCAA-1^XX^^^34D4567890|123 High Street^^Princeton^NJ^08540^USA^O^^34021|^Martin^Steven^M^^Dr.
OBX|7|CWE|22322-2^Hepatitis B virus surface Ab [Presence] in Serum^LN^HBVSAB^Hepatitis B surface antibody (anti-HBVs)^L||260385009^Negative (qualifier value)^SCT^NEG^NEGATIVE^L^^^Negative (qualifier value)||Negative|N|||F|||20120628070100|||||20120628100500||||Princeton Hospital Laboratory^^^^^NIST HCAA-1^XX^^^34D4567890|123 High Street^^Princeton^NJ^08540^USA^O^^34021|^Martin^Steven^M^^Dr.
OBX|8|CWE|16128-1^Hepatitis C virus Ab [Presence] in Serum^LN^HCVAB^Hepatitis C antibody screen (anti-HCV)^L||10828004^Positive (qualifier value)^SCT^POS^POSITIVE^L^^^Positive (qualifier value)||Negative|A|||F|||20120628070100|||||20120628100500||||Princeton Hospital Laboratory^^^^^NIST HCAA-1^XX^^^34D4567890|123 High Street^^Princeton^NJ^08540^USA^O^^34021|^Martin^Steven^M^^Dr.
OBX|9|SN|48159-8^Hepatitis C virus Ab Signal/Cutoff in Serum or Plasma by Immunoassay^LN^HCVSCO^Hepatitis C antibodies Signal to Cut-off Ratio^L||^10.8|{s_co_ratio}^Signal to cutoff ratio^UCUM^s/co^^L|0.0-0.9 s/co|H|||F|||20120628070100|||||20120628100500||||Princeton Hospital Laboratory^^^^^NIST HCAA-1^XX^^^34D4567890|123 High Street^^Princeton^NJ^08540^USA^O^^34021|^Martin^Steven^M^^Dr.
NTE|1||Negative: < 0.8; Indeterminate 0.8 - 0.9; Positive: > 0.9. In order to reduce the incidence of a false positive result, the CDC recommends that all s/co ratios between 1.0 and 10.9 be confirmed with additional Verification or PCR testing.
SPM|1|||119364003^Serum specimen (specimen)^SCT^SER^Serum^L|||||||||||||20120628070100
ORC|RE||R-511^NIST Lab Filler|||||||||1234567890^Fine^Larry^^^Dr.^^^NIST-AA-1^L^^^NPI|||||||||||||||||||HepABC Panel^Hepatitis A B C Panel^L
OBR|2||R-511^NIST Lab Filler|11011-4^Hepatitis C virus RNA [Units/volume] (viral load) in Serum or Plasma by Probe and target amplification method^LN^HCVRNA^Hepatitis C RNA PCR^L|||20120628070100||||G|||||1234567890^Fine^Larry^^^Dr.^^^NIST-AA-1^L^^^NPI||||||20120629132900-0500|||F|16128-1&Hepatitis C virus Ab [Presence] in Serum&LN&HCVAB&Hepatitis C antibody screen (anti-HCV)&L|||ORD448811&NIST EHR^R-511&NIST Lab Filler|||||||||||||||||||||HepABC Panel^Hepatitis A B C Panel^L
OBX|1|SN|11011-4^Hepatitis C virus RNA [Units/volume] (viral load) in Serum or Plasma by Probe and target amplification method^LN^HCVRNA^Hepatitis C RNA PCR^L||^7611200|[IU]/mL^international unit per milliliter^UCUM^IU/ml^^L|<43 IU/mL|H|||F|||20120628070100|||||20120629092700||||Princeton Hospital Laboratory^^^^^NIST HCAA-1^XX^^^34D4567890|123 High Street^^Princeton^NJ^08540^USA^O^^34021|^Martin^Steven^M^^Dr.
Code Snippet:
package HAPIHL7Parser.HAPIHL7Parser;
import java.io.FileReader;
import ca.uhn.hl7v2.DefaultHapiContext;
import ca.uhn.hl7v2.HapiContext;
import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.parser.Parser;
import ca.uhn.hl7v2.util.Hl7InputStreamMessageIterator;
import ca.uhn.hl7v2.util.Terser;
import ca.uhn.hl7v2.validation.impl.NoValidation;
/**
* 40 * Example code for using the {#link Terser} 41 * 42 * #author
* Sitansu 43 * #version $Revision:
* 1.3 $ updated on $Date: 2009-08-06 22:43:33 $ by $Author: Sitansu $ 44
*/
public class ExampleUseTerser {
/**
* 49 * A simple example of parsing a message 50 * 51 * #throws HL7Exception
* 52 * #throws EncodingNotSupportedException 53
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
FileReader reader = new FileReader("src\\main\\resource\\LRI_5.1-NG-RN_Reflex_Hepatitis_Parent_Child_Message.txt");
// Create an iterator to iterate over all the messages
Hl7InputStreamMessageIterator iter = new Hl7InputStreamMessageIterator(reader);
HapiContext context = new DefaultHapiContext();
context.setValidationContext(new NoValidation());
Parser p = context.getGenericParser();
String msg = null;
while (iter.hasNext()) {
/*
* If we don't already have a connection, create one. 42 * Note that
* unless something goes wrong, it's very common 43 * to keep
* reusing the same connection until we are done 44 * sending
* messages. Many systems keep a connection open 45 * even if a long
* period will pass between messages being 46 * sent. This is good
* practice, as it is much faster than 47 * creating a new
* connection each time. 48
*/
msg = iter.next().toString();
System.out.println("Sent message. Response was " + msg);
}
Message hapiMsg = p.parse(msg);
// Message hapiMsg = p.parse(next);
/*
* 70 * Another way of reading messages is to use a Terser. The terser
* 71 * accepts a particular syntax to retrieve segments. See the API 72
* * documentation for the Terser for more information. 73
*/
Terser terser = new Terser(hapiMsg);
/*
* 77 * Sending Application is in MSH-3-1 (the first component of the
* third 78 * field of the MSH segment) 79
*/
String sendingApplication = terser.get("/.MSH-3");
System.out.println("Sending Application:" + sendingApplication);
String sendingfacility = terser.get("/.MSH-4");
System.out.println("Sending Facility:" + sendingfacility);
String timeofdate = terser.get("/.MSH-7");
System.out.println("Date/time of the message:" + timeofdate);
String messagetype = terser.get("/.MSH-9");
System.out.println("Message Type:" + messagetype);
String messageControlID = terser.get("/.MSH-10");
System.out.println("Message Control ID:" + messageControlID);
String patientIDTypeCode = terser.get("/.PID-3-5");
System.out.println("Patient ID Type Code:" + patientIDTypeCode);
String dateOfBirth = terser.get("/.PID-7");
System.out.println("Date of Birth:" + dateOfBirth);
String placerGroupNumber = terser.get("/.ORC-4-1");
System.out.println("Placer Group Number:" + placerGroupNumber);
String placerOrderNo = terser.get("/.ORC-2-1");
System.out.println("Placer Order Number:" + placerOrderNo);
String fillerOrderNo = terser.get("/.ORC-3-1");
System.out.println("Filler Order Number:" + fillerOrderNo);
String orderProviderID = terser.get("/.ORC-12-1");
System.out.println("Ordering Provider ID:" + orderProviderID);
String orderProviderIDType = terser.get("/.ORC-12-13");
System.out.println("Ordering Provider ID Type:" + orderProviderIDType);
String plcOrderNO = terser.get("/.OBR-2-1");
System.out.println("Placer Order Number:" + plcOrderNO);
String fillrOrdNo = terser.get("/.OBR-3-1");
System.out.println("Filler Order Number:" + fillrOrdNo);
String testCode = terser.get("/.OBR-4-1");
System.out.println("Test Code:" + testCode);
String testDesc = terser.get("/.OBR-4-2");
System.out.println("Test Description:" + testDesc);
String testALTDesc = terser.get("/.OBR-4-9");
System.out.println("Test alternative Description:" + testALTDesc);
String obsDateTime = terser.get("/.OBR-7-1");
System.out.println("Observation Date Time:" + obsDateTime);
String speciActionCode = terser.get("/.OBR-11");
System.out.println("Specimen Action Code:" + speciActionCode);
String relevantClinicInfo = terser.get("/.OBR-13-1");
System.out.println("Relevant Clinical Information:" + relevantClinicInfo);
String relevantClinicInfo2 = terser.get("/.OBR-13-2");
System.out.println("Relevant Clinical Information:" + relevantClinicInfo2);
String resultStatus = terser.get("/.OBR-25");
System.out.println("Result Status:" + resultStatus);
String test = terser.get("/.OBSERVATION(0)/OBX-3-1");
System.out.println("Result Status:" + test);
String test1 = terser.get("/.OBSERVATION(2)/OBX-3-1");
System.out.println("Result Status:" + test1);
String test2 = terser.get("/.OBSERVATION(3)/OBX-3-1");
System.out.println("Result Status:" + test2);
String test3 = terser.get("/.OBSERVATION(4)/OBX-3-1");
System.out.println("Result Status:" + test3);
//terser.getFinder().iterate(, true);
String testNotes = terser.get("/.NTE");
System.out.println("Test Notes:" + testNotes);
String speType = terser.get("/.SPM-4-9");
System.out.println("Specimen Type:" + speType);
String speciRejReason = terser.get("/.SPM-21-9");
System.out.println("Specimen Reject reason:" + speciRejReason);
String specCond = terser.get("/.SPM-24-9");
System.out.println("Specimen condition:" + specCond);
String specCollStartDateTime = terser.get("/.SPM-17-1-1");
System.out.println("Speciment Collection Start Date Time:" + specCollStartDateTime);
String notesCommts = terser.get("/.NTE-3");
System.out.println("Notes and Comments:" + notesCommts);
String obxID = terser.get("/.OBX-3-1");
System.out.println("Observation ID:" + obxID);
String obxText = terser.get("/.OBX-3-2");
System.out.println("Observation Text:" + obxText);
String obxText2 = terser.get("/.OBX-3-9");
System.out.println("Observation Text:" + obxText2);
String obxValue = terser.get("/.OBX-5-1");
System.out.println("Observation Value:" + obxValue);
String obxUnits = terser.get("/.OBX-6-1");
System.out.println("Observation Units:" + obxUnits);
String refRange = terser.get("/.OBX-7");
System.out.println("Reference Range:" + refRange);
String abnormalFlags = terser.get("/.OBX-8");
System.out.println("Abnormal Flags:" + abnormalFlags);
String obXResultStatus = terser.get("/.OBX-11");
System.out.println("Observation Result Status:" + obXResultStatus);
String dateOfObservation = terser.get("/.OBX-14-1");
System.out.println("Date/Time of the Observation:" + dateOfObservation);
String perOrgName = terser.get("/.OBX-23-1");
System.out.println("Performing Organization Name:" + perOrgName);
String perOrgAdd1 = terser.get("/.OBX-24-1");
System.out.println("Performing Organization address Line 1:" + perOrgAdd1);
String perOrgAdd2 = terser.get("/.OBX-24-2");
System.out.println("Performing Organization address Line 2:" + perOrgAdd2);
String perOrgCity = terser.get("/.OBX-24-3");
System.out.println("Performing Organization City:" + perOrgCity);
String perOrgState = terser.get("/.OBX-24-4");
System.out.println("Performing Organization State:" + perOrgState);
String perOrgZipCode = terser.get("/.OBX-24-5");
System.out.println("Performing Organization Zip code:" + perOrgZipCode);
// HIS
/*
* 85 * We can use brackets to get particular repetitions 86
*/
/*
* 108 * MSH|^~\&|||||||ORU^R01|||2.5 109 * OBX|1||ST||This is the value
* for rep 0 110 * OBX|2||ST||This is the value for rep 1 111 *
* OBX|3||ST||This is the value for rep 2 112 * OBX|4||ST||This is the
* value for rep 3 113 * OBX|5||ST||This is the value for rep 4
*/
}
}
You can specify index while get call as below:
String perOrgCity = terser.get("/.OBX1-24-3");
Note the 1 in OBX1 in above code. Indexes zero based. You can simply replace 1 with your loop counter if you want to read multiple of them.
Here is the similar question.

Mainframe comp-3 field reading using JRecord

I am trying to read mainframe file but all are working other than comp 3 file.Below program is giving strange values.It is not able to read the salary value which is double also it is giving 2020202020.20 values. I don't know what am missing.Please help me to find it.
Program:
public final class Readcopybook {
private String dataFile = "EMPFILE.txt";
private String copybookName = "EMPCOPYBOOK.txt";
public Readcopybook() {
super();
AbstractLine line;
try {
ICobolIOBuilder iob = JRecordInterface1.COBOL.newIOBuilder(copybookName)
.setFileOrganization(Constants.IO_BINARY_IBM_4680).setSplitCopybook(CopybookLoader.SPLIT_NONE);
AbstractLineReader reader = iob.newReader(dataFile);
while ((line = reader.read()) != null) {
System.out.println(line.getFieldValue("EMP-NO").asString() + " "
+ line.getFieldValue("EMP-NAME").asString() + " "
+ line.getFieldValue("EMP-ADDRESS").asString() + " "
+ line.getFieldValue("EMP-SALARY").asString() + " "
+ line.getFieldValue("EMP-ZIPCODE").asString());
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Readcopybook();
}
}
EMPCOPYBOOK:
001700 01 EMP-RECORD.
001900 10 EMP-NO PIC 9(10).
002000 10 EMP-NAME PIC X(30).
002100 10 EMP-ADDRESS PIC X(30).
002200 10 EMP-SALARY PIC S9(8)V9(2) COMP-3.
002200 10 EMP-ZIPCODE PIC 9(4).
EMPFILE:
0000001001suneel kumar r bangalore e¡5671
0000001002JOSEPH WHITE FIELD rrn4500
Output:
1001 suneel kumar r bangalore 20200165a10 5671
2020202020.20
2020202020.20
2020202020.20
2020202020.20
2020202020.20
2020202020.20
2020202020.20
2020202020.20
0.00
1002 JOSEPH WHITE FIELD 202072726e0 4500
One problem is you have done a Ebcdic to Ascii conversion on the file.
The 2020... is a dead give away x'20' is the ascii space character.
This Answer deals with problems with doing an Ebcdic to ascii conversion.
You need to do a Binary transfer from the Mainframe and read the file using Ebcdic. You will need to check the RECFM on the Mainframe. If the RECFM is
FB - problems just transfer
VB - either convert to FB on the mainframe of include the RDW (Record Descriptor Word) option in the transfer.
Other - Convert to FB/VB on the mainframe
Updated java Code
int fileOrg = Constants.IO_FIXED_LENGTH_RECORDS; // or Constants.IO_VB
ICobolIOBuilder iob = JRecordInterface1.COBOL
.newIOBuilder(copybookName)
.setFileOrganization(fileOrg)
.setFont("Cp037")
.setSplitCopybook(CopybookLoader.SPLIT_NONE);
Note: IO_BINARY_IBM_4680 is for IBM 4690 Registers
There is a wiki entry here
or this Question
How do you generate java~jrecord code fror a Cobol copybook

Java GRIB-Decoder: Extract data from GRIB2 files

I've downloaded some grib data files from here: ftp://data-portal.ecmwf.int/20160721000000/ (file type is .bin) and want to extract the data from this file in my Java application (I want to load the extracted data into a database later). I'm just trying with the file ftp://wmo:essential#data-portal.ecmwf.int/20160721000000/A_HWXE85ECEM210000_C_ECMF_20160721000000_24h_em_ws_850hPa_global_0p5deg_grib2.bin.
Therefore I've created a new Java project and added the two libraries grib-8.0.29.jar and netcdfAll-4.6.6.jar. Documentation for the grib API can be found here: http://www.unidata.ucar.edu/software/decoders/grib/javadoc/. I need to open the downloaded files to get the data. Retrieving some metadata via Grib2Dump seems to work (see below). Also the Grib2Input instance sais, that I have a valid GRIB file of version 2.
Here my working code for retrieving some metadata:
public static void main(String[] args) throws IOException, InterruptedException {
File srcDir = new File("C://test//");
File[] localFiles = srcDir.listFiles();
for (File tempFile : localFiles) {
RandomAccessFile raf = new RandomAccessFile(tempFile.getAbsolutePath(), "r");
System.out.println("======= Grib2GDSVariables ==========");
Grib2GDSVariables gdsVariables = new Grib2GDSVariables(raf.readBytes(raf.read()));
System.out.println("Gds key : " + gdsVariables.getGdsKey());
System.out.println("======= Grib2Input ==========");
Grib2Input input = new Grib2Input(raf);
System.out.println(Grib2Input.isValidFile(raf));
System.out.println("scan : " + input.scan(true, true));
System.out.println("getGDSs.size: " + input.getGDSs().size());
System.out.println("getProducts.size: " + input.getProducts().size());
System.out.println("getRecords.size: " + input.getRecords().size());
System.out.println("edition: " + input.getEdition());
System.out.println("======= Grib2Dump ==========");
Grib2Dump dump = new Grib2Dump();
dump.gribDump(new String[] {tempFile.getAbsolutePath()});
System.out.println("======= Grib2ExtractRawData ==========");
Grib2ExtractRawData extractRawData = new
Grib2ExtractRawData(raf); extractRawData.main(new String[] {tempFile.getAbsolutePath()});
}
System.out.println("finished");
}
This produces the following output:
======= Grib2GDSVariables ==========
Gds key : -1732955898
======= Grib2Input ==========
true
scan : true
getGDSs.size: 0
getProducts.size: 0
getRecords.size: 0
edition: 2
======= Grib2Dump ==========
--------------------------------------------------------------------
Header : GRIB2
Discipline : 0 Meteorological products
GRIB Edition : 2
GRIB length : 113296
Originating Center : 98 European Center for Medium-Range Weather Forecasts (RSMC)
Originating Sub-Center : 0
Significance of Reference Time : 1 Start of forecast
Reference Time : 2016-07-21T00:00:00Z
Product Status : 0 Operational products
Product Type : 1 Forecast products
Number of data points : 259920
Grid Name : 0 Latitude_Longitude
Grid Shape: 6 Earth spherical with radius of 6,371,229.0 m
Number of points along parallel: 720
Number of points along meridian: 361
Basic angle : 0
Subdivisions of basic angle: -9999
Latitude of first grid point : 90.0
Longitude of first grid point : 0.0
Resolution & Component flags : 48
Winds : True
Latitude of last grid point : -90.0
Longitude of last grid point : 359.5
i direction increment : 0.5
j direction increment : 0.5
Grid Units : degrees
Scanning mode : 0
Product Definition : 2 Derived forecast on all ensemble members at a point in time
Parameter Category : 2 Momentum
Parameter Name : 1 Wind_speed
Parameter Units : m s-1
Generating Process Type : 4 Ensemble Forecast
ForecastTime : 24
First Surface Type : 100 Isobaric surface
First Surface value : 85000.0
Second Surface Type : 255 Missing
Second Surface value : -9.999E-252
======= Grib2ExtractRawData ==========
finished
I tried around for two days now but couldn't get it to work! I can't obtain the content data (lat, lon, value) from the file...
Can someone give an example in Java?
You shouldn't use the GRIB classes in netCDF-java directly. Instead, use
NetcdfFile.open()
That will give you access through the CDM, giving you a straightforward interface with variables and attributes. There's a tutorial here: https://www.unidata.ucar.edu/software/thredds/current/netcdf-java/tutorial/NetcdfFile.html

How to browse and to display XML content with Java

I have a problem in Java with DOM...
Here is the XML code :
<?xml version="1.0" encoding="UTF-8"?>
<bib>
<domain>
<title>Specifications</title>
<bib_ref>
<year>March 2000</year>
<title>MOF 1.3</title>
<author>OMG</author>
<weblink>D:\SALIM\Docs\Specifications\MOF1_3.pdf</weblink>
</bib_ref>
<bib_ref>
<year>August 2002</year>
<title>IDLto Java LanguageMapping Specification</title>
<author>OMG</author>
<weblink>D:\SALIM\Docs\Specifications\IDL2Java.pdf</weblink>
</bib_ref>
<bib_ref>
<year>1999</year>
<title>XML Metadata Interchange (XMI) Version 1.1</title>
<author>OMG</author>
<weblink>D:\SALIM\Docs\Specifications\xmi-1.1.pdf</weblink>
</bib_ref>
<bib_ref>
<year>2002</year>
<title>XML Metadata Interchange (XMI) Version 2</title>
<author>"OMG</author>
<weblink>D:\SALIM\Docs\Specifications\XMI2.pdf</weblink>
</bib_ref>
<bib_ref>
<year>2002</year>
<title>XMI Version 1Production of XML Schema Specification</title>
<author>OMG</author>
<weblink>D:\SALIM\Docs\Specifications\XMI1XSD.pdf</weblink>
</bib_ref>
<bib_ref>
<year>2002</year>
<title>EDOC</title>
<author>OMG</author>
<weblink>D:\SALIM\Docs\Specifications\EDOC02-02-05.pdf</weblink>
</bib_ref>
</domain>
<domain>
<title>Theses</title>
<bib_ref>
<year>Octobre 2001</year>
<title>Echanges de Spécifications Hétérogènes et Réparties</title>
<author>Xavier Blanc</author>
<weblink>D:\SALIM\Docs\Theses\TheseXavier.pdf</weblink>
</bib_ref>
<bib_ref>
<year>Janvier 2001</year>
<title>Composition of Object-Oriented Software Design Models</title>
<author>Siobhan Clarke</author>
<weblink>D:\SALIM\Docs\Theses\SClarkeThesis.pdf</weblink>
</bib_ref>
......
......
After, in Java main function, I call the dispContent function which is just there:
public void dispContent (Node n)
{
String domainName = null;
// we are in an element node
if (n instanceof Element) {
Element e = ((Element) n);
// domain title
if (e.getTagName().equals("title") && e.getParentNode().getNodeName().equals("domain")) {
domainName = e.getTextContent();
DomaineTemplate(domainName);
}
else if (e.getTagName().equals("bib_ref")) {
NodeList ref = e.getChildNodes();
for (int i = 0; i < ref.getLength(); i++) {
Node temp = (Node) ref.item(i);
if (temp.getNodeType() == Node.ELEMENT_NODE) {
if (temp.getNodeType() == org.w3c.dom.Node.TEXT_NODE)
continue;
out.println(temp.getNodeName() + " : " + temp.getTextContent() + "\n");
}
}
}
else {
NodeList sub = n.getChildNodes();
for(int i=0; (i < sub.getLength()); i++)
dispContent(sub.item(i));
}
}
/*else if (n instanceof Document) {
NodeList fils = n.getChildNodes();
for(int i=0; (i < fils.getLength()); i++) {
dispContent(fils.item(i));
}
}*/
}
The "domaineTemplate" function just displays its parameter!
My problem happens when I browse the "bib_ref" tag in Java. For each "bib_ref" loop, it displays all the contents of all "bib_ref" tags... in one line! I want to display only one content (year, title, author and weblink tag) per "bib_ref".
Here is what it is displaying at the moment when I browse bib_ref :
Specifications
year : March 2000 title : MOF 1.3 author : OMG weblink : D:\SALIM\Docs\Specifications\MOF1_3.pdf year : August 2002 title : IDLto Java LanguageMapping Specification author : OMG weblink : D:\SALIM\Docs\Specifications\IDL2Java.pdf year : 1999 title : XML Metadata Interchange (XMI) Version 1.1 author : OMG weblink : D:\SALIM\Docs\Specifications\xmi-1.1.pdf year : 2002 title : XML Metadata Interchange (XMI) Version 2 author : OMG weblink : D:\SALIM\Docs\Specifications\XMI2.pdf year : 2002 title : XMI Version 1Production of XML Schema Specification author : OMG weblink : D:\SALIM\Docs\Specifications\XMI1XSD.pdf year : 2002 title : EDOC author : OMG weblink : D:\SALIM\Docs\Specifications\EDOC02-02-05.pdf
Theses
year : Octobre 2001 title : Echanges de Sp�cifications H�t�rog�nes et R�parties author : Xavier Blanc weblink : D:\SALIM\Docs\Theses\TheseXavier.pdf year : Janvier 2001 title : Composition of Object-Oriented Software Design Models author : Siobhan Clarke weblink : D:\SALIM\Docs\Theses\SClarkeThesis.pdf year : Juin 2002 title : Contribution � la repr�sentation de processu par des techniques de m�ta mod�lisation author : Erwan Breton weblink : D:\SALIM\Docs\Theses\ErwanBretonThesis.pdf year : Octobre 2000 title : Technique de Mod�lisation et de M�ta mod�lisation author : Richard Lemesle weblink : D:\SALIM\Docs\Theses\RichardLemesle.pdf year : Juillet 2002 title : Utilsation d'agents mobiles pour la construction des services distribu�s author : Siegfried Rouvrais weblink : D:\SALIM\Docs\Theses\theserouvrais.pdf
...
...
Can you help me ?
I'm just a beginner in xml with java and I'm searching some solution for about 3 hours... Thank's a lot!
I called dispContent(doc.getFirstChild()); where doc is the Document with your given xml file contents.
Assumptions: out.println() is System.out.println() and DomaineTemplate(domainName); adds a newline (based on your output provided)
I get the following print out in the console:
Specifications
year : March 2000
title : MOF 1.3
author : OMG
weblink : D:\SALIM\Docs\Specifications\MOF1_3.pdf
year : August 2002
title : IDLto Java LanguageMapping Specification
author : OMG
weblink : D:\SALIM\Docs\Specifications\IDL2Java.pdf
year : 1999
title : XML Metadata Interchange (XMI) Version 1.1
author : OMG
weblink : D:\SALIM\Docs\Specifications\xmi-1.1.pdf
year : 2002
title : XML Metadata Interchange (XMI) Version 2
author : "OMG
weblink : D:\SALIM\Docs\Specifications\XMI2.pdf
year : 2002
title : XMI Version 1Production of XML Schema Specification
author : OMG
weblink : D:\SALIM\Docs\Specifications\XMI1XSD.pdf
year : 2002
title : EDOC
author : OMG
weblink : D:\SALIM\Docs\Specifications\EDOC02-02-05.pdf
Theses
year : Octobre 2001
title : Echanges de Spécifications Hétérogènes et Réparties
author : Xavier Blanc
weblink : D:\SALIM\Docs\Theses\TheseXavier.pdf
year : Janvier 2001
title : Composition of Object-Oriented Software Design Models
author : Siobhan Clarke
weblink : D:\SALIM\Docs\Theses\SClarkeThesis.pdf
If you're having an issue with "\n" creating a new line, you can try using what the System uses:
public static final String NEW_LINE = System.getProperty("line.separator");
If you don't want the new lines between each line of the "bib_ref" node's children print outs, change:
else if (e.getTagName().equals("bib_ref")) {
NodeList ref = e.getChildNodes();
for (int i = 0; i < ref.getLength(); i++) {
Node temp = (Node) ref.item(i);
if (temp.getNodeType() == Node.ELEMENT_NODE) {
if (temp.getNodeType() == org.w3c.dom.Node.TEXT_NODE)
continue;
out.println(temp.getNodeName() + " : " + temp.getTextContent() + "\n");
}
}
}
to:
else if (e.getTagName().equals("bib_ref")) {
NodeList ref = e.getChildNodes();
for (int i = 0; i < ref.getLength(); i++) {
Node temp = (Node) ref.item(i);
if (temp.getNodeType() == Node.ELEMENT_NODE) {
if (temp.getNodeType() == org.w3c.dom.Node.TEXT_NODE)
continue;
// Removed "\n":
out.println(temp.getNodeName() + " : " + temp.getTextContent());
}
}
// Added out.println();
out.println();
}
Results:
Specifications
year : March 2000
title : MOF 1.3
author : OMG
weblink : D:\SALIM\Docs\Specifications\MOF1_3.pdf
year : August 2002
title : IDLto Java LanguageMapping Specification
author : OMG
weblink : D:\SALIM\Docs\Specifications\IDL2Java.pdf
year : 1999
title : XML Metadata Interchange (XMI) Version 1.1
author : OMG
weblink : D:\SALIM\Docs\Specifications\xmi-1.1.pdf
year : 2002
title : XML Metadata Interchange (XMI) Version 2
author : "OMG
weblink : D:\SALIM\Docs\Specifications\XMI2.pdf
year : 2002
title : XMI Version 1Production of XML Schema Specification
author : OMG
weblink : D:\SALIM\Docs\Specifications\XMI1XSD.pdf
year : 2002
title : EDOC
author : OMG
weblink : D:\SALIM\Docs\Specifications\EDOC02-02-05.pdf
Theses
year : Octobre 2001
title : Echanges de Spécifications Hétérogènes et Réparties
author : Xavier Blanc
weblink : D:\SALIM\Docs\Theses\TheseXavier.pdf
year : Janvier 2001
title : Composition of Object-Oriented Software Design Models
author : Siobhan Clarke
weblink : D:\SALIM\Docs\Theses\SClarkeThesis.pdf
Of course, now I see you have tagged your question with html and your code has nothing to do with html so far. So I asume out.println is some OutputStream for a Servlet and you're trying to Output html.
So the println linebreak, and the "\n" linebreaks are just available in the html sourcecode. The browser will skip this.
Change this line
out.println(temp.getNodeName() + " : " + temp.getTextContent() +
"\n");
to
out.println(temp.getNodeName() + " : " + temp.getTextContent() + "< br
/>");
and a servlet should output just fine with the linebreaks.

Categories

Resources