aspose file tools*
The moose likes XML and Related Technologies and the fly likes empty xml element is changed in the output while using dom4j-1.6.jar 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 "empty xml element is changed in the output while using dom4j-1.6.jar" Watch "empty xml element is changed in the output while using dom4j-1.6.jar" New topic
Author

empty xml element is changed in the output while using dom4j-1.6.jar

asfak mahamud
Greenhorn

Joined: Apr 17, 2008
Posts: 5
The following method I have used to write a dom4j Document to the output.




Now my problem is :

The element <abc></abc> in the in the Document is changed to <abc/> in the output file.

I want

1. <abc/> will be output as <abc/>
2. <abc></abc> will be output as <abc></abc>

i.e. I want the output must be as it is in the input.

I found the XMLWriter.java has a method


is responsible for this problem.

But I can not figure out what will be the fix. I need an urgent solution of this problem.

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

Joined: Mar 22, 2000
Posts: 12789
    
    5
As far as XML correctness is concerned those two forms for an empty element are identical.

All the XML parsers I know of will treat them the same, so what is your problem?

Bill
asfak mahamud
Greenhorn

Joined: Apr 17, 2008
Posts: 5

Thanks for your reply. I have a program that modifies the source xml files in some specific positions. My clients want the output xml file should be changed only in those specific portions.

So when the output file changes the elements out of those specific portions my clients get confused. They do not want this unwanted changes.


Is there any fix?

Regards.

William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12789
    
    5
The only "fix" I can think of would be to forget treating the input and output as XML and treat it as text.

A bit of a pain to program but it would ensure that <tag/> and <tag></tag> and all the various formatting crlfs and tabs that people don't think about when looking at an XML document don't get changed.

Bill

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Well, it looks like the XMLWriter might have an option to output empty elements in the short form or in the long form, based on that code you posted. But it can't possibly have an option "output in the same form as the original document" because the XMLWriter can't know what the form in the original document was. That's because the parser doesn't keep track of that.

And that's because it doesn't matter. If your customer thinks it matters then they are making a mistake and you should educate them. It doesn't matter.
asfak mahamud
Greenhorn

Joined: Apr 17, 2008
Posts: 5
Thanks for your reply.

I have solved the problem almost like the way William Brogden has said.

1. At first I have taken the xml file and done a "find and replace operation" to all the "></" places with "a long special text" that must not occur in the xml file using apache common io.
2. Then I did my usual task and got the output file.
3. Then I did find and replace operation again upon both input and output file. This time I only replaced my "a long special text" with an empty string.

That's it.


Regards.
>
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12789
    
    5
Ha! That is a rather elegant solution, thanks for reporting it.

Bill
Sprieo Lee
Greenhorn

Joined: Dec 12, 2013
Posts: 1
Hi, asfak mahamud, simply, I use Document.asXML(); and I got the exactly what you want. lol~
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: empty xml element is changed in the output while using dom4j-1.6.jar