• About
Triona Weblog

Software Development and much more

Process XML files in web applications

26.09.2010 by Holger Klatt

This article answers a common question: “How to read a XML file in a web application?”

I was once in the situation, where I had to find a solution for this problem. So, I would like to share my solution with you.

To demonstrate the solution, I have attached an Eclipse project.  I have used Eclipse Galileo under Windows Vista.

This project is a servlet, which reads a XML file, processes the content and finally prints out the content.

The following steps demonstrates the solution.

1 .The XML – File (excerpt)

<person>
<firstName>Anna</firstName>
<lastName>Sazi</lastName>
</person>

2 . Read in the XML File:

InputStream inputStream = ctx.getResourceAsStream("/WEB-INF/" + XML_FILE_NAME);

Explanation: By this method you obtain an InputStream – Object. It turned out that the file must be located below the WEB-INF folder of the web application. Any other subfolder is also possible.

3. Get a Document – Object

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(inputStream);

Explanation: The aim of these three commands is to get an representation of the XML file which has been read in  during the step 1.

4. Get the values from the node

NodeList firstNameList = element.getElementsByTagName("firstName");
Element firstNameElement = (Element) firstNameList.item(0);
NodeList textFirstNameList = firstNameElement.getChildNodes();
person.setFirstName(((Node) textFirstNameList.item(0)).getNodeValue().trim());

Explanation: This code gets the value of the node whose name is “firstName”. It also adds the obtained value to an “Person” – Object.

5. Print out the values

for(Person person : personList)
{
out.println("<tr>");
out.println("<td>" + person.getFirstName() + "</td>");
out.println("<td>" + person.getLastName() + "</td>");
out.println("</tr>");
}

Explanation: This code prints out the values from the Person list.

For the full code, feel free to download the attached project.


To invoke the servlet, you need to enter “<domain>/XmlReadingExample” into the address bar of your browser.

Posted in: Development, Java Tagged: file, process, read, web application, XML
October 2023
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
3031  
« Nov    

Tags

API Architecture CDI Collections Comparable Comparator Database EA Eclipse EJB Enterprise Architect Excel Hessian HTML Iteration Java Java 8 JavaEE Java EE Java Enterprise Development javascript Javascript Canvas HTML5 JEE JEE 6 JPA jQuery JSF linux Makro Map MariaDB Maven Oracle Plugins Relation relationship Richfaces Service Service Facade Set SOA Subversion Tutorial VBA XML

Recent Posts

  • Domain Driven Design und Event Storming
  • NESTJS BEYOND „HELLO WORLD“
  • Jakarta EE 9 – An upheaval with difficulties
  • Jakarta EE 9 – Ein Umbruch mit Schwierigkeiten
  • Erste Schritte mit Hibernate Spatial

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Copyright © 2023 Triona Weblog.

Impressum | Datenschutzerklärung