Attributes always empty when parsing XML documents

Discussion in 'Java' started by Andy Carson, Nov 16, 2004.

  1. Andy Carson

    Andy Carson Guest

    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".
    Andy Carson, Nov 16, 2004
    #1
    1. Advertising

  2. On Tue, 16 Nov 2004 15:41:36 +0100, Andy Carson <> 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
    William Brogden, Nov 16, 2004
    #2
    1. Advertising

  3. Andy Carson

    Andy Carson Guest

    > 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>
    Andy Carson, Nov 16, 2004
    #3
  4. 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
    TechBookReport, Nov 16, 2004
    #4
  5. Andy Carson

    Guest

    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_ + "'") ;
    }
    }
    , Nov 16, 2004
    #5
  6. Andy Carson

    Andy Carson Guest

    > No attributes in there. An attribute (date) would look like this:
    >
    > <PersonResultSet date="04/11/16">


    Thanks a lot!
    Andy Carson, Nov 17, 2004
    #6
  7. Andy Carson

    Andy Carson Guest

    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. )
    Andy Carson, Nov 17, 2004
    #7
  8. 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
    TechBookReport, Nov 17, 2004
    #8
  9. Andy Carson

    Guest

    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).
    , Nov 17, 2004
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Soren Kuula

    Empty documents always DTD-valid?

    Soren Kuula, Sep 20, 2005, in forum: XML
    Replies:
    2
    Views:
    508
    Richard Tobin
    Sep 20, 2005
  2. Lukas
    Replies:
    3
    Views:
    780
    spiff
    Nov 10, 2005
  3. Replies:
    1
    Views:
    468
    Juan T. Llibre
    Oct 18, 2006
  4. Replies:
    2
    Views:
    294
    Max M
    Apr 26, 2007
  5. PL
    Replies:
    2
    Views:
    216
    Brian McCauley
    Dec 14, 2004
Loading...

Share This Page