B Jha
B Jha

Reputation: 11

Accept xml message in java from server

I am a newbie in using web services so sorry for the simple question. I want to receive a xml message or file from the server in java and then parse it and wanted help.

Upvotes: 0

Views: 77

Answers (1)

Mincong Huang
Mincong Huang

Reputation: 5552

Read content and save as file:

URL url = new URL("https://example.com/abc.xml");
Path xml = Paths.get("/path/to/abc.xml");;
try (InputStream in = url.openStream()) {
  Files.copy(in, xml);
}

Read content and parse as XML:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
URL url = new URL("https://example.com/abc.xml");
try (InputStream in = url.openStream()) {
  Document d = factory.newDocumentBuilder().parse(in);
  ...
}

These solutions use built-in classes provided by Java:

Upvotes: 1

Related Questions