I need to write a simple function to parsing xml from Url on java with DOM. This is xml url.
If i write this into /assets/exampleXML.xml and use this code all works.
static final String NODE_EMP = "Record";
static final String NODE_NAME = "Nominal";
static final String NODE_SALARY = "Value";
.....
public void onBtnClick3(View v) {
XMLDOMParser parser = new XMLDOMParser();
try {
InputStream is = getAssets().open("exampleXML2.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(is);
NodeList nodeList = doc.getElementsByTagName(NODE_EMP);
for (int i = 0; i < nodeList.getLength(); i++) {
Element e = (Element) nodeList.item(i);
nameText.append(parser.getValue(e, NODE_NAME) + "\n");
nameText.append(parser.getValue(e, NODE_SALARY) + "\n");
}
But if i change to
public static final String QUERY_URL = "http://www.cbr.ru/scripts/XML_dynamic.asp?......";
public void onBtnClick(View v) {
XMLDOMParser parser = new XMLDOMParser();
try {
URL url = new URL(QUERY_URL);
URLConnection conn = url.openConnection();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(conn.getInputStream());
This does not work. What is the problem and how to solve it? I have read it, but it does not work. Help me please. How to read XML response from a URL in java?