How do I print request and response from static xml file - java

i am having text file called "ecareframework.xml". It contains two request and response. From this file, i have to print request and response separately using string.
public class RequestTime {
public static void main(String[] args) throws IOException {
File xmlFile=new File("C:\\Users\\gsanaulla\\Documents\\My Received Files\\ecarewsframework.xml");
Reader fileReader = new FileReader(xmlFile);
BufferedReader bufReader = new BufferedReader(fileReader);
StringBuffer myStringBuffer = new StringBuffer();
String line;
while((line = bufReader.readLine()) != null) {
if (String.valueOf(line).contains("</S:Envelope>")) {
myStringBuffer.append(String.valueOf(line));
}
//fileReader.close();
System.out.println(myStringBuffer.toString());
}
bufReader.close();
}
}

Related

Trying to validate a json against Json Schema in java but getting java.lang.NoSuchMethodError

I want to validate a JSON object with JSON schema.
This is my method:
public static void jsonSchemValidator(String json, String defination) throws ValidationException, JSONException, IOException )
{
JSONObject jsonSchemaori = new JSONObject(new JSONTokener(readFromResources(defination)));
JSONObject jsonSubject = new JSONObject(new JSONTokener(readFromResources(json)));
Schema schema = SchemaLoader.load(jsonSchemaori);
schema.validate(jsonSubject);
}
String defination="src/test/resources/json/pick_event_schema.json";
String json="src/test/resources/json/pickevent1.json";
Utils.jsonSchemValidator(json,schema);
I generated the Schema file from the following site: https://www.liquid-technologies.com/online-json-to-schema-converter
This is my readFromResources:
public static String readFromResources(String fileName) throws IOException {
File file = new File(fileName);
String filePath = file.getAbsolutePath();
StringBuilder sb = new StringBuilder();
FileReader fr = new FileReader(filePath);
try( BufferedReader br = new BufferedReader(fr) ) {
String contentLine = br.readLine();
while (contentLine != null) {
sb.append(contentLine);
contentLine = br.readLine();
}
} catch (IOException e) {
throw e;
}
return sb.toString();
}
I am getting an exception:
java.lang.NoSuchMethodError: org.json.JSONObject.getNames(Lorg/json/JSONObject;)[Ljava/lang/String;
Please let me where I am going wrong. Thank you.

cannot print webpage contents to a file in my local system using java

I cannot print contents of a webpage to a file in my local system.please help me to solve this problem
import java.net.*;
import java.io.*;
public class bpart
{
public static void main(String[] args) throws Exception {
URL oracle = new URL("http://www.google.com/");
BufferedReader in = new BufferedReader(new InputStreamReader(oracle.openStream()));
OutputStream os = new FileOutputStream("my file location");
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}
I would suggest using a try-with-resources structure for you I/O and take a look a java-naming conventions. If you want to write the output of your http-call to a file you can do the following:
public static void main(String[] args) throws Exception {
URL googleUrl = new URL("http://www.google.com/");
try (BufferedReader in = new BufferedReader(new
InputStreamReader(googleUrl.openStream()));
PrintWriter out = new PrintWriter(new FileWriter("path/to/desired/file"))) {
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
out.println(inputLine);
}
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}

How to read local php file from java gui program (not from web)?

this is my php code checker code for java application but i want to scan a new file in my java GUI program...
public class test {
public static void main(String[] args) throws MalformedURLException, IOException {
URL u = new URL("http://www.example.com/my/php/doc.php");
URLConnection c = u.openConnection();
InputStream r = c.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(r));
for (String line;
(
line = reader.
`enter code here`
readLine()) != null;
)
System.out.println(line);
This code opens file from the web but I want to open file from the desktop
You can use classes available in the java.io package to read from files. For e.g like this:
public class TextFileReadingExample {
public static void main(String[] args) {
try {
FileReader reader = new FileReader("C:\\Users\\MyUsername\\Desktop\\MyFile.txt");
BufferedReader bufferedReader = new BufferedReader(reader);
String line;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Try to use: new File(your_path).toURI().toURL();
i.e.
public class Main {
public static void main(String[] args) throws MalformedURLException, IOException {
URL u = new File("E:\\test_data_014.lst").toURI().toURL();
URLConnection c = u.openConnection();
InputStream r = c.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(r));
BufferedReader bufferedReader = new BufferedReader(reader);
String line;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
reader.close();
}
}

How to parse a txt web service

I'm trying to obtain a result from a web service in a java program. I've done xml services before but this one is text based and i can't figure out how to record the response.
Here is the webService: http://ws.geonames.org/countryCode?lat=47.03&lng=10.2
Thanks!
If it's only text, and you doesn't use any standard format (like SOAP), you need to use Sockets:
URL myURL = new URL("http://ws.geonames.org/countryCode?lat=47.03&lng=10.2");
URLConnection serviceConnection = myURL.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
serviceConnection.getInputStream()));
List<String> response =new ArrayList<String>();
Use this if you had many lines:
while ((inputLine = in.readLine()) != null)
response.add(inputLine);
Or use this if you had ONLY ONE line (like the Web Service in your question):
String countryCode = in.readLine();
And finish with:
serviceConnection.close();
In my case, countryCode it was "AT"
An alternative implementation in addition to URL is to use a HttpClient.
public class CountryCodeReader {
private String readAll(Reader rd) throws IOException {
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
}
return sb.toString();
}
public String readFromUrl(String url) throws IOException, JSONException {
InputStream is = new URL(url).openStream();
try {
InputStreamReader is = new InputStreamReader(is, Charset.forName("UTF-8"))
BufferedReader rd = new BufferedReader(is);
return readAll(rd);
} finally {
is.close();
}
return null;
}
public static void main(String[] argv) {
CountryCodeReader ccr = new CountryCodeReader();
String cc = ccr.readFromUrl("http://ws.geonames.org/countryCode?lat=47.03&lng=10.2");
}
}

Reading from a URL Connection Java

I'm trying to read html code from a URL Connection. In one case the html file I'm trying to read includes 5 line breaks before the actual doc type declaration. In this case the input reader throws an exception for EOF.
URL pageUrl =
new URL(
"http://www.nytimes.com/2011/03/15/sports/basketball/15nbaround.html"
);
URLConnection getConn = pageUrl.openConnection();
getConn.connect();
DataInputStream dis = new DataInputStream(getConn.getInputStream());
//some read method here
Has anyone ran into a problem like this?
URL pageUrl = new URL("http://www.nytimes.com/2011/03/15/sports/basketball/15nbaround.html");
URLConnection getConn = pageUrl.openConnection();
getConn.connect();
DataInputStream dis = new DataInputStream(getConn.getInputStream());
String urlData = "";
while ((urlData = dis.readUTF()) != null)
System.out.println(urlData);
//exception thrown
java.io.EOFException
at java.io.DataInputStream.readUnsignedShort(DataInputStream.java:323)
at java.io.DataInputStream.readUTF(DataInputStream.java:572)
at java.io.DataInputStream.readUTF(DataInputStream.java:547)
in the case of bufferedreader, it just responds null and doesn't continue
pageUrl = new URL("http://www.nytimes.com/2011/03/15/sports/basketball/15nbaround.html");
URLConnection getConn = pageUrl.openConnection();
getConn.connect();
BufferedReader br = new BufferedReader(new InputStreamReader(getConn.getInputStream()));
String urlData = "";
while(true)
urlData = br.readLine();
System.out.println(urlData);
outputs null
You're using DataInputStream to read data that wasn't encoded using DataOutputStream. Examine the documented behavior for your call to DataInputStream#readUtf(); it first reads two bytes to form a 16-bit integer, indicating the number of bytes that follow comprising the UTF-encoded string. The data you're reading from the HTTP server is not encoded in this format.
Instead, the HTTP server is sending headers encoded in ASCII, per RFC 2616 sections 6.1 and 2.2. You need to read the headers as text, and then determine how the message body (the "entity") is encoded.
This works fine:
package url;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
/**
* UrlReader
* #author Michael
* #since 3/20/11
*/
public class UrlReader
{
public static void main(String[] args)
{
UrlReader urlReader = new UrlReader();
for (String url : args)
{
try
{
String contents = urlReader.readContents(url);
System.out.printf("url: %s contents: %s\n", url, contents);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
public String readContents(String address) throws IOException
{
StringBuilder contents = new StringBuilder(2048);
BufferedReader br = null;
try
{
URL url = new URL(address);
br = new BufferedReader(new InputStreamReader(url.openStream()));
String line = "";
while (line != null)
{
line = br.readLine();
contents.append(line);
}
}
finally
{
close(br);
}
return contents.toString();
}
private static void close(Reader br)
{
try
{
if (br != null)
{
br.close();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
This:
public class Main {
public static void main(String[] args)
throws MalformedURLException, IOException
{
URL pageUrl = new URL("http://www.google.com");
URLConnection getConn = pageUrl.openConnection();
getConn.connect();
BufferedReader dis = new BufferedReader(
new InputStreamReader(
getConn.getInputStream()));
String myString;
while ((myString = dis.readLine()) != null)
{
System.out.println(myString);
}
}
}
Works perfectly. The URL you are supplying, however, returns nothing.

Categories