JSP + DOM anyone?

Discussion in 'OT Technology' started by mobbarley, May 27, 2008.

  1. mobbarley

    mobbarley Active Member

    Joined:
    Mar 4, 2005
    Messages:
    9,256
    Likes Received:
    2
    Location:
    Sydney
    I am using DOM in JSP to load up an xml file, I want to be able to grab the subelements of an element by its name only. All the examples I have seen iterate through the whole xml and dont seem to provide a method to easily select what I want... Can anyone point me in the right direction?
    for example:

    Code:
    <document>
      <something>
      ...
      </something>
      <categories>
        <category>cat1</category>
        <category>cat2</category>
      </categories>
    </document>
    
    I only want to extract the categories.
     
  2. mobbarley

    mobbarley Active Member

    Joined:
    Mar 4, 2005
    Messages:
    9,256
    Likes Received:
    2
    Location:
    Sydney
    okay, so far have got:

    Code:
    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++) {
    			out.println(childNodes.item(x).getNodeValue());
    		}
    	}
    }
    
    but that is returning : null null null
     
  3. mobbarley

    mobbarley Active Member

    Joined:
    Mar 4, 2005
    Messages:
    9,256
    Likes Received:
    2
    Location:
    Sydney
    which is interesting because there are only two <category> entries under <categories>
     

Share This Page