Thread: Parsing XML

  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2002
    Rep Power

    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="" %>
    <%@ page import="" %>
    <%@ page import="" %>

    <FONT SIZE="4"><STRONG>Results</STRONG></FONT><BR><A HREF="10.jsp">Click Here to search again.</A><HR><BR>
    String fileName= "";
    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>");


    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="">
    <ProductName>ColdFusion MX Web Application Construction Kit, Fifth Edition</ProductName>
    <Author>Ben Forta</Author>
    <Author>Nate Weiss</Author>
    <Author>Leon Chalnick</Author>
    <Author>Angela C. Buraglia</Author>
    <ReleaseDate>23 August, 2002</ReleaseDate>
    <Manufacturer>Macromedia Press</Manufacturer>


    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. #2
  3. dont click here
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Rep Power
    NodeList children = node.getChildren();
    if(children != null){
    // create for loop to go through the children

    check out this site..

    should answer all ur XML and java questions

IMN logo majestic logo threadwatch logo seochat tools logo