File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes SAX:Why content handler's characters() method is called and print multiple space string. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "SAX:Why content handler Watch "SAX:Why content handler New topic
Author

SAX:Why content handler's characters() method is called and print multiple space string.

raj malhotra
Ranch Hand

Joined: Feb 22, 2007
Posts: 285
HI
I am overriding these methods in my ContentHandler as shown below.





and xml which SAX parser, parse is person.xml .I am passing this xml as first argument.-




I am getting the output as


Can somebody please tell me the reason where parser is finding these strings with multiple spaces?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
1. Those are the contents of Text type Nodes corresponding to the document formating lines.

2. Your implementation of characters method does not recognize the fact that characters may be called multiple times inside the content of a single element if the document is longer than a single input buffer.

Bill
raj malhotra
Ranch Hand

Joined: Feb 22, 2007
Posts: 285
Thanks William for your reply.How would i handle it if i want to show only the element name and and its value in the output?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18989
    
    8

raj malhotra wrote:How would i handle it if i want to show only the element name and and its value in the output?

You would use some if-statements which checked to see if the node in question was an Element node. And by the way, in DOM elements don't have values. Perhaps you're thinking of elements which only have a single child node which is a Text node? If you want to get that, then just write code to get the child. But you should be aware that it's possible for an Element node to have more than one child node. This is called "mixed content" and it's a perfectly normal thing to do.
raj malhotra
Ranch Hand

Joined: Feb 22, 2007
Posts: 285
Thanks William and Paul for your replies.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: SAX:Why content handler's characters() method is called and print multiple space string.