aspose file tools*
The moose likes XML and Related Technologies and the fly likes getting value with Sax/w3c dom Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "getting value with Sax/w3c dom" Watch "getting value with Sax/w3c dom" New topic
Author

getting value with Sax/w3c dom

Lucy Sommerman
Ranch Hand

Joined: Nov 25, 2004
Posts: 61
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

Joined: Nov 25, 2004
Posts: 61
and is javax.xml.xpath only available as part of Java 2 Platform SE 5.0 and not 4.x?

ta
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39545
    
  27

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.


Ping & DNS - updated with new look and Ping home screen widget
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Moving to our XML forum...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getting value with Sax/w3c dom
 
Similar Threads
Displaying DOM using swing
Nesting of tags
validate schema in DOM
Suppress text nodes in output when empty
XML to DOM to JTree