2016-08-18 128 views
-1

我需要編寫一個簡單的函數來解析xml從Java的URL與DOM。 這是xml url如何從URL讀取XML響應?

如果我把它寫入/assets/exampleXML.xml並使用這段代碼都可以。

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"); 
     } 

但是,如果我改變

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()); 

這是行不通的。什麼是問題以及如何解決它? 我已閱讀它,但它不起作用。請幫幫我。 How to read XML response from a URL in java?

回答

0

我並不喜歡手工解析XML,我通常使用POJO和簡單的XML來爲我完成這項工作。下面是一個使用Volley爲HTTP通信和Simple XML

的POJO

@org.simpleframework.xml.Root(name="ValCurs", strict=false) 
class ValCurs { 

    @Attribute(name="ID") public String id; 
    @Attribute(name="DateRange1") public String dateRange1; 
    @Attribute(name="DateRange2") public String dateRange2; 
    @Attribute public String name; 

    @ElementList(inline = true) 
    public List<Record> records; 

    public ValCurs() { 
     id = ""; 
     name = ""; 
     dateRange1 = ""; 
     dateRange2 = ""; 
     records = new ArrayList<Record>(); 

    } 
} 

@Root(name="Record") 
class Record { 
    @Attribute(name="Date") public String date; 
    @Attribute(name="Id") public String id; 
    @Element(name="Nominal") public String nominal; 
    @Element(name="Value") public String value; 

    public Record() { 
     date = ""; 
     id = ""; 
     nominal = ""; 
     value = ""; 
    } 
} 

的單元測試

@Test 
    public void XMLTest() throws Exception { 

     final CountDownLatch unitTestLatch = new CountDownLatch(1); 
     String url = "http://www.cbr.ru/scripts/XML_dynamic.asp?VAL_NM_RQ=R&date_req1=01.08.2016&date_req2=18.08.2016&rt=1&mode=1[1]"; 

     Context appContext = InstrumentationRegistry.getTargetContext(); 
     RequestQueue queue = Volley.newRequestQueue(appContext); 

     StringRequest stringRequest = new StringRequest(Request.Method.GET, url, 
       new Response.Listener<String>() { 
        @Override 
        public void onResponse(String response) { 

         try { 
          Serializer serializer = new Persister(); 
          ValCurs valcur = serializer.read(ValCurs.class, new StringReader(response), false); 
          Log.d("TAG", "Record size: " + valcur.records.size()); 

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

         unitTestLatch.countDown(); 
        } 
       }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       unitTestLatch.countDown(); 
      } 
     }); 

     queue.add(stringRequest); 

     unitTestLatch.await(); 
    } 
一個例子