Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 13th, 2002, 02:21 AM
JessJenn's Avatar
JessJenn JessJenn is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2002
Posts: 133 JessJenn User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 38 m 49 sec
Reputation Power: 6
Parsing XML

Being still relatively new at parsing XML with Java, I'd like some help with a JSP page I'm working on. I'm having trouble parsing subelements (e.g. Author in Authors). This is my code so far: (I'm doing the parsing with Xerces)

<%@ page language="java" %>
<%@ page import="javax.xml.parsers.*" %>
<%@ page import="org.w3c.dom.Document" %>
<%@ page import="org.w3c.dom.Element" %>
<%@ page import="org.w3c.dom.DOMException" %>
<%@ page import="java.net.URL" %>
<%@ page import="java.io.InputStream" %>
<%@ page import="java.io.IOException" %>

<FONT SIZE="4"><STRONG>Results</STRONG></FONT><BR><A HREF="10.jsp">Click Here to search again.</A><HR><BR>
<%
String fileName= "http://xml.amazon.com/onca/xml?v=1.0&t=webservices-20&dev-t=D4H18BA1ALLJ42&KeywordSearch=ColdFusion&mode=books&type=heavy&page=1&f=xml";
int listLength, listLengthAuthors;
Document document;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

//Read XML file
//Open the file for reading:
URL u = new URL(fileName);
InputStream inputXML = u.openStream();

//Build document:
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(inputXML);

//Generate the NodeList;
org.w3c.dom.NodeList nodeList = document.getElementsByTagName("Details");
listLength = nodeList.getLength(); // how many elements there are

// Query through XML File
for (int i=0; i<nodeList.getLength(); i++) {
String elementAsin = document.getElementsByTagName("Asin").item(i).getFirstChild().getNodeValue();
String elementProductName = document.getElementsByTagName("ProductName").item(i).getFirstChild().getNodeValue();
String elementCatalog = document.getElementsByTagName("Catalog").item(i).getFirstChild().getNodeValue();


String elementReleaseDate = document.getElementsByTagName("ReleaseDate").item(i).getFirstChild().getNodeValue();
String elementManufacturer = document.getElementsByTagName("Manufacturer").item(i).getFirstChild().getNodeValue();
String elementListPrice = document.getElementsByTagName("ListPrice").item(i).getFirstChild().getNodeValue();

out.print(elementAsin + "<BR>");
out.print(elementProductName + "<BR>");
out.print(elementCatalog + "<BR>");
out.print(elementReleaseDate + "<BR>");
out.print(elementManufacturer + "<BR>");
out.print(elementListPrice + "<BR>");

out.print("<HR>");
}
%>


This is the type of XML file I'm trying to parse - the above code retrieves an XML file from Amazon Web Services. (I'm not listing the whole XML file):

<Details url="http://www.amazon.com/exec/obidos/redirect?tag=webservices-20%26creative=D4H18BA1ALLJ42%26camp=2025%26link_code=xm2%26path=ASIN/0321125169">
<Asin>0321125169</Asin>
<ProductName>ColdFusion MX Web Application Construction Kit, Fifth Edition</ProductName>
<Catalog>Book</Catalog>
<Authors>
<Author>Ben Forta</Author>
<Author>Nate Weiss</Author>
<Author>Leon Chalnick</Author>
<Author>Angela C. Buraglia</Author>
</Authors>
<ReleaseDate>23 August, 2002</ReleaseDate>
<Manufacturer>Macromedia Press</Manufacturer>
<ImageUrlSmall>http://images.amazon.com/images/P/0...01.THUMBZZZ.jpg</ImageUrlSmall>
<ImageUrlMedium>http://images.amazon.com/images/P/0...01.MZZZZZZZ.jpg</ImageUrlMedium>
<ImageUrlLarge>http://images.amazon.com/images/P/0...01.LZZZZZZZ.jpg</ImageUrlLarge>
<ListPrice>$54.99</ListPrice>
<OurPrice>$38.49</OurPrice>
<UsedPrice>$29.85</UsedPrice>
<ThirdPartyNewPrice>$35.63</ThirdPartyNewPrice>
<SalesRank>1,865</SalesRank>
<Lists>
<ListId>WIM55ESR00FV</ListId>
<ListId>1VMBFGMQ6C7Q3</ListId>
<ListId>1HK0FJUSWVEXI</ListId>
</Lists>

.
.
.

and so on.

Elements such as Author and Lists have subelements. I'm curious to know how to extract these (Print them out like I am doing the other elements). If anyone has a solution I would gladly appreciate it. Thanks!

Reply With Quote
  #2  
Old December 14th, 2002, 04:13 PM
Shocka's Avatar
Shocka Shocka is offline
dont click here
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Posts: 409 Shocka User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 35 m
Reputation Power: 7
try
NodeList children = node.getChildren();
if(children != null){
// create for loop to go through the children
}

check out this site..

http://www-106.ibm.com/developerwor...ljava-menu.html

should answer all ur XML and java questions

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Parsing XML


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway