Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java htmlUnit获取xml页面ClassCastException问题_Java_Xml_Htmlunit_Classcastexception - Fatal编程技术网

Java htmlUnit获取xml页面ClassCastException问题

Java htmlUnit获取xml页面ClassCastException问题,java,xml,htmlunit,classcastexception,Java,Xml,Htmlunit,Classcastexception,我有一个使用HtmlUnit库的java应用程序,在从xml页面获取源代码时遇到了问题。我得到的只是一个“ClassCastException”错误 我正在使用最新的HtmlUnit 2.15 API库 public static void main(String[] args) { WebClient webClient = new WebClient(BrowserVersion.CHROME); webClient = new WebClient(BrowserVersi

我有一个使用HtmlUnit库的java应用程序,在从xml页面获取源代码时遇到了问题。我得到的只是一个“ClassCastException”错误

我正在使用最新的HtmlUnit 2.15 API库

 public static void main(String[] args) {
    WebClient webClient = new WebClient(BrowserVersion.CHROME);
    webClient = new WebClient(BrowserVersion.CHROME);
    webClient.getOptions().setJavaScriptEnabled(true);
    webClient.getOptions().setActiveXNative(false);
    webClient.getOptions().setAppletEnabled(false);
    webClient.getOptions().setCssEnabled(true);
    webClient.getOptions().setPopupBlockerEnabled(true);
    webClient.getOptions().setPrintContentOnFailingStatusCode(false);
    webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
    webClient.getOptions().setThrowExceptionOnScriptError(true);
    webClient.getOptions().setTimeout(100000);
    webClient.getOptions().setThrowExceptionOnScriptError(false);
    webClient.getOptions().setDoNotTrackEnabled(false);
    //webClient.getOptions().setProxyConfig(new ProxyConfig("10.0.0.1", 8080));
    try{    
        HtmlPage page = webClient.getPage("http://www.w3schools.com/xml/note.xml");
        // TODO, add your application code
        System.out.println(page.asXml());

    } catch (Exception e) {
        System.out.println("An error occurs when getting the page: "+e);
    }
}

关于如何通过htmlunit库检索xml文件并将其保存到文件中,您有什么想法吗?

我最终找到了答案。 我们认为您首先需要导入com.gargoylesoftware.htmlunit.xml.XmlPage

然后可以声明XmlPage对象,从而避免ClassCastException问题

所以我的代码对于那些对HtmlUnit也不熟悉的人来说是这样的

public static void main(String[] args) {

    WebClient webClient = new WebClient(BrowserVersion.CHROME);

    XmlPage page = null;

    webClient = new WebClient(BrowserVersion.CHROME);
    webClient.getOptions().setJavaScriptEnabled(true);
    webClient.getOptions().setActiveXNative(false);
    webClient.getOptions().setAppletEnabled(false);
    webClient.getOptions().setCssEnabled(true);
    webClient.getOptions().setPopupBlockerEnabled(true);
    webClient.getOptions().setPrintContentOnFailingStatusCode(false);
    webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
    webClient.getOptions().setThrowExceptionOnScriptError(true);
    webClient.getOptions().setTimeout(100000);
    webClient.getOptions().setThrowExceptionOnScriptError(false);
    webClient.getOptions().setDoNotTrackEnabled(false);
    //webClient.getOptions().setProxyConfig(new ProxyConfig("10.0.0.1", 8080));

    try {

        //go to url
        page = webClient.getPage("http://www.w3schools.com/xml/note.xml");
        System.out.println(xpage.asXml());

        //just for test save to file to see results
        File file = new File("result.html");
        FileOutputStream fos = new FileOutputStream(file);

        fos.write(xpage.asXml().getBytes());
        fos.close();
        //end test

    } catch (Exception e) {
        System.out.println("An error occurs when getting the page: "+e);

    }

}