By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 1,686 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

XML SAX parsing between the element tags

P: n/a
Hi, I am using SAX parsing and I need to get the information between
the tags. I have narrowed the problem down to my characters() method
in the DefaultHandler class. Here is my code:

public void characters(char[] ch, int start, int length) throws
SAXException{
String s = new String(ch, start, length);
Compnames[pos] = s;
}

Here is my xml file:

<?xml version="1.0"?>
<Data>
<coName>Company Name</coName>
<coAdd>Address</coAdd>
<coCity>City</coCity>
<coState>State</coState>
<coZIP>Zip</coZIP>
<coPhone>5555555555</coPhone>
<coEmail>Email</coEmail>
<coPicpth>Picture url</coPicpth>
</Data>

My startElement() method works fine so I know that the xml tags are
being parsed correctly. My problem is that int start is always being
set to 0, length is always set to 1, and char[] ch is an empty
string(" "). All examples I have seen online of the characters()
method seem to show that if I were to print the Compnames array then
it would show what is between the tags. As of right now it prints the
empty string. I am not sure why mine doesn't work the way others
doing the same thing have reported. My experience with XML is minimal
so maybe it is the xml file? Any help is appreciated.

Thanks in advance,

W2B
Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Wes Batson wrote:
My startElement() method works fine so I know that the xml tags are
being parsed correctly. My problem is that int start is always being
set to 0, length is always set to 1, and char[] ch is an empty
string(" "). All examples I have seen online of the characters()
method seem to show that if I were to print the Compnames array then
it would show what is between the tags. As of right now it prints the
empty string. I am not sure why mine doesn't work the way others
doing the same thing have reported. My experience with XML is minimal
so maybe it is the xml file? Any help is appreciated.


Hello Wes.

I had the same problem.
You should try to read the content of an element in the endElement(...)
method of your SAXParser class.

So long,
Thomas

Jul 17 '05 #2

P: n/a
Have a look at this article in Java Pro

http://www.fawcette.com/javapro/2003...s/javatecture/

(watch the wrap)

"Wes Batson" <ba****@aol.com> wrote in message
news:11**************************@posting.google.c om...
Hi, I am using SAX parsing and I need to get the information between
the tags. I have narrowed the problem down to my characters() method
in the DefaultHandler class. Here is my code:

public void characters(char[] ch, int start, int length) throws
SAXException{
String s = new String(ch, start, length);
Compnames[pos] = s;
}

Here is my xml file:

<?xml version="1.0"?>
<Data>
<coName>Company Name</coName>
<coAdd>Address</coAdd>
<coCity>City</coCity>
<coState>State</coState>
<coZIP>Zip</coZIP>
<coPhone>5555555555</coPhone>
<coEmail>Email</coEmail>
<coPicpth>Picture url</coPicpth>
</Data>

My startElement() method works fine so I know that the xml tags are
being parsed correctly. My problem is that int start is always being
set to 0, length is always set to 1, and char[] ch is an empty
string(" "). All examples I have seen online of the characters()
method seem to show that if I were to print the Compnames array then
it would show what is between the tags. As of right now it prints the
empty string. I am not sure why mine doesn't work the way others
doing the same thing have reported. My experience with XML is minimal
so maybe it is the xml file? Any help is appreciated.

Thanks in advance,

W2B


Jul 17 '05 #3

P: n/a
Thomas Fleige <NO******************@materna.de> wrote in message news:<bv**********@pentheus.materna.de>...
Hello Wes.

I had the same problem.
You should try to read the content of an element in the endElement(...)
method of your SAXParser class.

So long,
Thomas


How would I go about doing this? endElement only gives me access to
the name of the end tag. Where would I get the text inside the tags
from this method?

Thanks,

Wes
Jul 17 '05 #4

P: n/a
> being parsed correctly. My problem is that int start is always being
set to 0, length is always set to 1, and char[] ch is an empty
string(" "). All examples I have seen online of the characters()


I faced the same problem in my project.

I realized the answer is in the xml document instance.
If you have something like this:

<tag>
text content
</tag>

i.e. if you have "new_line", the first call to characters() gives you
the first \n. But as the parser removes the simbol so you have " ".
The next call gives you " text content" and so on...

Finally I solved the problem just putting tags near each-other:
<tag>text content</tag>
and I see you've already done so.

Then I wrote as follows:

public void characters(char[] ch, int start, int length)
throws SAXException
{
super.characters(ch, start, length);

String chrs = "";
for(int i=0; i<length; i++)
chrs += ch[start + i];

if(chrs.equals(""))
return;

...
}

Naturally you have to check that into a tag that requires text some
text there is!
But generally this check is implemented with either a schema or a DTD.

Regards.
Luca Paganelli
Jul 17 '05 #5

P: n/a
my approch is different

I hava a StringBuffer variable in the handler class and in function
characters() I just append the received characters to the StringBuffer.

Then when endElement() is called you can do anything you want with
the content of the StringBuffer.. maybe sb.toString().trim(); ;)

naturally in startElement you should reset the StringBuffer...

Best regards.

Luca Paganelli wrote:
being parsed correctly. My problem is that int start is always being
set to 0, length is always set to 1, and char[] ch is an empty
string(" "). All examples I have seen online of the characters()

I faced the same problem in my project.

I realized the answer is in the xml document instance.
If you have something like this:

<tag>
text content
</tag>

i.e. if you have "new_line", the first call to characters() gives you
the first \n. But as the parser removes the simbol so you have " ".
The next call gives you " text content" and so on...

Finally I solved the problem just putting tags near each-other:
<tag>text content</tag>
and I see you've already done so.

Then I wrote as follows:

public void characters(char[] ch, int start, int length)
throws SAXException
{
super.characters(ch, start, length);

String chrs = "";
for(int i=0; i<length; i++)
chrs += ch[start + i];

if(chrs.equals(""))
return;

...
}

Naturally you have to check that into a tag that requires text some
text there is!
But generally this check is implemented with either a schema or a DTD.

Regards.
Luca Paganelli


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.