Whitespace problem - Xerces & Java - Anyone?

J

John

I have been trying with no result, to force Xerces parsers to ignore
whitespace in my XML file. I have tried several of the NG suggested
setFeature() methods in DOMParser to no avail.

I have now started to use the newest version of Xerces and as a result
I stopped using DOMParser. The following fragment of my code shows how
I create the parser and parse my XML file, which still has <null>
#text fields for whitespace. How can I force Xerces to ignore them?

Thanks,
John

Java Code:
=======

public class XMLConvert {

private DocumentBuilderFactory builderFactory;
private DocumentBuilder docFactory;
private Document inDoc;

public static void main(String[] args)
{
XMLConvert xml = null;

try {
xml = new XMLConvert(new FileInputStream(new File(args[0])));
} catch(Exception e) {
e.printStackTrace();
}

}

public XMLConvert(InputStream is)
{
try {
builderFactory = DocumentBuilderFactory.newInstance();

docFactory = builderFactory.newDocumentBuilder();

inDoc = docFactory.parse(is);
} catch(Exception e) {
e.printStackTrace();
}
}

My simple XML file:
============
<?xml version="1.0" encoding="UTF-8"?>
<Envelope>
<Body>
Test
</Body>
</Envelope>

Output from Java (children of root element):
============================

Node Name: #text
Local Name: null
URI: null

Node Name: Body
Local Name: null
URI: null

Node Name: #text
Local Name: null
URI: null
 
P

Philippe Poulard

Hi,

this method allow white spaces to be ignored :
builderFactory.setIgnoringElementContentWhitespace(true);
but you must specify a grammar (a DTD) for your document (the parser
can't guess which spaces can be ignored)

see JAXP documentation :
http://java.sun.com/j2se/1.4.2/docs/api/javax/xml/parsers/DocumentBuilderFactory.html
I have been trying with no result, to force Xerces parsers to ignore
whitespace in my XML file. I have tried several of the NG suggested
setFeature() methods in DOMParser to no avail.

I have now started to use the newest version of Xerces and as a result
I stopped using DOMParser. The following fragment of my code shows how
I create the parser and parse my XML file, which still has <null>
#text fields for whitespace. How can I force Xerces to ignore them?

Thanks,
John

Java Code:
=======

public class XMLConvert {

private DocumentBuilderFactory builderFactory;
private DocumentBuilder docFactory;
private Document inDoc;

public static void main(String[] args)
{
XMLConvert xml = null;

try {
xml = new XMLConvert(new FileInputStream(new File(args[0])));
} catch(Exception e) {
e.printStackTrace();
}

}

public XMLConvert(InputStream is)
{
try {
builderFactory = DocumentBuilderFactory.newInstance();

docFactory = builderFactory.newDocumentBuilder();

inDoc = docFactory.parse(is);
} catch(Exception e) {
e.printStackTrace();
}
}

--
Cordialement,

///
(. .)
-----ooO--(_)--Ooo-----
| Philippe Poulard |
-----------------------
 

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

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top