This is the first time I try to parse XML
so be gentle with me :).
So i downloaded the source code of a web page i want to parse certain information from http://www.songlyrics.com/eminem/my-name-is-lyrics/.
Now i copy pasted the XML
file to the notepad.
I saved the file as XML - 1
.
My code looks like this:
public class Program
{
public static void main(String[] args)
{
System.out.println("Program starts:");
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
try
{
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(new File("C:/Users/volca_000/Desktop/XML - 1.txt"));
NodeList paragraphsNodeList = document.getElementsByTagName("p");
for (int i = 0;i < paragraphsNodeList.getLength();i++)
{
Node paragraphNode = paragraphsNodeList.item(i);
if (paragraphNode.getNodeType() == Node.TEXT_NODE)
{
Element element = (Element)paragraphNode;
String node = element.getTextContent();
System.out.println(node);
}
}
}
catch (ParserConfigurationException e)
{
e.printStackTrace();
}
catch (SAXException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
} // End of main
} // End of Program class
Even if i discard the if
statement get nothing on the console.
if (paragraphNode.getNodeType() == Node.TEXT_NODE)
What am i doing wrong? Any suggestions would be very appreciated.