473,320 Members | 2,193 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Whitespace problem - Xerces & Java - Anyone?


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
Jul 20 '05 #1
1 5999
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/...erFactory.html

John wrote:
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 |
-----------------------

Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: bugbear | last post by:
Subject pretty much says it all. I'd like to parse XML (duh!) using Xerces (because its fast, and reliable, and comprehensive, and supports lots of features). I'd like to conform to standards...
0
by: Pud | last post by:
Hiya, I was wondering if anyone could help me, I am trying to create an example program that uses the xerces XML parser in Java. Unfortunately I have come across a problem when using this...
0
by: Curtiss Howard | last post by:
I'm using Xerces 2.4.0 and I've got a schema (converted from a DTD) for my XML documents. However, Xerces will NOT ignore the whitespace between elements. These are the options I've set for the...
0
by: Dale Gerdemann | last post by:
I've been trying to use DOM level 3 with xerces-2_6_2. There's a sample called samples/DOM3.java, but I've had trouble with compilation. I've downloaded Xerces-J-bin.2.6.2 and...
2
by: Cigdem | last post by:
Hello, I am trying to parse the XML files that the user selects(XML files are on anoher OS400 system called "wkdis3"). But i am permenantly getting that error: Directory0: \\wkdis3\ROOT\home...
7
by: Georg J. Stach | last post by:
Hi, as mentioned above I'd like to validate a simple XML-document with a simple DTD. For this, I use Java and Xerces. But, when I have tags of this form: <tag>some characters in here</tag> ...
2
by: Carlitos | last post by:
Hi there, A class in Xerces J-API (Java) called TextImpl contains a property that returns whether the text is ignorable whitespace...
2
by: Carlitos | last post by:
Hi there, A class in Xerces J-API (Java) called TextImpl contains a property that returns whether the text is ignorable whitespace...
18
by: jacksu | last post by:
I have a simple program to run xpath with xerces 1_2_7 XPathFactory factory = XPathFactory.newInstance(); XPath xPath = factory.newXPath(); XPathExpression xp = xPath.compile(strXpr);...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.