0

I am using following code to read XML from webpage. I have mentioned public URL here as cant mention project URL:

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

but I am receiving value of doc as null.

Nipun Kumar
  • 143
  • 1
  • 6
  • 16
  • 1
    possible duplicate of [How to read XML response from a URL in java?](http://stackoverflow.com/questions/2310139/how-to-read-xml-response-from-a-url-in-java) – Arpit Aggarwal Jul 13 '15 at 14:38
  • you trying to parse string with url, not the document..... – Puh Jul 13 '15 at 15:22

2 Answers2

1

Do something like :-

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

Output:-

    Tove
    Jani
    Reminder
    Don't forget me this weekend!
AnkeyNigam
  • 2,810
  • 4
  • 15
  • 23
0

Refer below link hope it helps you..see the part for 1.2 in it !!

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