JSP XML with DOM

Discussion in 'Java' started by designbyjohn@gmail.com, May 27, 2008.

  1. Guest

    Hi all.

    I am new to JSP and DOM, I am trying to get a subset of values
    (categories) from an xml file which looks something like this:

    <page>
    <something>
    ....
    </something>
    <categories>
    <category>cat1</category>
    <category>cat2</category>
    </categories>
    </page>

    So far I have come up with this. It returns "null null null", which is
    interesting because there are only two categories in my xml file.. Any
    help would be greatly appreciated.

    Regards,
    John.

    Document doc = db.parse(file);
    NodeList list = doc.getElementsByTagName("categories");
    for (int i=0; i<list.getLength(); i++) {
    Element element = (Element)list.item(i);
    NodeList childNodes = element.getChildNodes();
    if (childNodes != null) {
    for (int x=0; x<childNodes.getLength(); x++) {
    String string = childNodes.item(x).getNodeValue();
    out.println(string);
    }
    }
    }
    , May 27, 2008
    #1
    1. Advertising

  2. On May 28, 12:22 am, wrote:
    ...
    > So far I have come up with this. It returns "null null null", which is
    > interesting because there are only two categories in my xml file.. Any
    > help would be greatly appreciated.


    The best advice I can immediately offer on this code
    snippet is. When in doubt - print out.

    > Document doc = db.parse(file);
    > NodeList list = doc.getElementsByTagName("categories");
    > for (int i=0; i<list.getLength(); i++) {
    > Element element = (Element)list.item(i);


    System.out.println(element);

    > NodeList childNodes = element.getChildNodes();


    System.out.println(childNodes);

    > if (childNodes != null) {
    > for (int x=0; x<childNodes.getLength(); x++) {


    System.out.println(childNodes.item(x));

    > String string = childNodes.item(x).getNodeValue();
    > out.println(string);
    >
    >
    >
    > }
    > }
    > }


    As an aside. Please indent code that is posted.
    Unindented code is harder for many people to
    understand.

    Also note that you will generally get better help
    if you can prepare an SSCCE* that demonstrates the
    problem. (If you had, I would be running the code
    right now).

    * <http://sscce.org/>

    --
    Andrew T.
    PhySci.org
    Andrew Thompson, May 27, 2008
    #2
    1. Advertising

  3. Dave Miller Guest

    wrote:
    > Hi all.
    >
    > I am new to JSP and DOM, I am trying to get a subset of values
    > (categories) from an xml file which looks something like this:
    >
    > <page>
    > <something>
    > ...
    > </something>
    > <categories>
    > <category>cat1</category>
    > <category>cat2</category>
    > </categories>
    > </page>
    >
    > So far I have come up with this. It returns "null null null", which is
    > interesting because there are only two categories in my xml file.. Any
    > help would be greatly appreciated.
    >
    > Regards,
    > John.
    >
    > Document doc = db.parse(file);
    > NodeList list = doc.getElementsByTagName("categories");
    > for (int i=0; i<list.getLength(); i++) {
    > Element element = (Element)list.item(i);
    > NodeList childNodes = element.getChildNodes();
    > if (childNodes != null) {
    > for (int x=0; x<childNodes.getLength(); x++) {
    > String string = childNodes.item(x).getNodeValue();
    > out.println(string);
    > }
    > }
    > }
    >

    Drill down one more level:

    NodeList nl = d.getElementsByTagName("categories");
    for (int i = 0;i< nl.getLength(); i++){
    Node nd = nl.item(i);
    if(nd != null && nd.getFirstChild() != null &&
    nd.getFirstChild().getNodeValue() != null){
    out.print(nd.getFirstChild().getNodeValue());
    }
    }


    --
    Dave Miller
    Java Web Hosting at:
    http://www.cheap-jsp-hosting.com/
    Dave Miller, May 27, 2008
    #3
  4. Are Nybakk Guest

    *snip*

    > Drill down one more level:
    >
    > NodeList nl = d.getElementsByTagName("categories");
    > for (int i = 0;i< nl.getLength(); i++){
    > Node nd = nl.item(i);
    > if(nd != null && nd.getFirstChild() != null &&
    > nd.getFirstChild().getNodeValue() != null){
    > out.print(nd.getFirstChild().getNodeValue());
    > }
    > }


    This seems so familiar. After several weeks of programming in JavaScript
    I really got tired of the very clumsy DOM API. To make things simpler I
    made my own abstraction. I defined an object type that had simple
    methods that covered the uses I had, and it turned out there weren't
    many. I defined methods that set the object's "root element" field; one
    that took a XMLHttpRequest object and one that took any element. This
    way I could create a new object for each step in an XML hierarchy and
    use the very same methods instead of messing with childnode's
    childnode's childnode and whatever.

    --example--

    var DOM = new XmlTree();
    DOM.setRequest(request);

    var categories = DOM.getElements("category");

    for(var i=0; i<categories.lenght; i++) {

    var categoryTree = new XmlTree();
    categoryTree.setRoot(categories);

    //...

    }

    ----

    I used this method to generate object-trees from xml-responses. (It was
    inspired by JAX-B, however as far as I can see JavaScript can't offer
    that kind of functionality what with annotations and all.) But anyway,
    this method was so much cleaner than using the DOM API directly for
    everything. Also, it's way more dynamical, resulting in looser dependency.

    Are
    Are Nybakk, May 28, 2008
    #4
  5. Arne Vajhøj Guest

    wrote:
    > I am new to JSP and DOM, I am trying to get a subset of values
    > (categories) from an xml file which looks something like this:
    >
    > <page>
    > <something>
    > ...
    > </something>
    > <categories>
    > <category>cat1</category>
    > <category>cat2</category>
    > </categories>
    > </page>
    >
    > So far I have come up with this. It returns "null null null", which is
    > interesting because there are only two categories in my xml file.. Any
    > help would be greatly appreciated.


    > Document doc = db.parse(file);
    > NodeList list = doc.getElementsByTagName("categories");
    > for (int i=0; i<list.getLength(); i++) {
    > Element element = (Element)list.item(i);
    > NodeList childNodes = element.getChildNodes();
    > if (childNodes != null) {
    > for (int x=0; x<childNodes.getLength(); x++) {
    > String string = childNodes.item(x).getNodeValue();
    > out.println(string);
    > }
    > }
    > }


    A direct modification of your code that works is:

    <%@ page language="java" import="javax.xml.parsers.*,org.w3c.dom.*" %>
    <%
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse("C:/cat.xml");
    NodeList list = doc.getElementsByTagName("categories");
    for(int i = 0; i < list.getLength(); i++) {
    Element element = (Element)list.item(i);
    NodeList childNodes = element.getChildNodes();
    for(int j = 0; j < childNodes.getLength(); j++) {
    if(childNodes.item(j).getNodeType() == Node.ELEMENT_NODE &&
    childNodes.item(j).getNodeName().equals("category")) {
    Element child = (Element)childNodes.item(j);
    String txt = child.getFirstChild().getNodeValue();
    out.println(txt);
    }
    }
    }
    %>

    But that code can be a lot simplified by using XPath:

    <%@ page language="java"
    import="javax.xml.parsers.*,org.w3c.dom.*,org.apache.xpath.*" %>
    <%
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse("C:/cat.xml");
    NodeList cats = XPathAPI.selectNodeList(doc,
    "//page/categories/category/text()");
    for(int i = 0; i < cats.getLength(); i++) {
    String txt = cats.item(i).getNodeValue();
    out.println(txt);
    }
    %>

    And since JSTL supports XML, then it can done without scriptlet
    code at all:

    <%@ page language="java" %>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x" %>
    <c:import var="xml" url="file:///C:/cat.xml"/>
    <x:parse var="doc" xml="${xml}"/>
    <x:forEach select="$doc//page/categories/category">
    <x:eek:ut select="."/>
    </x:forEach>

    Arne
    Arne Vajhøj, May 31, 2008
    #5
  6. Arne Vajhøj Guest

    Are Nybakk wrote:
    > This seems so familiar. After several weeks of programming in JavaScript
    > I really got tired of the very clumsy DOM API. To make things simpler I
    > made my own abstraction. I defined an object type that had simple
    > methods that covered the uses I had, and it turned out there weren't
    > many. I defined methods that set the object's "root element" field; one
    > that took a XMLHttpRequest object and one that took any element. This
    > way I could create a new object for each step in an XML hierarchy and
    > use the very same methods instead of messing with childnode's
    > childnode's childnode and whatever.


    > I used this method to generate object-trees from xml-responses. (It was
    > inspired by JAX-B, however as far as I can see JavaScript can't offer
    > that kind of functionality what with annotations and all.) But anyway,
    > this method was so much cleaner than using the DOM API directly for
    > everything. Also, it's way more dynamical, resulting in looser dependency.


    There are so many XML API's available in Java - many of them are
    a lot simpler than W3C DOM.

    XPath can often simplify things a lot.

    JDOM is more Java and less W3C.

    Some like DOM4J.

    Arne
    Arne Vajhøj, May 31, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    534
  2. Replies:
    0
    Views:
    4,327
  3. Replies:
    3
    Views:
    517
    Stefan Behnel
    Aug 3, 2007
  4. mobbarley

    JSP XML with DOM

    mobbarley, May 27, 2008, in forum: Java
    Replies:
    0
    Views:
    322
    mobbarley
    May 27, 2008
  5. Alan
    Replies:
    6
    Views:
    1,568
Loading...

Share This Page