|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
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! |
|
#2
|
||||
|
||||
|
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 |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Java Help > Parsing XML |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|