Results 1 to 2 of 2
  1. #1
    himacherla is offline Member
    Join Date
    Jul 2009
    Posts
    2
    Rep Power
    0

    Thumbs up Challenge for jsp developers

    hi guys,
    This is murali krishna. i have a small problem,
    i have one xml file (books.xml)

    <?xml version="1.0" encoding="iso-8859-1"?>
    <library>
    <book id="1">
    <name>Head First Java, 2nd Edition</name>
    <author>Kathy Sierra and Bert Bates</author>
    <publication-date>09-Feb-2005</publication-date>
    </book>
    <book id="2">
    <name>Effective Java</name>
    <author>Joshua Bloch</author>
    <publication-date>28-May-2008</publication-date>
    </book>
    <book id="3">
    <name>Java How to Program, 7th Edition</name>
    <author>Harvey M. Deitel and Paul J. Deitel</author>
    <publication-date>6-Jan-2007</publication-date>
    </book>
    </library>

    I tried to read this xml file in jsp as shown below

    <%@ page language="java" %>
    <%@ page import="org.w3c.dom.*" %>
    <%@ page import="javax.xml.parsers.DocumentBuilder" %>
    <%@ page import="javax.xml.parsers.DocumentBuilderFactory" %>
    <%
    DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
    DocumentBuilder db =dbf.newDocumentBuilder();
    Document doc=db.parse("c:\\books.xml");


    NodeList nl = doc.getElementsByTagName("book");

    %>
    <html>
    <head>
    <title>How to read XML file in JAVA</title>
    </head>
    <body>
    <%

    for(int i=0;i<nl.getLength();i++)
    {
    NodeList nameNlc= doc.getElementsByTagName("name");
    Element nameElements=(Element)nameNlc.item(i);
    String nameTagValue=nameElements.getChildNodes().item(0). getNodeValue();


    NodeList authorNlc= doc.getElementsByTagName("author");
    Element authorElements=(Element)authorNlc.item(i);
    String authorTagValue=authorElements.getChildNodes().item (0).getNodeValue();

    NodeList dateNlc= doc.getElementsByTagName("publication-date");
    Element dateElements=(Element)dateNlc.item(i);
    String dateTagValue=dateElements.getChildNodes().item(0). getNodeValue();

    out.println("name :"+nameTagValue+"<br>");
    out.println("author :"+authorTagValue+"<br>");
    out.println("publication-date :"+dateTagValue+"<br><br>");
    }

    %>

    </body>
    </html>

    so, my problem is I was unable to read the attribute of the tag book , that is "id".
    can any one tell me how to read this "id" attribute.

    Murali Krishna
    himacherla@gmail.com

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    changes needed in bold

    Java Code:
    for(int i=0;i<nl.getLength();i++)
    {
    NodeList nameNlc= doc.getElementsByTagName("name");
    Element nameElements=(Element)nameNlc.item(i);
    String nameTagValue=nameElements.getChildNodes().item(0). getNodeValue();
    
    
    NodeList authorNlc= doc.getElementsByTagName("author");
    Element authorElements=(Element)authorNlc.item(i);
    [B]Element authorTag = (Element) authorElements.getChildNodes().item (0);
    String authorTagValue=authorTag.getNodeValue();
    String authorIdAttribute = authorTag.getAttribute("id");[/B]
    
    NodeList dateNlc= doc.getElementsByTagName("publication-date");
    Element dateElements=(Element)dateNlc.item(i);
    String dateTagValue=dateElements.getChildNodes().item(0). getNodeValue();
    
    out.println("name :"+nameTagValue +"<br>");
    out.println("author :"+authorTagValue+[B]" id :" + authorIdAttribute +[/B]"<br>");
    out.println("publication-date :"+dateTagValue+"<br><br>");
    }
    Where instead of getting the node, and the name directly. we want to capture the node object, cast it to an element object (because it is an element in this case), and then invoke the getAttribute().

    alternatively, I guess we could have done a
    Java Code:
    ((Element)dateElements.getChildNodes().item(0)).getAttribute("id")
    If you are just learning XML, I highly recommend getting into StAX, it is generally more useful than DOM, and in most cases just as functional.

Similar Threads

  1. Cash Prize Challenge
    By Laura 123 in forum New To Java
    Replies: 19
    Last Post: 04-01-2009, 03:56 AM
  2. Build Clock Challenge Help
    By Jadles in forum New To Java
    Replies: 1
    Last Post: 02-03-2009, 10:04 PM
  3. challenge for swing programmers
    By makpandian in forum AWT / Swing
    Replies: 2
    Last Post: 12-24-2008, 07:21 PM
  4. Ultimate challenge in JComboBox
    By vaidhu in forum AWT / Swing
    Replies: 3
    Last Post: 08-21-2008, 06:56 PM
  5. The Million Musician Challenge 0.821
    By levent in forum Java Software
    Replies: 0
    Last Post: 05-25-2007, 07:39 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •