aspose file tools*
The moose likes XML and Related Technologies and the fly likes unable to remove spaces in xml Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "unable to remove spaces in xml" Watch "unable to remove spaces in xml" New topic
Author

unable to remove spaces in xml

madhuri kunchala
Ranch Hand

Joined: Mar 30, 2010
Posts: 350
hi,
i tried to delete an element in xml..for that i used servlet coding...it is getting deleted but blank spaces are remaining i dont want that blank spaces..can any one do me needfull..here is my code..can any one check it



thanks in advance,
madhu
madhuri kunchala
Ranch Hand

Joined: Mar 30, 2010
Posts: 350
i had edited the code
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10266
    
168

Please choose the correct forum for XML related questions. I'll move this from the Servlets forum to the XML forum.

[My Blog] [JavaRanch Journal]
madhuri kunchala
Ranch Hand

Joined: Mar 30, 2010
Posts: 350
the code is in servlet...from servlet im deleting xml file
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

madhuri kunchala wrote:the code is in servlet...from servlet im deleting xml file

(His point was that the question has nothing to do with servlets, and everything to do with XML. You could be calling the same code from Swing, an Applet, a console program, anywhere.)
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Define "it doesn't remove spaces". At what point do you see the spaces? What happens if you print out the node value immediately after removing the spaces?
madhuri kunchala
Ranch Hand

Joined: Mar 30, 2010
Posts: 350
k..i got it..it is moved to xml..no one is responding to my question...
madhuri kunchala
Ranch Hand

Joined: Mar 30, 2010
Posts: 350
it is going to delete and spaces are left..example here is my sample xml

'original xml file'



after doing delete of the pack..the output is




im getting spaces like this

</MA05>


<MA00>
i.e after end of first pack and start of next pack..
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12825
    
    5
As far as XML is concerned, those spaces (and line breaks and tabs) are valid Nodes of type TEXT and are part of the DOM.

Bill
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

And why does it matter if there's whitespace between elements?
madhuri kunchala
Ranch Hand

Joined: Mar 30, 2010
Posts: 350
but i dont need that spaces...can you suggest me how to delete that spaces..here is my code..



can you check my code and tell me where to do modifications...i tried.but i didnt got it...waiting for your reply.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Why do you care if there are spaces?
madhuri kunchala
Ranch Hand

Joined: Mar 30, 2010
Posts: 350
because client needs full xml file with no whitespaces..thats why..
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Then they're parsing XML wrong.
madhuri kunchala
Ranch Hand

Joined: Mar 30, 2010
Posts: 350
can you provide me where is wrong i'll do it ..im so thankful if you provide me the solution..
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I'd probably just post-process the output file since you're not in control of how the transformer chooses to render XML (unless you are, in which case figure out what property needs to be set).
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

madhuri kunchala wrote:can you provide me where is wrong i'll do it ..im so thankful if you provide me the solution..

Whoever is *PARSING* it is wrong. Inter-nodal whitespace should be completely ignored if it's being parsed correctly.
madhuri kunchala
Ranch Hand

Joined: Mar 30, 2010
Posts: 350
i need is to remove blankspaces if i delete any pack....i used trim() concept at line 18..but not working..it is not taking trim() function...
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Omg.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12825
    
    5
Stop and think.

You already have the information you need - the offending blank lines are a child node of type TEXT_NODE and a direct child of the PACKS element (document root ?)

You want the TEXT_NODE between your MAxx Elements to only have one formatting crlf instead of several.

Now - how those extra lines got in there is not clear from my cursory examination of your code, but you should be able to locate the TEXT_NODE and change it.

Bill
madhuri kunchala
Ranch Hand

Joined: Mar 30, 2010
Posts: 350
i tried according what you had suggested by unable to remove spaces..
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

David Newton wrote:I'd probably just post-process the output file since you're not in control of how the transformer chooses to render XML (unless you are, in which case figure out what property needs to be set).

With the caveat that this is silly, because inter-nodal space shouldn't matter.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12825
    
    5
Why are you doing this:


See javax.xml.transform.OutputKeys - a yes authorizes the transformer to insert additional whitespace.



Bill
(I should have noticed that before, sorry. My excuse is I never use that setting.)
madhuri kunchala
Ranch Hand

Joined: Mar 30, 2010
Posts: 350
hi good morning,
i modified my code and run my code...even though im getting the same problem....here is my modified code...



what should be done..inorder to remove spaces..
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

David Newton wrote:
David Newton wrote:I'd probably just post-process the output file since you're not in control of how the transformer chooses to render XML (unless you are, in which case figure out what property needs to be set).

With the caveat that this is silly, because inter-nodal space shouldn't matter.
madhuri kunchala
Ranch Hand

Joined: Mar 30, 2010
Posts: 350
According to William Brogden, i had changed my transformer code...eventhough same problem arises....is there any solution for my problem..
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

David Newton wrote:
David Newton wrote:
David Newton wrote:I'd probably just post-process the output file since you're not in control of how the transformer chooses to render XML (unless you are, in which case figure out what property needs to be set).

With the caveat that this is silly, because inter-nodal space shouldn't matter.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12825
    
    5
I suppose you could always treat it as a pure text problem, reading the file line by line and discarding blank lines, writing the lines with content to a new file, which what David meant by "post process".

Or the equivalent in memory with a custom OutputStream - however I suspect that your code or the transformer you are using is somehow introducing the extra blank lines.

Bill
madhuri kunchala
Ranch Hand

Joined: Mar 30, 2010
Posts: 350
hi,
i checked line by line...but i didnt see that where to remove the blank lines...
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

What?
madhuri kunchala
Ranch Hand

Joined: Mar 30, 2010
Posts: 350
here is my code..i had some modifications inorder to remove whitespaces in xml file..but it is not get deleted...can you suggest me the solution

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Er. Anyway, go through the output file line by line and remove any lines that are entirely whitespace, just like William already said. Still with the caveat that this should in no way be necessary if the XML is being parsed in any reasonable way. You realize you're talking about non-significant whitespace between elements, right?
madhuri kunchala
Ranch Hand

Joined: Mar 30, 2010
Posts: 350
hi,
According to William and David i checked line by line and i used 'OutputStream' also..but im unable to remove the spaces...here is my modified code...can anyone help in this issue..



waiting for the reply..
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

1) Open the XML file you've written.
2) Read it line-by-line
3) For each line that is not only whitespace write it to a new XML file.

Now do you understand?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: unable to remove spaces in xml