Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem to respresent data in JSP  RSS feed

 
jyoti mahajan
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello frnds:
following is the code....



xml_retrieve.java
this file read xml and gives the node name and node value.....

package org.servlet;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author CDAC
*/
import java.io.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import java.util.*;
public class xml_retrieve
{
String targetFileName = "c:/example2.xml";
public final static String getElementValue( Node elem )
{
Node kid;
if( elem != null){
if (elem.hasChildNodes()){
for( kid = elem.getFirstChild(); kid != null; kid = kid.getNextSibling() ){
if( kid.getNodeType() == Node.TEXT_NODE ){
return kid.getNodeValue();
}
}
}
}
return "";
}

private String getIndentSpaces(int indent)
{
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < indent; i++) {
buffer.append(" ");
}
return buffer.toString();
}

/** Writes node and all child nodes into System.out
* @param node XML node from from XML tree wrom which will output statement start
* @param indent number of spaces used to indent output
*/
public NodeData writeDocumentToOutput(Node node,int indent)
// public void writeDocumentToOutput(Node node,int indent)
{
// get element name
//String nodeName = node.getNodeName();
//NodeList nodeName1;
NodeData nodeData=new NodeData();
String nodeName=node.getNodeName();
// get element value
String nodeValue = getElementValue(node);
// get attributes of element
NamedNodeMap attributes = node.getAttributes();
// for(int j=0;nodeName.getLength(); j++)
System.out.println(getIndentSpaces(indent) + "NodeName: " + nodeName + ", NodeValue: " + nodeValue);
// nodeData.setNodeName(nodeName);
for (int i = 0; i < attributes.getLength(); i++) {
Node attribute = attributes.item(i);
String attName=attribute.getNodeName();
String attValue=attribute.getNodeValue();
System.out.println(getIndentSpaces(indent + 2) + "AttributeName: " + attribute.getNodeName() + ", attributeValue: " + attribute.getNodeValue());
nodeData.setAttName(attName);
nodeData.setAttValue(attValue);

}
// write all child nodes recursively
NodeList children = node.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
Node child = children.item(i);
if (child.getNodeType() == Node.ELEMENT_NODE) {
writeDocumentToOutput(child,indent + 2);
}
}
return nodeData;
}
/** Saves XML Document into XML file.
* @param fileName XML file name
* @param doc XML document to save
* @return <B>true</B> if method success <B>false</B> otherwise
*/
public boolean saveXMLDocument(String fileName, Document doc) {
System.out.println("Saving XML file... " + fileName);
// open output stream where XML Document will be saved
File xmlOutputFile = new File(fileName);
FileOutputStream fos;
Transformer transformer;
try {
fos = new FileOutputStream(xmlOutputFile);
}
catch (FileNotFoundException e) {
System.out.println("Error occured: " + e.getMessage());
return false;
}
// Use a Transformer for output
TransformerFactory transformerFactory = TransformerFactory.newInstance();
try {
transformer = transformerFactory.newTransformer();
}
catch (TransformerConfigurationException e) {
System.out.println("Transformer configuration error: " + e.getMessage());
return false;
}
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(fos);
// transform source into result will do save
try {
transformer.transform(source, result);
}
catch (TransformerException e) {
System.out.println("Error transform: " + e.getMessage());
}
System.out.println("XML file saved.");
return true;
}

/** Parses XML file and returns XML document.
* @param fileName XML file to parse
* @return XML document or <B>null</B> if error occured
*/
public Document xml_retrieve(String xmlString) {
System.out.println("Parsing XML file... " + xmlString);
DocumentBuilder docBuilder;
Document doc = null;
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
docBuilderFactory.setIgnoringElementContentWhitespace(true);
try {
docBuilder = docBuilderFactory.newDocumentBuilder();
}
catch (ParserConfigurationException e) {
System.out.println("Wrong parser configuration: " + e.getMessage());
return null;
}
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xmlString));

// Document doc = db.parse(is);
try {
doc = docBuilder.parse(is);
}
catch (SAXException e) {
System.out.println("Wrong XML file structure: " + e.getMessage());
return null;
}
catch (IOException e) {
System.out.println("Could not read source file: " + e.getMessage());
}
System.out.println("XML file parsed");
return doc;
}

}

NodeData.java

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package org.servlet;

import java.util.ArrayList;
/**
*
* @author CDAC
*/
import javax.servlet.http.*;
public class NodeData
{
HttpSession sess;
String NodeName="";
String NodeValue="";
String AttName="";
String AttValue="";
ArrayList NodeName1= new ArrayList();
//ArrayList NodeValue1= new ArrayList();
//ArrayList AttName1= new ArrayList();
//ArrayList AttValue1= new ArrayList();

public String getAttName() {
return AttName;
}

public void setAttName(String AttName) {
System.out.println( "AttName1: " + AttName );
NodeName1.add(AttName);
this.AttName = AttName;
//sess.setAttribute("AttName1", AttName1);
}

public String getAttValue() {
return AttValue;
}

public void setAttValue(String AttValue) {
System.out.println( "AttValue1: " + AttValue );
NodeName1.add(AttValue);
this.AttValue = AttValue;
//sess.setAttribute("AttValue1", AttValue1);
}

public String getNodeName() {
return NodeName;
}

public void setNodeName(String NodeName) {
System.out.println( "NodeName1: " + NodeName );
NodeName1.add(NodeName);
this.NodeName = NodeName;
// sess.setAttribute("NodeName", NodeName);
}
public String getNodeValue() {
return NodeValue;
}


public void setNodeValue(String NodeValue) {
System.out.println( "NodeValue1: " + NodeValue );
NodeName1.add(NodeValue);
this.NodeValue = NodeValue;
// sess.setAttribute("NodeValue1", NodeValue1);
}


}



xml_output.jsp



<%--
Document : index
Created on : Dec 31, 2010, 12:46:56 PM
Author : CDAC
--%>
<%@page import="java.util.*"%>
<%@page import="java.io.*,javax.xml.parsers.*,javax.xml.transform.*,org.servlet.*,javax.xml.transform.dom.*,javax.xml.transform.stream.*, org.w3c.dom.*;" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!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">
<title>JSP Page</title>
</head>
<body>
<%

String xmlString =
"<AvailablePackages>"
+ "<package packageName='NEW DELHI-MATARANI PACKAGE' packageCode='NDR00001' duration='3 Nights/4 Days' description='One of the holiest Hindu temples, the shrine of Vaishno Devi is dedicated to the Goddess Shakti. Affectionately also called as Mata Rani and Vaishnavi, the temple is the manifestation of the Mother Goddess. The temple is situated at an titleitude of 5200 ft., a distance of approx. 12 kms from Katra.' packageKeyname='vaishno-devi-package-tour' packageType='VAT'/>"
+ "<package packageName='NEW DELHI-SHIRDI PACKAGE' packageCode='NDR0023' duration='3 Nights / 4 Days' description='IRCTC proudly announces to relaunch SHIRDI PACKAGE with Charter Operations .Lord Sai Baba one of the most famous saints of India endowed with unprece dented powers and worshipped as a God incarnate. The life of Sai Baba is as wide and as deep as the infinite ocean; all can dive deep into it and take out precious gems in terms of knowledge and bhakti and distribute them.' packageKeyname='shirdi-package-tour' packageType='VAT'/>"
+" <package packageName='VAISHNO DEVI PACKAGE' packageCode='NDH028' duration='01 Night/02 Days' description='IRCTC presents Holiday package for JAMMU-KATRA-JAMMU, Mata Vaishno Devi Ji resides in a Holy Cave located in the folds of the three peaked mountain named Trikuta pronounced as Trikoot. The Holy Cave attracts lakhs of devotees every year' packageKeyname='vaishno-devi-package' packageType='VAT'/>"
+ "<package packageName='HIMALAYAN GLORY STANDARD PACKAGE' packageCode='WMH39S' duration='Nights/7 Days' description='Himalayan National Park, Poney and Yak Ride and One can see the endless Himalayan Panorama from Kufri, after lunch sight seeing of' packageKeyname='Amritsar_Dalhousie_tour' packageType='VAT'/>"
+ "<package packageName='DELHI-SHIMLA PACKAGE' packageCode='WMH32' duration='7 Night/ 8 Days' description='The former summer capital of the British in India, and the present capital of Himachal Pradesh, Shimla has been blessed with all the natural bounties w hich one can think of. It has got a scenic location; it is surrounded by green hills with snow capped peaks. Chandigarh, also called The Beautiful City, serves as the capital of two states, Punjab and Haryana, and is a union territory of India. The name translates from Hindi to English as the fort of Chandi, Chandi being a Hindu Goddess. Shimla bulging at its seams with unprecedented expansion, Sh imla retains its colonial heritage, with grand old buildings. Chandigarh known internationally for its architecture and urban planning, Chandigarh is home to numerous architectural projects of Le Corbusier, Pierre Jeanneret, Matthew Nowicki, and Albert Mayer , Serenity.' packageKeyname='shimla-delhi-holidays-packages' packageType='VAT' />"
+ "</AvailablePackages>";

String targetFileName = "c:/example2.xml";
xml_retrieve xml1=new xml_retrieve();
Document doc = xml1.xml_retrieve(xmlString);
// get root node of xml tree structure
Node root = doc.getDocumentElement();
ArrayList NodeName1=new ArrayList();

// write node and its child nodes into System.out
System.out.println("Statemend of XML document...");
xml1.writeDocumentToOutput(root,0);
System.out.println("... end of statement");

// write Document into XML file
// xml1.saveXMLDocument(targetFileName, doc);
//String NodeValue1=(String)session.getAttribute("NodeValue1");%>
<table>
<tr>
<td>
<%
System.out.println("Result"+NodeName1);
for (Iterator iter = NodeName1.iterator(); iter.hasNext(); )
{
String res=(String)iter.next();
System.out.println("RES:"+ res);
%>

<%= res %>
</td>
</tr>
<%}%>
</table>
</body>
</html>


I am sending string xml to retrieve the nodes and its value....its working gud..thn i am puting it arraylist.and whn it retrive the array list in jsp it doest show any result.

please help me out its very urgent..
or provide some other solution to show data in table in jsp.

Regards
jyoti mahajan
 
dileep keely
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Avoid using scriplet in jsp.

Use the JSTL tags when iterating through the collection(list....) instead of the java code.




 
Javin Paul
Ranch Hand
Posts: 297
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Also please use code tag to format code when you past it in forum , that would make code more readable .

e.g.

 
jyoti mahajan
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello frnds:
following is the code....



xml_retrieve.java
this file read xml and gives the node name and node value.....



NodeData.java




xml_output.jsp


I am sending string xml to retrieve the nodes and its value....its working gud..thn i am puting it arraylist.and whn it retrive the array list in jsp it doest show any result.

please help me out its very urgent..
or provide some other solution to show data in table in jsp.
submitting the same prob....please help me out..

Regards
jyoti mahajan
 
Vijitha Kumara
Bartender
Posts: 3992
27
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... its working gud..thn i am puting it arraylist.and whn

Please UseRealWords when posting in the forums, "gud" instead "good" etc... are make the reader confused.
... its very urgent..

Please EaseUp.

In to your problem, I'm not sure about your other code but this part:


You don't write anything to ArrayList called NodeName1 and try to iterate it?

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!