How can I ignore \t and \n chars when parsing my XML?

Hi. I have a problem when parsing an XML file...

This part works (well, it doesn't cause any exceptions):

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

try

{

DocumentBuilder builder = factory.newDocumentBuilder();

Document MyDocument = builder.parse(new File(FolderName +"\\XMLDoc.xml") );

}

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

}

Afterward, I try the following code to traverse through my document:try

{

Node TargetNode = MyDocument.getDocumentElement();

Node NextNode = TargetNode.getFirstChild();

}

catch (Exception ex)

{

returnfalse;

}

returntrue;

TargetNode does in fact get the DocumentElement, TARGET. The problem is that NextNode ends up grabbing "\n\t", not the NEXT element!

Below is what my XML file looks like (except for the underscores, which I inserted to show where my file has tabs):

<?xml version="1.0" encoding="UTF-8"?>

<TARGET>

_<NEXT>

__<DATA>Default</DATA>

_</NEXT>

</TARGET>

And yes, I am sure that the encoding really IS UTF-8...

So, does anyone know how I can get it to ignore those whitespace characters and just give me the element that I want?

Thanks a bunch.

[2607 byte] By [SaintFresha] at [2007-11-26 18:06:32]
# 1

Perhaps I should also mention that it does work fine IF I remove all tabs and linefeeds from the XML file.

But XML files usually are formatted with tabs and linefeeds, so I am convinced that there must be a way to do it this way! :-) I have done it successfully in VB6/VB2005 with no problem... the parser just ignored all whitespace chars.

SaintFresha at 2007-7-9 5:37:35 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2
The Microsoft parsers are known to contain that sort of bug.If you want to get a certain kind of element, write code that looks for that kind of element. You can't just assume it will be the first child if people are allowed to insert arbitrary white-space text.
DrClapa at 2007-7-9 5:37:35 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3

Hi,-

your problem looks like the one that i had last week.

DrClap helped me see the problem.

all you need to do is to check the type of your node,

in this case you want to ignore text nodes, right?

http://forum.java.sun.com/thread.jspa?threadID=5134193&tstart=15

best regards

geomana at 2007-7-9 5:37:35 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...