File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Testing and the fly likes How to remove indents from xml? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "How to remove indents from xml?" Watch "How to remove indents from xml?" New topic
Author

How to remove indents from xml?

Angelo Huang
Greenhorn

Joined: May 02, 2008
Posts: 4
I have a xml like this:

<?xml version="1.0" encoding="UTF-8"?>
<view>
<help>
<block>No.1</block>
<device>USB</device>
</help>
</view>

How could I output the xml to :
<?xml version="1.0" encoding="UTF-8"?><view><help><block>No.1</block><device>USB</device></help></view>

The reason to output like this is to make the xml comparison passed.
I use org.custommonkey.xmlunit.XMLTestCase.compareXML to compare two xml string, one from Java code, the other from C# code. Now, since there are new line and whitespaces embedded in the Java generated XML, the comparison always failed. Is there any way to output the indent xml to the one string only?
[ May 03, 2008: Message edited by: Angelo Huang ]
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

You could use String.replaceAll to remove all \r\n and insert ""
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19695
    
  20

Originally posted by David O'Meara:
You could use String.replaceAll to remove all \r\n and insert ""

Which will only work with Windows files. I'd first replace all \r occurrences with "", then replace all \n occurrences with "". That would handle Mac and Unix files as well.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

In my defense I was being deliberately vague
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Seems like you just need to configure XMLUnit correctly: http://xmlunit.sourceforge.net/userguide/html/ar01s03.html#Whitespace%20Handling


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Angelo Huang
Greenhorn

Joined: May 02, 2008
Posts: 4
XMLUnit works well. Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to remove indents from xml?