Showing posts with label xml. Show all posts
Showing posts with label xml. Show all posts

Read XML Taglib and JSTL in JSP


samle.xml

 <?xml version="1.0" encoding="ISO-8859-1"?>
 <weather ver="2.0">
   <loc id="INXX0096">
     <dnam>New Delhi, India </dnam>
     <tm>3:30 PM </tm>
   </loc>
   <cc>
     <tmp>75 </tmp>
     <bar>
       <r>29.94 </r>
       <d>steady </d>
     </bar>
   </cc>
 </weather>

--------------------------------
xml_read.jsp

<%@ page contentType="text/html; charset=iso-8859-1" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
<c:import url="http://localhost:8080/XML/samle.xml" var="xml"/>
<x:parse xml="${xml}" varDom="dom"/>
<html>
<head>
<title>Read XML through taglib JSTL</title>
</head>

<body>
      <x:forEach select="$dom/weather/cc/tmp" var="tmp">
        <x:out select="$tmp" />
    </x:forEach>

    <br>

    <x:forEach select="$dom/weather/cc/bar/d" var="d">
        <x:out select="$d" />
    </x:forEach>
</body>
</html>
READ MORE - Read XML Taglib and JSTL in JSP

How to read XML file in java



books.xml

<?xml version="1.0" encoding="iso-8859-1"?>
<library>
 <book>
   <name>Head First Java, 2nd Edition</name>
   <author>Kathy Sierra and Bert Bates</author>
   <publication-date>09-Feb-2005</publication-date>
 </book>
 <book>
   <name>Effective Java</name>
   <author>Joshua Bloch</author>
   <publication-date>28-May-2008</publication-date>
 </book>
 <book>
   <name>Java How to Program, 7th Edition</name>
   <author>Harvey M. Deitel and Paul J. Deitel</author>
   <publication-date>6-Jan-2007</publication-date>
 </book>
</library>

------------------------------------------------------------
readDOMXML.jsp

<%@ page language="java" %>
<%@ page import="org.w3c.dom.*" %>
<%@ page import="javax.xml.parsers.DocumentBuilder" %>
<%@ page import="javax.xml.parsers.DocumentBuilderFactory" %>
<%
    DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
    DocumentBuilder db =dbf.newDocumentBuilder();
    Document doc=db.parse("c:\\books.xml");
   
   
    NodeList nl = doc.getElementsByTagName("book");
   
%>
<html>
<head>
<title>How to read XML file in JAVA</title>
</head>
<body>
<%

for(int i=0;i<nl.getLength();i++)
{
  NodeList nameNlc=    doc.getElementsByTagName("name");
  Element nameElements=(Element)nameNlc.item(i);
  String nameTagValue=nameElements.getChildNodes().item(0).getNodeValue();
 
 
  NodeList authorNlc=    doc.getElementsByTagName("author");
  Element authorElements=(Element)authorNlc.item(i);
  String authorTagValue=authorElements.getChildNodes().item(0).getNodeValue();
 
  NodeList dateNlc=    doc.getElementsByTagName("publication-date");
  Element dateElements=(Element)dateNlc.item(i);
  String dateTagValue=dateElements.getChildNodes().item(0).getNodeValue();
 
  out.println("name :"+nameTagValue+"<br>");  
  out.println("author :"+authorTagValue+"<br>");  
  out.println("publication-date :"+dateTagValue+"<br><br>");  
}

%>

</body>
</html>
READ MORE - How to read XML file in java

 
 
 
 


Copyright © 2012 http://codeprecisely.blogspot.com. All rights reserved |Term of Use and Policies|