Java for Testers – Read and Print XML file

Purpose

In my former post I provided the java code to search for xml-files in our logs- directory and write these filenames including  their pathname to a text file named “FilteredLogFileList.txt” in the project directory. I also described how to create the logs-folder and fill it with files.

I use this setup as my starting point for the next step. Here we take a single well formatted xml-file and present it’s content in the NetBeans console. This is easier than you think. Java knows a lot of libraries to perform specific tasks and reading xml-files is one of them. If you used dummyXMLs you can use dummy2.xml in the rest of this post.

Preparation

Point of departure is you have a logs-directory filled with some xml-files in your NetBeans HelloWorld project folder.

Run the code

Next download ReadAndPrintXMLFile.java and unpack it in your HelloWorld project src-directory.

The java file will pop up immediately in your NetBeans src folder if it’s opened. Still, after the copying the first thing you have to do is rebuild your project otherwise running ReadAndPrintXMLFile.java may fail as it can’t be found though visible for your eyes. Now you can open and run ReadAndPrintXML.java which shows you the XML-content of dummy2.xml in the NetBeans console.

Now we can read and print solely one hard coded xml-file. But we want more. In my former post the code produced a text file containing a list of all our filtered (xml-)files including their pathnames. In the following post I show you how read and print all the xml-files that are listed in our text file.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *