File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes A simple Java SAX parser - Output is not ok Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "A simple Java SAX parser - Output is not ok " Watch "A simple Java SAX parser - Output is not ok " New topic
Author

A simple Java SAX parser - Output is not ok

Andy Jack
Ranch Hand

Joined: Nov 22, 2012
Posts: 257
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


Java Newbie with 72% in OCJP/SCJP - Super Confused Jobless Programmer.
I am a "newbie" too. Please verify my answers before you accept them.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3446
    
  12
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 ?


Joanne
Andy Jack
Ranch Hand

Joined: Nov 22, 2012
Posts: 257
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
Sheriff

Joined: Sep 28, 2004
Posts: 18715
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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
Sheriff

Joined: Sep 28, 2004
Posts: 18715
    
  40

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

Joined: Nov 22, 2012
Posts: 257
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

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A simple Java SAX parser - Output is not ok