Hi Michael,
Yes, I can be able to parse the xml data, but my program is failing
when executing the code below, to retrieve the Record where there is
this empty tag without a value, <weight/>
<weight>something</weight> can be retrieved, but why not this one
<weight/>
As far as I am concerned <weight/> is a well-formed empty tag, and it
should be retrieved successfully!
There must an XML/Jave API to be able to retrieve it!
..............................................................................................
if (node.getNodeName()=="weight") {
String weightvalue = node.getFirstChild().getNodeValue();
}
-----------------------------------------------------------------------------------------------
For your info, Node is the object of the class: org.w3c.dom.Node
see below a sample of my xml file:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<root>
<Record>
<PartNo>01</PartNo>
<weight/>
</Record>
<Record>
<PartNo>02</PartNo>
<weight>A</weight>
</Record>
</root>
Andrew Thompson - 13 Aug 2006 19:33 GMT
...
> Yes, I can be able to parse the xml data, but my program is failing
With what exact exception or error message?
Please copy/paste it (or at least, the first few lines).
> if (node.getNodeName()=="weight") {
> String weightvalue = node.getFirstChild().getNodeValue();
> }
...
> For your info, Node is the object of the class: org.w3c.dom.Node
For my info., an SSCCE* serves better..
But note that the API documentation mentions that
the second method called might not return an Object,
therefore the call of the third method on the null
returned by the second method might iteslf
result in a NullPointerException.
( BTW -Multiple statements on one line, is not
the best strategy for debugging broken code. )
There are a variety of other things that might be
happening, best examined in that SSCCE.
* <http://www.physci.org/codes/sscce>
Andrew T.
Michael Rauscher - 13 Aug 2006 19:48 GMT
BeGreen schrieb:
> Hi Michael,
>
> Yes, I can be able to parse the xml data, but my program is failing
> when executing the code below, to retrieve the Record where there is
> this empty tag without a value, <weight/>
> ..............................................................................................
>
[quoted text clipped - 5 lines]
>
> For your info, Node is the object of the class: org.w3c.dom.Node
Note, org.w3c.dom.Node is an interface.
It's correct that you can't get the first child of an empty element: an
empty element is empty.
Change your code to something like:
if ( node.getNodeName()=="weight" ) {
String weightValue = (node.hasChildNodes()
? node.getFirstChild().getNodeValue()
: "EMPTY");
}
Bye
Michael
Roland de Ruiter - 13 Aug 2006 19:52 GMT
> Hi Michael,
>
[quoted text clipped - 17 lines]
>
> -----------------------------------------------------------------------------------------------
An empty tag has no child nodes. Find out how many children a node has
by using node.getChildNodes().getLength();
See Michael's example below in which I've extended to obtain the value
of the <weight> tags.
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
public class XMLTest {
public static void main(String[] args) throws Exception {
String xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"
+ "<root>\n"
+ " <Record>\n"
+ " <PartNo>01</PartNo>\n"
+ " <weight/>\n"
+ " </Record>\n"
+ " <Record>\n"
+ " <PartNo>2</PartNo>\n"
+ " <weight>A</weight>\n"
+ " </Record>\n" + "</root>";
System.out.println(xml);
byte data[] = xml.getBytes("ISO-8859-1");
ByteArrayInputStream is = new ByteArrayInputStream(data);
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(is);
is.close();
inspect(doc);
System.out.println("Done.");
}
private static void inspect(Node node) {
if (node != null) {
if ("weight".equals(node.getNodeName())) {
String weightValue;
NodeList children = node.getChildNodes();
if (children.getLength() > 0) {
weightValue = node.getFirstChild().getNodeValue();
}
else
{
weightValue = "#empty#";
}
System.out.println(node + " " + weightValue);
}
NodeList children = node.getChildNodes();
int n = children.getLength();
for (int i = 0; i < n; i++) {
inspect(children.item(i));
}
}
}
}

Signature
Regards,
Roland
BeGreen - 14 Aug 2006 00:28 GMT
Hi Guys,
Thanks for your help. I finally fixed it, with your sample codes.
> > Hi Michael,
> >
[quoted text clipped - 75 lines]
> }
> }