Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Claude Carter   on Jul 03 In Java Category.

  
Question Answered By: Paul Brooks   on Jul 03

i am also got same problem but i slove the problem
See tht ffolling JSP code and xml  CODE


<%@page language="java" import="java.io.File,
org.w3c.dom.Document,
org.w3c.dom.*,

javax.xml.parsers.DocumentBuilderFactory,
javax.xml.parsers.DocumentBuilder,
org.xml.sax.SAXException,
org.xml.sax.SAXParseException"
contentType="text/html"%>

<%@page pageEncoding="UTF-8"%>

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

"http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8">
</head>

<body>

<h1>JSP Page</h1>

<form action="##" method="get">

<%

int i=0;

String item="item";

try {


DocumentBuilderFactory docBuilderFactory =
DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse (new File
("C:/projects/JuiceMobi/sampleApp/src/java/Songs/music.xml"));

// normalize text representation
doc.getDocumentElement ().normalize ();
System.out.println ("Root element of the doc is " +
doc.getDocumentElement().getNodeName());


NodeList listOfPersons = doc.getElementsByTagName("song");
int totalPersons = listOfPersons.getLength();
//out.println("Total no of songs : " + totalPersons);
%><table border=1><tr><td><%
for(int s=0; s<listOfPersons.getLength() ; s++){

Node firstPersonNode = listOfPersons.item(s);
if(firstPersonNode.getNodeType() == Node.ELEMENT_NODE){

Element firstPersonElement = (Element)firstPersonNode;

//-------
NodeList titleNodeList = firstPersonElement.getElementsByTagName
("title");
Element titleElement = (Element)titleNodeList.item(0);

NodeList textTitleList = titleElement.getChildNodes();

out.println(" Title Name : " + ((Node)textTitleList.item
(0)).getNodeValue().trim()); %></td></tr><tr><td><%

//-------
NodeList artistNodeList = firstPersonElement.getElementsByTagName
("artist");
Element artistElement = (Element)artistNodeList.item(0);

NodeList textArtistList = artistElement.getChildNodes();
out.println("Artist : " + ((Node)textArtistList.item(0)).getNodeValue
().trim()); %></td</tr><tr><td><%
//------

NodeList priceNodeList= firstPersonElement.getElementsByTagName
("price");
Element priceElement=(Element)priceNodeList.item(0);

NodeList textPriceList=priceElement.getChildNodes();
out.println("Price :" +((Node)textPriceList.item(0)).getNodeValue
().trim()); %></td</tr><tr><td><%
//----

NodeList ageNodeList = firstPersonElement.getElementsByTagName
("year");
Element ageElement = (Element)ageNodeList.item(0);

NodeList textAgeList = ageElement.getChildNodes();
out.println("Year : " + ((Node)textAgeList.item(0)).getNodeValue
().trim()); %></td</tr><tr><td><%

//------


}//end of if clause


}//end of for loop with s var
out.println("</table>");

}catch (SAXParseException err) {
System.out.println ("** Parsing error" + ", line " +
err.getLineNumber () + ", uri " + err.getSystemId ());
System.out.println(" " + err.getMessage ());

}catch (SAXException e) {
Exception x = e.getException ();
((x == null) ? e : x).printStackTrace ();

}catch (Throwable t) {
t.printStackTrace ();
}
%>
</form>

</body>

</html>



THIS IS THE XML CODE


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

<root>
<song>
<title>"C:/projects/JuiceMobi/web/images/mm.jpg"</title>
<artist>Celin Daion</artist>
<price>10.00</price>
<year>1998</year>
</song>
<song>
<title>Bilamos</title>
<artist>Enrique</artist>
<price>15.00</price>
<year>1999</year>
</song>
<song>
<title>AAAA</title>
<artist>aaaa</artist>
<price>20.00</price>
<year>2000</year>
</song>
<song>
<title>BBBB</title>
<artist>bbbb</artist>
<price>12.00</price>
<year>2003</year>
</song>
<song>
<title>CCCC</title>
<artist>cccc</artist>
<price>15.00</price>
<year>2006</year>
</song>
</root>



but i need to cteate a bean what i wrote in the jsp page and same
thing retrive on the jsp page...

Share: 

 

This Question has 5 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on XML with java Or get search suggestion and latest updates.


Tagged: