DTN ProphetX® Web Services

Java

Here is a small example of making a GetQuoteSnap request for a couple symbols and pulling the symbol and last price from the result.

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import java.io.StringReader;


public class GetQuoteSnapExample {
		
	public static void main(String[] args) {
		try {
			// Request quotes for the front two corn contracts
			String[] symbols = { "@C@1", "@C@2" };
			String symbolString = String.join(",", symbols);
			// Enter userid and password
			String userid = "";
			String password = "";
			String urlString = String.format("https://pxweb.dtn.com/PXWebSvc/PXServiceWeb.svc/GetQuoteSnap?UserID=%s&Password=%s&Type=P&Symbol=%s&SymbolLimit=&PageToken=&Market=&Vendor=&Format=", userid, password, symbolString);
			System.out.println(urlString);
			URL url = new URL(urlString);

			// Open a connection
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();

			// Set the request method to GET
			connection.setRequestMethod("GET");

			// Get the response code
			int responseCode = connection.getResponseCode();
			System.out.println("Response Code: " + responseCode);

			// Read the response
			BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			String inputLine;
			StringBuilder response = new StringBuilder();

			while ((inputLine = in.readLine()) != null) {
				response.append(inputLine);
			}
			in.close();

			// Parse the XML response
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document doc = builder.parse(new InputSource(new StringReader(response.toString())));
			
			// Example: Print all "Quote" elements in the XML
			NodeList items = doc.getElementsByTagName("Quote");
			for (int i = 0; i < items.getLength(); i++) {
				Element quote = (Element) items.item(i);
				String symbol = quote.getAttribute("TickerSymbol");
				NodeList lastElements = quote.getElementsByTagName("Last");
				Element lastElement = (Element) lastElements.item(0);
				String lastString = lastElement.getTextContent();
				System.out.println("Quote  " + (i + 1) + ": " + symbol + " " + lastString);
			}

			// Close the connection
			connection.disconnect();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}