• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ha! That is a rather elegant solution, thanks for reporting it.

Bill
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, asfak mahamud, simply, I use Document.asXML(); and I got the exactly what you want. lol~
 
I want my playground back. Here, I'll give you this tiny ad for it:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic