Java Reference
In-Depth Information
of grammar to use, in our case, XML Schema C , and load the XSD schema file in a
Schema instance D . Finally, we can create an XML Validator for our schema and vali-
date the DOM document E . At this point, we know that our document is valid and
well formed.
The next step would be to check that the application data is as expected. The DOM
document API can be painful to use, so at this point you have several options. The
JDOM 19 API is a friendlier interface to XML than DOM . You can use Java's XP ath 20 sup-
port to check the contents of a document. You can also use Sun's JAXB 21 framework,
although it's not trivial, to transform XML into POJO s.
Next, let's consider JSON as the data format.
13.7.3
Validating a JSON response
JSON 22 (JavaScript Object Notation) is a data-interchange language based on a subset
of the JavaScript Programming Language, Standard ECMA -262 3rd Edition, December
1999. 23 Applications use JSON instead of XML as their data format because it's text
based and easy for people and machines to read and understand. In this first example
in listing 13.12, we show a simple check of a JSON document.
Listing 13.12
A JSON service test
private static final String URL_FIXTURE =
" http://localhost/ch13personal/glossary.json";
@Test
public void testGetJsonBasicCheck() throws IOException {
HttpClient httpClient = new HttpClient();
GetMethod get = new GetMethod(URL_FIXTURE);
String responseString;
try {
httpClient.executeMethod(get);
InputStream input = get.getResponseBodyAsStream();
responseString = IOUtils.toString(input, "UTF-8");
} finally {
get.releaseConnection();
}
String responseNoWs =
StringUtils.deleteWhitespace(responseString);
String response1Line = "{ \"glossary\": { \"title\":
B
\"example glossary\", \"GlossDiv\": { \"title\": \"S\",
\"GlossList\": { \"GlossEntry\": { \"ID\": \"SGML\", \"SortAs\":
\"SGML\", \"GlossTerm\": \"Standard Generalized Markup Language\",
\"Acronym\": \"SGML\", \"Abbrev\": \"ISO 8879:1986\", \"GlossDef\": {
\"para\": \"A meta-markup language, used to create markup languages
19
http://www.jdom.org/
20
http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/xpath/package-summary.html
21
https://jaxb.dev.java.net/
22
http://www.json.org/
23
http://www.ecma-international.org/publications/files/ecma-st/ECMA-262.pdf
 
 
 
 
 
Search WWH ::




Custom Search