Ignoring reading the record when the tag is <weight/>

B

BeGreen

Hi All,

I am trying to read the xml file below using java/xml API, such as
DocumentBuilderFactory, DocumentBuilder and so on.

Do you know if there is any java/xml class, I can use to be able to
ignore the first record below which doesn't have any weight value. The
tag is: <weight/>




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

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

BeGreen

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!
 
J

Johannes Koch

BeGreen said:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<root>
<Record>
<PartNo>01</PartNo>
<weight/>
<Record>
[...]
This is not well-formed, close the Record element with </Record>.
 
B

BeGreen

Johannes,

<weight/> is well-formed. XML/Java API should be able to parse it, but
I am not having any luck!

Thanks!

Johannes said:
BeGreen said:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<root>
<Record>
<PartNo>01</PartNo>
<weight/>
<Record>
[...]
This is not well-formed, close the Record element with </Record>.
 
M

Martin Honnen

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

Sure it is, but your XML has the problem here

^^^^^^^^
where you need a closing tag </Record> instead.
 
J

Johannes Koch

BeGreen said:
Johannes said:
BeGreen said:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<root>
<Record>
<PartNo>01</PartNo>
<weight/>
<Record>

[...]
This is not well-formed, close the Record element with </Record>.
> <weight/> is well-formed. XML/Java API should be able to parse it, but
> I am not having any luck!

I didn't talk about <weight/>. Please read again.
 
B

BeGreen

Martin, yes the closing tag is </Record>, sorry for the typo!

So far, my program is failing when executing the code below which is
used to to retrieve the Record where there is this empty tag without a
value, <weight/>

...............................................................................................

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

Martin Honnen

BeGreen wrote:

So far, my program is failing when executing the code below which is
used to to retrieve the Record where there is this empty tag without a
value, <weight/>

..............................................................................................

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

Well if the element weight is empty then it does not have any child
nodes so getFirstChild() returns null and you can't call a method (i.e.
getNodeValue) on null.
If you are using Java 1.5 then you can simply do e.g.
String weightvalue = node.getTextContent();
as Java 1.5 supports the DOM Level 3 Core.
If you are using Java 1.4 respectively DOM Level 2 then you could check e.g.
String weightvalue = node.getFirstChild() != null ?
node.getFirstChild().getNodeValue(): "";
 

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,780
Messages
2,569,611
Members
45,286
Latest member
ChristieSo

Latest Threads

Top