aspose file tools*
The moose likes XML and Related Technologies and the fly likes Issue with writing Quote( Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Issue with writing Quote(") character in XML" Watch "Issue with writing Quote(") character in XML" New topic
Author

Issue with writing Quote(") character in XML

Sumeet H Singh
Greenhorn

Joined: Jan 03, 2009
Posts: 29
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


Sumeet
SCJP, SCWCD
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 535
    
    3
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

Joined: Jan 03, 2009
Posts: 29
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

Joined: Jan 18, 2011
Posts: 535
    
    3
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

Joined: Jan 03, 2009
Posts: 29
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

Joined: Jan 18, 2011
Posts: 535
    
    3
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

Joined: Jan 03, 2009
Posts: 29
Can you share the exact code(entire java file content)? Maybe I'm making some mistake elsewhere..
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 535
    
    3
Here is my throw-away.
Sumeet H Singh
Greenhorn

Joined: Jan 03, 2009
Posts: 29
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

Joined: Jan 18, 2011
Posts: 535
    
    3
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

Joined: Jan 03, 2009
Posts: 29
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
 
Consider Paul's rocket mass heater.
 
subject: Issue with writing Quote(") character in XML