读取XML drom网页返回null

时间:2015-07-13 14:18:52

标签: java xml selenium

我使用以下代码从网页读取XML。我在这里提到了公共URL,因为无法提及项目URL:

`String g1="http://www.w3schools.com/xml/note.xml"; 
    DocumentBuilderFactory dbFactory=DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder=dbFactory.newDocumentBuilder();
    Document doc=dBuilder.parse(g1);`

但我收到doc的值为null。

2 个答案:

答案 0 :(得分:1)

做类似的事情: -

    String urlString = "http://www.w3schools.com/xml/note.xml";
    URL url = new URL(urlString);
     DocumentBuilderFactory dbFactory=DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder=dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(url.openStream());
    NodeList descNodes = doc.getElementsByTagName("note");

     for(int i=0; i<descNodes.getLength();i++)
     {
         System.out.println(descNodes.item(i).getTextContent());
     }

输出: -

    Tove
    Jani
    Reminder
    Don't forget me this weekend!

答案 1 :(得分:0)

请参阅下面的链接,希望它可以帮助你......看到1.2中的部分!!

http://viralpatel.net/blogs/java-xml-xpath-tutorial-parse-xml/