• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

getting value with Sax/w3c dom

 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Node value always coming back as Null given XML snippet and code below (scroll right to end to see the XML). It is obviously getting value of the Element, rather than the text - pse assist!! Have looked at APIs, can't work it out. thanks v.much

If a Node looks like
<item>hi</item>
I would expect node.getValue() to return the String "hi". But it is returning null.


CODE:


import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.w3c.dom.*;
//import org.w3c.dom.DOMException;
import java.io.*;
public class DomEcho {

static Document document;

public static void main(String argv[])
{
if (argv.length != 1) {
System.err.println("Usage: java DomEcho filename");
System.exit(1);
}

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//factory.setValidating(true);
//factory.setNamespaceAware(true);
try {
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse( new File(argv[0]) );
Element xmlRoot = document.getDocumentElement();
String rootName = xmlRoot.getTagName();
System.out.println ("rootName: " + rootName);
NodeList nl = document.getElementsByTagName("item");

int len = nl.getLength();
System.out.println ("LEN" + len);
for (int i = 0; i<len; i++) {
Node node = nl.item(i);
String nodeVal = node.getNodeValue();
String nodeName = node.getNodeName();
System.out.println ("nodeVal" + nodeVal + "nodeName" + nodeName + "nodeLocalName" + nodeLocalName + "nodeType" + nodeType);
}

} //catch (SAXParseException spe) {

//}
catch (SAXException sxe) {
// Error generated during parsing
Exception x = sxe;
if (sxe.getException() != null)
x = sxe.getException();
x.printStackTrace();
}
catch (ParserConfigurationException pce) {
// Parser with specified options can't be built
pce.printStackTrace();
} catch (IOException ioe) {
// I/O error
ioe.printStackTrace();
}

}// main

}// DomEcho

XML:

<?xml version='1.0' encoding='utf-8'?>

<!-- A SAMPLE set of slides -->
<slideshow
title="Sample Slide Show"
date="Date of publication"
author="Yours Truly"
>
<!-- TITLE SLIDE -->
<slide type="all">
<title>Wake up to WonderWidgets!</title>
</slide>

<!-- OVERVIEW -->
<slide type="all">
<title>Overview</title>
<item>Why <em>WonderWidgets</em> are great</item>
<item/>
<item>Who <em>buys</em> WonderWidgets</item>
<item>*auckland?christchurch*</item>
</slide>
</slideshow>
 
Lucy Sommerman
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and is javax.xml.xpath only available as part of Java 2 Platform SE 5.0 and not 4.x?

ta
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


If a Node looks like
<item>hi</item>
I would expect node.getValue() to return the String "hi". But it is returning null.



Nope, the value is defined to be null. Check the javadocs for org.w3c.dom.Node. You get at the enclosed text through the children of the node.


and is javax.xml.xpath only available as part of Java 2 Platform SE 5.0 and not 4.x?



Yes. You can find a Java 1.4 comaptible version of JAXP here.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to our XML forum...
 
Watchya got in that poodle gun? Anything for me? Or this tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic