Read data from an xml file and process it with Java.
XML is a data format very flexible and widely used. With Java is very simple to read data from an xml source and process them or print them.
Suppose you have a file like this:
Code:
<?xml version="1.0" ?>
<?xml-stylesheet type="text/xsl" href="books.xsl" ?>
<books>
<book>
<title>Programming in XML</title>
<author>Marc Vince</author>
<pages>348</pages>
<category>Programming</category>
</book>
<book>
<title>Programming in C#</title>
<author>Loren Gallipyos</author>
<pages>234</pages>
<category>Programming</category>
</book>
<book>
<title>Android</title>
<author>Norman Bert</author>
<pages>419</pages>
<category>Operating Systems</category>
</book>
<book>
<title>Linux</title>
<author>Hurth Francy</author>
<pages>294</pages>
<category>Operating Systems</category>
</book>
<book>
<title>WordPress</title>
<author>Milena Zarch</author>
<pages>364</pages>
<category>CMS</category>
</book>
<book>
<title>Joomla!</title>
<author>Denise Patrics</author>
<pages>163</pages>
<category>CMS</category>
</book>
<book>
<title>jQuery</title>
<author>Teresa Curtis</author>
<pages>349</pages>
<category>Programming</category>
</book>
</books>
The example shows how to read the file and print the data on screen:
Code:
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;
public class ReadXMLFile {
public static void main(String argv[]) {
try {
File xmlFile = new File("books.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(xmlFile);
doc.getDocumentElement().normalize();
System.out.println("List of the books" );
NodeList nl = doc.getElementsByTagName("book");
System.out.println("------------------------------------");
for (int n = 0; n < nl.getLength(); n++) {
Node node = nl.item(n);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element e = (Element) node;
System.out.println();
System.out.println("Title : " + getTagValue("title",e));
System.out.println("Author : " + getTagValue("author",e));
System.out.println("Pages : " + getTagValue("pages",e));
System.out.println("Category : " + getTagValue("category",e));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static String getTagValue(String sTag, Element e)
{
NodeList nlList= e.getElementsByTagName(sTag).item(0).getChildNodes();
Node nValue = (Node) nlList.item(0);
return nValue.getNodeValue();
}
}
Result:
>java -cp . ReadXMLFile
List of the books
------------------------------------
Title : Programming in XML
Author : Marc Vince
Pages : 348
Category : Programming
Title : Programming in C#
Author : Loren Gallipyos
Pages : 234
Category : Programming
Title : Android
Author : Norman Bert
Pages : 419
Category : Operating Systems
Title : Linux
Author : Hurth Francy
Pages : 294
Category : Operating Systems
Title : WordPress
Author : Milena Zarch
Pages : 364
Category : CMS
Title : Joomla!
Author : Denise Patrics
Pages : 163
Category : CMS
Title : jQuery
Author : Teresa Curtis
Pages : 349
Category : Programming
>Exit code: 0