簡體   English   中英

XML響應中來自DocumentBuilderFactory的“ document:null”?

[英]“document: null” from DocumentBuilderFactory on an XML response?

我正在創建jUnit測試,以檢查為客戶端應用程序獲取XML的代理Servlet是否返回了它們而沒有損壞或任何更改。

在我的jUnit類中,我有一個doGet方法,該方法執行對代理Servlet的HTTP get請求,並向其傳遞一個URL,該URL最后帶有GET參數。

應將XML響應接收回給客戶端(jUnit)。

protected String doGet() throws IOException, ParserConfigurationException, SAXException
{
    String requestURL = url + params;
    System.out.println(requestURL);
    // fetch XML from URL
        System.out.println(requestURL);

        URL url = new URL(requestURL);
        HttpURLConnection connection =
            (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setRequestProperty("Accept", "application/xml");

        InputStream xml = connection.getInputStream();
        System.out.println(connection.getResponseMessage());
        System.out.println(connection.getResponseCode());

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();

        org.w3c.dom.Document text = db.parse(xml);
        System.out.print(text.getNodeValue());
        return text.toString();
}

這是我的輸出:

200

空值

[#document:null]

我正在嘗試獲取作為XML文檔的HTTP響應的字符串值,因此我可以將其與另一個字符串進行比較以確保它是正確的。

如何獲得此字符串值?

謝謝

使用Apache.IO.Utils Common Lib中的這段優雅的代碼解決了該問題

org.apache.commons.io.IOUtils.toString(new URL(requestURL));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM