How to extract details from XML?

View Answers

May 23, 2008 at 8:11 PM

Hi friend,

stocks.xml

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

<portfolio>
<stock>
<symbol>code</symbol>
<name>Java Tutorial</name>
<price>70.0</price>
</stock>
<stock>
<symbol>code1</symbol>
<name>Online Tutorial Available</name>
<price>45.00</price>
</stock>
<stock>
<symbol>Roseindia</symbol>
<name>Allover world allow online Tutorial</name>
<price>124.10</price>
</stock>

</portfolio>

------------------------------------

genXML.jsp

<%@ page contentType="text/xml" %>

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

<portfolio>
<stock>
<symbol>code</symbol>
<name>Java Tutorial</name>
<price>70.0</price>
</stock>
<stock>
<symbol>code1</symbol>
<name>Online Tutorial Available</name>
<price>45.00</price>
</stock>
<stock>
<symbol>Roseindia</symbol>
<name>Allover world allow online Tutorial</name>
<price>124.10</price>
</stock>
</portfolio>
-----------------------------------
stocks.jsp

<%@ page contentType="text/xml" %>

<jsp:useBean id="portfolio" class="PortfolioBean" />

<%
java.util.Iterator folio = portfolio.getPortfolio();
Stock stock = null;
%>
<?xml version="1.0" encoding="UTF-8"?>
<portfolio>
<% while (folio.hasNext()) { %>
<% stock = (Stock)folio.next(); %>
<stock>
<symbol><%= stock.getSymbol() %></symbol>
<name><%= stock.getName() %></name>
<price><%=stock.getPrice() %></price>
</stock>
<% } %>
</portfolio>
--------------------------------


May 23, 2008 at 8:13 PM

Stock.java


public class Stock implements java.io.Serializable {
private String symbol;
private String name;
private float price;

public Stock(String symbol, String name, float price) {
this.symbol = symbol;
this.name = name;
this.price = price;
}

public String getSymbol() {
return symbol;
}

public String getName() {
return name;
}

public float getPrice() {
return price;
}
}

-------------------------
PortfolioBean.java

import java.util.*;

public class PortfolioBean implements java.io.Serializable {
private Vector portfolio = new Vector();

public PortfolioBean() {
portfolio.addElement(new Stock("code", "Java", (float) 70.0));
portfolio.addElement(new Stock("code1", "Online Tutorial Available", (float) 45.00));
portfolio.addElement(new Stock("Roseindia", "Allover world allow online Tutorial",(float) 124.10));
}

public Iterator getPortfolio() {
return portfolio.iterator();
}
}









Related Tutorials/Questions & Answers:
Advertisements