Ignoring white spaces in XML file

Hi All,

Please have a look at the following code (me learning XML, java, databases etc)

http://java.sun.com/xml/jaxp/dist/1.1/docs/tutorial/dom/work/DomEcho02.java

This displays the correct tree if XML file does not have any whitespaces. But displays a pretty MESSY tree if XML file has white spaces.

How to ignore whitespaces while processing XML file.

-Ajay
avi_indiaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
Try using a DTD declaration in your file
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dejan PažinHead of SW DevelopmentCommented:
Can you give examples of short XML files that display correct and then changed version that displays messy? Cause I couldt really reproduce it.
0
avi_indiaAuthor Commented:


correct XML file

----------------------------------
<?xml version="1.0" encoding="utf-8" ?><rules name="schema_repository_rules" desc="The set of rules"><cat1 name="category1"><rule name="assert_table" handler="Assert_Table"><table>master_global</table></rule></cat1></rules>
----------------------------------

incorrect (I mean which produces wrong output)

----------------------------------

<?xml version="1.0" encoding="utf-8" ?>
<rules name="schema_repository_rules" desc="The set of rules">
      <cat1 name="category1">
            <rule name="assert_table" handler="Assert_Table">
                  <table>master_global</table>
            </rule>
      </cat1>
</rules>

----------------------------------
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

CEHJCommented:
For testing purposes you'll need to post links to actual files
0
avi_indiaAuthor Commented:
How Do I do that. As soon as I host XML files on my web server and try saving them again back to my desktop, they get formatted!!!!!!
0
CEHJCommented:
They shouldn't get changed at all
0
CEHJCommented:
"This file is hosted by Tripod, a Lycos®Network Site, and is not available for download. "
0
avi_indiaAuthor Commented:
But I am able to!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

I am trying to implement DTD. As I am new, it will take time.

btw - which is better, DTD or schema
0
CEHJCommented:
Downloadable now.

DTD more widely supported. Schema more powerful
0
CEHJCommented:
The files you posted display identically with that program for me
0
Dejan PažinHead of SW DevelopmentCommented:

They dont display identically to me (I've copied the correct and incorrect files). The problem is with new lines. They are displayed in the tree as empty nodes.

If you want a quick solution (with no real understanding why this is so), just clear all the new lines from the file. I am not saying this is a good solution, but if you need a quick fix, it might work...
0
CEHJCommented:
Both files contain Windows line breaks
0
Dejan PažinHead of SW DevelopmentCommented:

CEHJ,

I was not talking about files (my mistake), I tried to say I copy-pasted the xml contents, that avi_india posted above. The one, which is in one line, is displayed nicely in the JTree, and the one with many lines, has nodes of empty text.

Therefore I suggested a quick solution of clearing all the new lines...
0
CEHJCommented:
>>I tried to say I copy-pasted the xml contents

Don't! You should be using the files posted ;-)
0
NaeemgCommented:
if u r using Java to parse xml and having whitespace problem then use it

builderFactory.setIgnoringElementContentWhitespace(true); //  set to ignore elements whitespaces
   
0
avi_indiaAuthor Commented:
Can somebody please help me with generating a DTD for ignoring whitespaces?
0
CEHJCommented:
My comment really centres around the fact i've noticed whitespace-related problems when using docs without DTDs. In this case, as i've already mentioned, the two files you posted both render fine for me, so your problem may lie elsewhere
0
avi_indiaAuthor Commented:
It was infact whitespace related issue. When I used DTD and setIgnoringElementContentWhitespace method..... it is working fine.

I am not sure how does those two files work good for you..
0
CEHJCommented:
>>When I used DTD and setIgnoringElementContentWhitespace method

What about when you just do the first of those two?

>>I am not sure how does those two files work good for you..

You've got to remember that xml libraries are subject to a lot of change. It could well be that i have a different parsing setup on my machine, even due to something as small as a minor version change in SDK
0
avi_indiaAuthor Commented:
doesnt work just with dtd
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.