• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Issue with writing Quote(") character in XML

 
Sumeet H Singh
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
How to write the " character in the value of an attribute through Java?
I am using StAX API for writing XML and I replace the " with its escape sequence which is <ampersand>quot; (read<ampersand> as & as I cant write the exact sequence here; it get displayed as ") in my attribute values:



The issue is this that it also inserts the escape sequence for '&' alongwith it; so the XML attribute values look like <ampersand>amp;quot; instead of just <ampersand>quot;

Any suggestions on how to resolve this?

Thanks,
Sumeet
 
g tsuji
Ranch Hand
Posts: 651
3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The documentation of XMLStreamWriter interface has this to say.
http://docs.oracle.com/javase/6/docs/api/javax/xml/stream/XMLStreamWriter.html
The XMLStreamWriter does not perform well formedness checking on its input. However the writeCharacters method is required to escape & , < and > For attribute values the writeAttribute method will escape the above characters plus " to ensure that all character content and attribute values are well formed.


It means if you have already the string value attributeValue for some attribute of name attributeName (and suppose no namespace etc...), the XMLStreamWriter instance, say writer, will automatically take care of the escape when it writes out the attribute.

 
Sumeet H Singh
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't..I read this too(I am using XMLEventWriter, but that also claims the same thing) and was hoping it'll work, but it still outputs " instead of the escape sequence, resulting in a malformed XML..
Try this:




Output(Only relevant tag):

 
g tsuji
Ranch Hand
Posts: 651
3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't..I read this too(I am using XMLEventWriter, but that also claims the same thing) and was hoping it'll work, but it still outputs " instead of the escape sequence, resulting in a malformed XML..

Well, no. It works as claimed, it does, mind you. (Be careful of the need of writer's flush and close too.)

You cannot view the output with a user agent that replace character entity with the character itself, like for instance a browser. Look at the output xml with a plain text editor or hex editor or whatever else. The output should and does look like this literally.


The consequence is that when you use any parser to retrieve the attribute value of attr1, it shows up a string with the proper quotes as one intends to say the attribute containing.
 
Sumeet H Singh
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Tsuji, but its still not working for me..I have been trying with a simple text editor only(Notepad++)..Now I've even tried with notepad & wordpad..But it still has the same output..
Can you please try executing that piece of code and verify the output at your end?
 
g tsuji
Ranch Hand
Posts: 651
3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please try executing that piece of code and verify the output at your end?

Sure, I did, in your terms rather than in mine, prior to my post above.
 
Sumeet H Singh
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you share the exact code(entire java file content)? Maybe I'm making some mistake elsewhere..
 
g tsuji
Ranch Hand
Posts: 651
3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my throw-away.
 
Sumeet H Singh
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to your code I could nail down the reason!
It has got something to do with eclipse...Your as well as my code works perfectly fine if run through command prompt.
But through eclipse, both of them write the " character instead of the escape sequence..

Any guesses on what could be the reason?
 
g tsuji
Ranch Hand
Posts: 651
3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I run a simple test on Helios. The xml generated looks just fine. I would be worried indeed if it even failed on such a basic task with these extensively used api's.
 
Sumeet H Singh
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah seriously, shouldn't behave this way..I am on Juno..Will try to figure out the issue and post if I find something.
Thanks for the help
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic