import org.w3c.dom.*;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
public class Main {
public static void main(String[] args){
try {
String pre_apiURL = "https://www.cbr-xml-daily.ru/daily_utf8.xml";
System.out.println("url " + pre_apiURL);
URL url = new URL(pre_apiURL);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(url.openStream());
NodeList list = doc.getElementsByTagName("Valute");
Element element = doc.getDocumentElement();
for (int i = 0; i < list.getLength(); i++) {
Node node = list.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element el = (Element) node;
String name1 = el.getAttribute("Name");
NodeList nodelist = el.getChildNodes();
for (int j = 0; j < nodelist.getLength(); j++) {
Node n = nodelist.item(j);
if (n.getNodeType() == Node.ELEMENT_NODE) {
Element name = (Element) n;
if (name.getTagName().equals("Name")) {
System.out.println(name.getTextContent());
}
if (name.getTagName().equals("Value")) {
System.out.println(name.getTextContent());
}
}
}
}
}
}catch(Exception e){
}
}
}
Попробовала переделать под андроид и ничего не выводит объясните в чем ошибка и как исправить?
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ListView name;
private ListView value;
public String Name[];
public String Value[];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lvMain = (ListView) findViewById(R.id.name);
try {
String pre_apiURL = "https://www.cbr-xml-daily.ru/daily_utf8.xml";
System.out.println("url " + pre_apiURL);
URL url = new URL(pre_apiURL);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(url.openStream());
NodeList list = doc.getElementsByTagName("Valute");
Element element = doc.getDocumentElement();
for (int i = 0; i < list.getLength(); i++) {
Node node = list.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element el = (Element) node;
String name1 = el.getAttribute("Name");
NodeList nodelist = el.getChildNodes();
for (int j = 0; j < nodelist.getLength(); j++) {
Node n = nodelist.item(j);
if (n.getNodeType() == Node.ELEMENT_NODE) {
Element name = (Element) n;
if (name.getTagName().equals("Name")) {
Name[j]=name.getTextContent()+".";
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, Name);
lvMain.setAdapter(adapter);
}
if (name.getTagName().equals("Value")) {
Value[j]=name.getTextContent();
}
}
}
}
}
}catch(Exception e){
}
}
}