How To fetch data from XML nodes using Java

Today I came across a very common problem. The data which I need to validate was coming from a web page. The webpage consists of a very large and complex XML. And we need to validate data from that XML. So the problem is how to take the XML from a webpage and extract nodes from it. Below points will describe what I did in the program.

  • I took the page source and stored it in a .txt file.
  • Assign all the data available in .txt file to a String variable.
  • Create a DOM parser
  • Evaluate an XML query on xpath object.
  • Print the node we received by xpath query.