Attributes always empty when parsing XML documents

A

Andy Carson

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".
 
W

William Brogden

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
 
A

Andy Carson

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>
 
T

TechBookReport

Andy said:
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
 
S

shakahshakah

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_ + "'") ;
}
}
 
A

Andy Carson

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. )
 
T

TechBookReport

Andy said:
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
 
S

shakahshakah

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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top