Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Attributes always empty when parsing XML documents

Reply
Thread Tools

Attributes always empty when parsing XML documents

 
 
Andy Carson
Guest
Posts: n/a
 
      11-16-2004
I'm writing a small app that needs to parse XML-files. However, in the
method

final public void startElement( final String namespace, final String
localname, final String type, final org.xml.sax.Attributes attributes )
throws org.xml.sax.SAXException
{ (...) }

the attributes is always empty. Why is this?

The final public void characters( final char[] ch, final int start, final
int len ) seems to show correct behavior, i e the file seems to be parsed
"correctly".




 
Reply With Quote
 
 
 
 
William Brogden
Guest
Posts: n/a
 
      11-16-2004
On Tue, 16 Nov 2004 15:41:36 +0100, Andy Carson <(E-Mail Removed)> wrote:

> I'm writing a small app that needs to parse XML-files. However, in the
> method
>
> final public void startElement( final String namespace, final String
> localname, final String type, final org.xml.sax.Attributes attributes )
> throws org.xml.sax.SAXException
> { (...) }
>
> the attributes is always empty. Why is this?


My guess is that your elements don't have any attributes.

Bill
 
Reply With Quote
 
 
 
 
Andy Carson
Guest
Posts: n/a
 
      11-16-2004
> My guess is that your elements don't have any attributes.

I believe I have misunderstood the whole idea and the concepts behind the
concepts though I have googled a lot.

Say that I have the the XML-file below.

1) Are there any attributes in this file?
2) How do I retrieve the values in different PersonResultRecord effectively?

<PersonComposeResponse>
<DeliveryId>881718</DeliveryId>
<PersonResultSet>
<PersonResultRecord>
<ID>1043487240</ID>
<FirstName>Irena</FirstName>
<MiddleName></MiddleName>
<LastName>Hansson</LastName>
</PersonResultRecord>
<PersonResultRecord>
<ID>1043487240</ID>
<FirstName>Irena</FirstName>
<MiddleName></MiddleName>
<LastName>Hansson</LastName>
</PersonResultRecord>
</PersonResultSet>
</PersonComposeResponse>


 
Reply With Quote
 
TechBookReport
Guest
Posts: n/a
 
      11-16-2004
Andy Carson wrote:
>>My guess is that your elements don't have any attributes.

>
>
> I believe I have misunderstood the whole idea and the concepts behind the
> concepts though I have googled a lot.
>
> Say that I have the the XML-file below.
>
> 1) Are there any attributes in this file?
> 2) How do I retrieve the values in different PersonResultRecord effectively?
>
> <PersonComposeResponse>
> <DeliveryId>881718</DeliveryId>
> <PersonResultSet>
> <PersonResultRecord>
> <ID>1043487240</ID>
> <FirstName>Irena</FirstName>
> <MiddleName></MiddleName>
> <LastName>Hansson</LastName>
> </PersonResultRecord>
> <PersonResultRecord>
> <ID>1043487240</ID>
> <FirstName>Irena</FirstName>
> <MiddleName></MiddleName>
> <LastName>Hansson</LastName>
> </PersonResultRecord>
> </PersonResultSet>
> </PersonComposeResponse>
>
>

No attributes in there. An attribute (date) would look like this:

<PersonResultSet date="04/11/16">


Pan

===============================================
TechBookReport http://www.techbookreport.com
 
Reply With Quote
 
shakahshakah@gmail.com
Guest
Posts: n/a
 
      11-16-2004
With a SAX parser you have to accumulate the element's content
presented in the characters(...) callback.

Something like:

public static class MyParser
extends org.xml.sax.helpers.DefaultHandler {

private StringBuffer sbCharBuffer_ ;

public void characters(char [] ach, int nStart, int nLength) {
if(null==sbCharBuffer_) {
StringBuffer sbTemp = new StringBuffer(nLength + 1) ;
sbTemp.append(ach, nStart, nLength) ;
System.out.println("MyParser: ignoring " + nLength + " characters
(" + sbTemp.toString() + ")") ;
}
else {
sbCharBuffer_.append(ach, nStart, nLength) ;
}
}

public void startElement(String sURI, String sLocalName, String
sQName, org.xml.sax.Attributes attrs) {
System.out.println("startElement('" + sQName + "')") ;
sbCharBuffer_ = new StringBuffer(500) ;
}

public void endElement(String sURI, String sLocalName, String sQName)
{
System.out.println("endElement('" + sQName + "'): content was '" +
sbCharBuffer_ + "'") ;
}
}

 
Reply With Quote
 
Andy Carson
Guest
Posts: n/a
 
      11-17-2004
> No attributes in there. An attribute (date) would look like this:
>
> <PersonResultSet date="04/11/16">


Thanks a lot!



 
Reply With Quote
 
Andy Carson
Guest
Posts: n/a
 
      11-17-2004
Thanks for the code sample!

Are there more effective parsers? The code sample you provided suggests that
the reading of the values is a bit messy ...

(I'm very found of the way you handle XML in the .Net-environment. In
C#/.Net you just define a schema (sort of a data set) and then the parser
just load that schema with the correct values automatically. )


 
Reply With Quote
 
TechBookReport
Guest
Posts: n/a
 
      11-17-2004
Andy Carson wrote:
> Thanks for the code sample!
>
> Are there more effective parsers? The code sample you provided suggests that
> the reading of the values is a bit messy ...
>
> (I'm very found of the way you handle XML in the .Net-environment. In
> C#/.Net you just define a schema (sort of a data set) and then the parser
> just load that schema with the correct values automatically. )
>
>

Personally I much prefer JDOM (http://www.jdom.org). It's cleaner and
much more intuitive IMHO.

Pan
================================================
TechBookReport http://www.techbookreport.com
 
Reply With Quote
 
shakahshakah@gmail.com
Guest
Posts: n/a
 
      11-17-2004
A DOM parser is usually easier to use, but be aware that it sucks in
the whole document before giving you a look at it (which could cause
problems when processing very large documents).

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Parsing HTML/XML documents pabloski@giochinternet.com Python 2 04-26-2007 07:57 PM
Different results parsing a XML file with XML::Simple (XML::Sax vs. XML::Parser) Erik Wasser Perl Misc 5 03-05-2006 10:09 PM
Altova Mapforce - xml 2 xml map: empty elements output although input element is not empty Lukas XML 3 11-10-2005 02:25 PM
Empty documents always DTD-valid? Soren Kuula XML 2 09-20-2005 09:16 PM
[XML::XSLT] empty result while parsing xml file PL Perl Misc 2 12-14-2004 10:24 PM



Advertisments