Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A simple Java SAX parser - Output is not ok

 
Andy Jack
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried making a java based SAX parser for my XML file. I altered the code in this example.
I also posted this question on stack overflow, but none of the responses could solve my problem.
The problem is that I do not see passenger id value in the output. But, I see passenger printed. How do I fix this ?

XML File


Java code


Output

 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What values do you print out for qname on line 24 ?
Do any of these values match what you are checking for on lines 26 and 31 ?

 
Andy Jack
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:What values do you print out for qname on line 24 ?
Do any of these values match what you are checking for on lines 26 and 31 ?



I tried putting "passenger", "passenger id", "id" in both 26 and 31. It did not give me the value of id. What do I do now ?
 
Henry Wong
author
Marshal
Pie
Posts: 21408
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andy Jack wrote:I tried putting "passenger", "passenger id", "id" in both 26 and 31. It did not give me the value of id. What do I do now ?


In this example, the "id" component is *not* an element -- it is an attribute. So, you need to iterate through the attribute list that is passed to the element callback.

Henry
 
Paul Clapham
Sheriff
Posts: 21319
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to go back to your XML book and read a bit more about the basic parts of an XML document. This fragment here:



is the start tag of a "passenger" element. It contains an "id" attribute whose value is "001". If you interpreted it as an element whose name was "passenger id", then that wasn't correct. An XML element name can't contain whitespace.

It isn't illegal to put whitespace around the equal sign in an attribute, but almost nobody ever does that. Typically the tag would look like this:



That would make it a little more difficult for the unwary reader to mistake the meaning of the tag.

And by the way you shouldn't be using "equalsIgnoreCase" to compare an element name. In XML, element names are case-sensitive so "passenger" and "Passenger" are different things.

 
Henry Wong
author
Marshal
Pie
Posts: 21408
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andy Jack wrote:I also posted this question on stack overflow, but none of the responses could solve my problem.



And BTW, it looks like you also got an answer (with a code example) in your other topic -- so probably should mark this as resolved.

Henry
 
Andy Jack
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Andy Jack wrote:I also posted this question on stack overflow, but none of the responses could solve my problem.



And BTW, it looks like you also got an answer (with a code example) in your other topic -- so probably should mark this as resolved.

Henry


Yes, here is the solution

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic