[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Ignoring white spaces in XML file

Posted on 2004-11-26
21
Medium Priority
?
464 Views
Last Modified: 2013-11-23
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
0
Comment
Question by:avi_india
  • 10
  • 7
  • 3
  • +1
21 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 300 total points
ID: 12680159
Try using a DTD declaration in your file
0
 
LVL 17

Expert Comment

by:Dejan Pažin
ID: 12680177
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
 

Author Comment

by:avi_india
ID: 12680204


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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 86

Expert Comment

by:CEHJ
ID: 12680214
For testing purposes you'll need to post links to actual files
0
 

Author Comment

by:avi_india
ID: 12680395
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12680468
They shouldn't get changed at all
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12680535
"This file is hosted by Tripod, a Lycos®Network Site, and is not available for download. "
0
 

Author Comment

by:avi_india
ID: 12680553
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12680588
Downloadable now.

DTD more widely supported. Schema more powerful
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12680841
The files you posted display identically with that program for me
0
 
LVL 17

Expert Comment

by:Dejan Pažin
ID: 12681277

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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12681306
Both files contain Windows line breaks
0
 
LVL 17

Expert Comment

by:Dejan Pažin
ID: 12681387

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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12681437
>>I tried to say I copy-pasted the xml contents

Don't! You should be using the files posted ;-)
0
 
LVL 5

Assisted Solution

by:Naeemg
Naeemg earned 200 total points
ID: 12693798
if u r using Java to parse xml and having whitespace problem then use it

builderFactory.setIgnoringElementContentWhitespace(true); //  set to ignore elements whitespaces
   
0
 

Author Comment

by:avi_india
ID: 12703093
Can somebody please help me with generating a DTD for ignoring whitespaces?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12739700
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
 

Author Comment

by:avi_india
ID: 12743675
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12743751
>>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
 

Author Comment

by:avi_india
ID: 12744817
doesnt work just with dtd
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month18 days, 20 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question