• Post Reply Bookmark Topic Watch Topic
  • New Topic

Nested quotes causing trouble  RSS feed

 
A Misa
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

I am facing a pretty interesting problem right now with processing a piece of xml code.

It's actually an xml request on which a bunch of methods are applied. In order to process it, it is converted to a String, but it already has some nested quotes inside. And i suppose that this convertion to String wraps the hole thing in another pair of quotes. Hence, the exception org.xml.sax.SAXParseException, followed by the message : "The element type 'myTag' must be terminated by the matching end-tag '</myTag>'".

How should i handle this error? Can i replace with something else the nested quotes when making the conversion? With what? What is the best approach?

Thanks!
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37462
537
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand. Can you post a (short) String that shows the problem?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Misa wrote:And i suppose that this convertion to String wraps the hole thing in another pair of quotes.

And I'm pretty sure that you suppose wrong. The only pre-processing that Java will do on a String is to store it in a way that it can decipher; and it will only do that for very obscure characters (like possibly some Chinese ones).

I think you're confusing a String with a String literal where, for visual purposes, you have to add an 'escape' character (\) to a double-quote.

Hence, the exception org.xml.sax.SAXParseException, followed by the message : "The element type 'myTag' must be terminated by the matching end-tag '</myTag>'".
How should i handle this error?

Follow Jeanne's advice; but I can assure you that you're basic premise is almost certainly wrong.

HIH

Winston
 
Paul Clapham
Sheriff
Posts: 22823
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Winston -- it's extremely unlikely that string quoting, in any language whatever, is going to result in that error message. That error message is much more likely to occur if you make the error (a common one) of failing to escape a < character which is in a text node between those two tags.

And it's much easier to make that kind of error if you process the XML document as an unparsed string. You're much better off using code which knows how to apply the XML rules, such as a DOM parser or a StAX parser.
 
A Misa
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

I said i only suppose that, i thought if it could be possible and that's why i wanted an experienced advice.
Meanwhile, i investigated more this problem and here's what happens:

I have my xml request stored as a string. On it i apply the method removeEmptyTags(). And this is the moment it crashes. To remove the empty tags, this method calls another three methods: one for converting the string to document element, one for removing the empty tags and lastly, one which converts it back to string. I localized the problem to be in the first of these methods - the conversion from string to element. The stringToElement method does this:



and further more, stringToDocument does this:



So, i think it messes up the parsing and fails in cascade through all these methods up until throwing the error message i posted previously.
I hope i explained more clearly this time.

Thanks!
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(repeat) Please post a short XML string that causes this error to occur.
 
A Misa
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here you go:



This is a hardcoding for the test.
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I found an end tag of "</fri:FTRPCInvoke>" without a corresponding begin tag.

An empty tag is "<mytag/>" NOT "</mytag>". Is that the problem?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Misa wrote:This is a hardcoding for the test.

Well, to my mind, that's just asking for trouble.

First: those lines are ENORMOUS (and far too long for our code blocks),
and I wouldn't trust myself not to miss out an escaped quote in one of them; let alone seven.

Second: why not just put these "commands" into a text file and read it in?
Then you don't have any worries about "escaping", and you can also indent it in a way that makes it more readable
(which would have probably have highlighted that mismatched tag).

Winston
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The pretty picture

Lines 4, 9, and 10 look they were intended to be empty tags but are not, other wise the begin tag is missing.

Line 28 looks like a corrupted CDATA line.
 
A Misa
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for pointing that out. I was checking out the code and didn't pay attention to the actual xml.

I cannot read this from a file. It's a request to a server, automatically built up based on some configuration file and accessed by its dao. After multiple operations, it gets in the format you've already seen and I don't really understand where it loses information or gets messed up.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Misa wrote:I cannot read this from a file. It's a request to a server, automatically built up based on some configuration file and accessed by its dao.

Which you obviously don't care about, since it's presented to you as a String - otherwise, why would your test variable be a String?

So, put the text in a file for your test program, and read it in.

After multiple operations, it gets in the format you've already seen and I don't really understand where it loses information or gets messed up.

Well, that strikes me as a prerequisite for doing any testing; so I'd find out.
Is there anybody who can explain it to you where you work? If so, that's probably the best place to start.

Winston
 
A Misa
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Well, that strikes me as a prerequisite for doing any testing; so I'd find out.
Winston


Well, i didn't even know where to start from, so the bad testing was not as useless as it seems.
Your answers got me on the right track in investigating this and thanks for that.

If i come up with a resolution which can help someone with the same kind of trouble, i'll post it here.

Thank you!
 
Tim Holloway
Saloon Keeper
Posts: 18789
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, there are 2 "gotcha's" when you're talking XML and Java.

In Java string literals, quotes must be escaped via a preceeding backslash or the compiler will think that the quote signals the end of the string literal.

In XML, quotes are one of the 5 "magic characters", (the other four are apostrophe, ampersand and left/right angle brackets). And in XML, you must replace the quote with a quote entiity. That is: "&quot;". And note the final semi-colon. If you omit it, your entity fails.

The alternative, when there's a lot of text is to bracket it in a CDATA element, where you can put any text you like except for "]]>", which is the CDATA terminator - its equivalent to a closing quote.

CDATA is especially useful for embedding stuff like JavaScript, where the js code may include not only quoted strings but less-than/greater-than comparisons and logical "and" expressions.

Judging from your error message, I'd say it's probably this sort of escape that you've run afoul of rather than the backslash escape.

And yes, Eclipse does have a built-in debugger. Plus, if you write your XML file out to disk and open it in Eclipse, the "source format" context menu option will pretty-print it.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!