Help: Inconsistent results of org.w3c.dom-based code evoked from different programs

B

Bryan

Hi there,

I ran into a kind of strange behavior of Java code. Maybe the reason
is obvious but I cannot figure it out ...

Suppose I have a xml file and from the field "<footag>bar
text</footag>" I simply want to extract "bar text". The following is
my code:

/**************************/
import org.w3c.dom.*;
..
..
..

String text;
Node textFieldNode;
..
..
.. //textFieldNode now points to the field.

//code A:
NodeList myLs = textFieldNode.getChildNodes();
for (int ii = 0; ii < myLs.getLength(); ii++) {
if (myLs.item(ii) instanceof Text)
text = myLs.item(ii).getNodeValue();
}

//code B:
text=textFieldNode.toString().replaceAll("<[/]*"+"footag"+">","");
..
..
..
/**********end of code********/

The problem is: Both code A and B generate the right result ("bar
text") when I test it. But when I include this class into a .jar file
and try to call it from within another package, code A generates,
still, the right result but code B assigns "[footage: null]" to text.
Just keen to know why the code works so differently.

Thanks in advance!

Regards,
Bryan
 
X

xarax

Bryan said:
Hi there,

I ran into a kind of strange behavior of Java code. Maybe the reason
is obvious but I cannot figure it out ...

Suppose I have a xml file and from the field "<footag>bar
text</footag>" I simply want to extract "bar text". The following is
my code:

/**************************/
import org.w3c.dom.*;
.
.
.

String text;
Node textFieldNode;
.
.
. //textFieldNode now points to the field.

//code A:
NodeList myLs = textFieldNode.getChildNodes();
for (int ii = 0; ii < myLs.getLength(); ii++) {
if (myLs.item(ii) instanceof Text)
text = myLs.item(ii).getNodeValue();
}

//code B:
text=textFieldNode.toString().replaceAll("<[/]*"+"footag"+">","");
.
.
.
/**********end of code********/

The problem is: Both code A and B generate the right result ("bar
text") when I test it. But when I include this class into a .jar file
and try to call it from within another package, code A generates,
still, the right result but code B assigns "[footage: null]" to text.

You wrote "footage", instead of "footag".
 
B

Bryan

xarax said:
Bryan said:
Hi there,

I ran into a kind of strange behavior of Java code. Maybe the reason
is obvious but I cannot figure it out ...

Suppose I have a xml file and from the field "<footag>bar
text</footag>" I simply want to extract "bar text". The following is
my code:

/**************************/
import org.w3c.dom.*;
.
.
.

String text;
Node textFieldNode;
.
.
. //textFieldNode now points to the field.

//code A:
NodeList myLs = textFieldNode.getChildNodes();
for (int ii = 0; ii < myLs.getLength(); ii++) {
if (myLs.item(ii) instanceof Text)
text = myLs.item(ii).getNodeValue();
}

//code B:
text=textFieldNode.toString().replaceAll("<[/]*"+"footag"+">","");
.
.
.
/**********end of code********/

The problem is: Both code A and B generate the right result ("bar
text") when I test it. But when I include this class into a .jar file
and try to call it from within another package, code A generates,
still, the right result but code B assigns "[footage: null]" to text.

You wrote "footage", instead of "footag".
Oops. That's a typo occurred when I tried to make up the example to
illustrate the problem. Sorry about that...
 

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,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top