0% found this document useful (0 votes)
47 views4 pages

WP Experiment 7 and 8: EXP 7: Read An XML File Using DOM Parser API - Java Program

The document describes two experiments using XML files. Experiment 7 uses DOM parsing in Java to read an XML file containing employee data and print the information. Experiment 8 transforms the same XML file into HTML using XSL transformation, where the XSL file contains the code to map XML elements to an HTML table with the employee data.

Uploaded by

SALAZAR sh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views4 pages

WP Experiment 7 and 8: EXP 7: Read An XML File Using DOM Parser API - Java Program

The document describes two experiments using XML files. Experiment 7 uses DOM parsing in Java to read an XML file containing employee data and print the information. Experiment 8 transforms the same XML file into HTML using XSL transformation, where the XSL file contains the code to map XML elements to an HTML table with the employee data.

Uploaded by

SALAZAR sh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

WP EXPERIMENT 7 AND 8

Snehit sharma
RA1711003040027
CSE B

EXP 7 : Read an XML file using DOM Parser API – java Program

XML CODE:

<?xml version="1.0" encoding="UTF-8"?>


<company>
<employee id="111">
<firstName>Lokesh</firstName>
<lastName>Gupta</lastName>
<location>India</location>
</employee>
<employee id="222">
<firstName>Alex</firstName>
<lastName>Gussin</lastName>
<location>Russia</location>
</employee>
<employee id="333">
<firstName>David</firstName>
<lastName>Feezor</lastName>
<location>USA</location>
</employee>
</company>

JAVA CODE:

package DOMreadpckg;
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.*;
public class DOMread {
public static void main(String[] args) {
File file= new File("employees.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dbBuilder = null;
Document doc=null;
try
{
dbBuilder =dbFactory.newDocumentBuilder();
doc=dbBuilder.parse(file);
doc.getDocumentElement().normalize();
System.out.println(""+doc.getDocumentElement().getNodeName());
NodeList nlist =doc.getElementsByTagName("employee");
for(int i=0;i<nlist.getLength();i++)
{
Node node =nlist.item(i);
System.out.println("Current element :
"+node.getNodeName());
if(node.getNodeType()==Node.ELEMENT_NODE)
{
Element element =(Element)node;
System.out.println("Employee ID:
"+element.getAttribute("id"));
System.out.println("FirstName:
"+element.getElementsByTagName("firstName").item(0).getTextContent());
System.out.println("LastName:
"+element.getElementsByTagName("lastName").item(0).getTextContent());
System.out.println("Location:
"+element.getElementsByTagName("location").item(0).getTextContent());
}
}
}
catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
EXP 8: Transform an xml file into HTML using XSL
transformation

XML CODE:

<?xml version="1.0" encoding="UTF-8"?>


<?xml-stylesheet type="text/xsl" href="Rule.xsl"?>
<company>
<employee>
<firstName>Lokesh</firstName>
<lastName>Gupta</lastName>
<location>India</location>
</employee>
<employee>
<firstName>Alex</firstName>
<lastName>Gussin</lastName>
<location>Russia</location>
</employee>
<employee>
<firstName>David</firstName>
<lastName>Feezor</lastName>
<location>USA</location>
</employee>
</company>

XSL CODE:

<?xml version="1.0" encoding="UTF-8"?>


<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h1 align="center">Employee Details</h1>
<table border="3" align="center">
<tr>
<th>Firstname</th>
<th>Secondname</th>
<th>Location</th>
</tr>
<xsl:for-each select="company/employee">
<tr>
<td><xsl:value-of select="firstName"/></td>
<td><xsl:value-of select="secondName"/></td> <td><xsl:value-of
select="location"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

You might also like