DocumentBuilder's parse method is not able to read a tag such as this one <weight/>

B

BeGreen

Hi All,

javax.xml.parsers.DocumentBuilder's parse method is not able to parse
the xml file, when
the record has an empty tag without a value, such as <weight/>.

Any Java class, you may know which can parse that tag successfully or
even ignore it?
Thanks!

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>
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

BeGreen said:
javax.xml.parsers.DocumentBuilder's parse method is not able to parse
the xml file, when
the record has an empty tag without a value, such as <weight/>.

Any Java class, you may know which can parse that tag successfully or
even ignore it?
Thanks!

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>

Java parses that XML just fine.

Assuming you change the bad <Record> to </Record> so
the XML becomes wellformed !

Arne
 
B

BeGreen

Arne, sorry for that. Actually I forgot the the forward slash on the
record, it's:

<Record>
<PartNo>01</PartNo>
<weight/>
</Record>



My only problem is to parse an empty tag with this format <weight/>
 
B

BeGreen

Arne, sorry for that. Actually I forgot the forward slash on the
record, it's:
...............................................................................
<Record>
<PartNo>01</PartNo>
<weight/>
</Record>
..............................................................................


My only problem is to parse an empty tag with this format <weight/>
 
M

Michael Rauscher

BeGreen said:
Hi All,

javax.xml.parsers.DocumentBuilder's parse method is not able to parse
the xml file, when
the record has an empty tag without a value, such as <weight/>.

Any Java class, you may know which can parse that tag successfully or
even ignore it?
Thanks!

import java.io.*;
import javax.xml.parsers.*;

public class XMLTest {
public static final 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();
builder.parse(is);
is.close();
System.out.println("Done.");
}
}

Bye
Michael
 
A

Andrew Thompson

Michael said:
BeGreen schrieb: .... ....
import java.io.*;
import javax.xml.parsers.*; .....
System.out.println("Done.");
}
}

:) That was a lovely little ..example. ;)

Andrew T.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

BeGreen said:
My only problem is to parse an empty tag with this format <weight/>

That is not a problem.

Java XML parsers do not have a problem with that.

Arne
 
B

BeGreen

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>
 
B

BeGreen

Arne,

See below my respone to Michael.

Yes, I can be able to parse the xml data, but to retrieve this kind of
empty tag <weight/> is my problem.

<weight> something </weight> can be retrieved though!

Thanks!
 
A

Andrew Thompson

BeGreen wrote:
....
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.
 
M

Michael Rauscher

BeGreen said:
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/>
..............................................................................................

if (node.getNodeName()=="weight") {
String weightvalue = node.getFirstChild().getNodeValue();
}

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
 
R

Roland de Ruiter

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();
}

-----------------------------------------------------------------------------------------------

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));
}
}
}
}
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

BeGreen said:
Yes, I can be able to parse the xml data, but to retrieve this kind of
empty tag <weight/> is my problem.
>See below my respone to Michael.

You can find the tag.

You can not find the non existing value inside the tag.

To me Java acts very logical.

if (node.getNodeName()=="weight") {
String weightvalue = node.getFirstChild().getNodeValue();
}

I can not see what node.getFirstChild() can return for
<weight/> except null.

And calling a method on null gives an exception.

Arne
 
B

BeGreen

Hi Guys,

Thanks for your help. I finally fixed it, with your sample codes.
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();
}

-----------------------------------------------------------------------------------------------

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));
}
}
}
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top