×

Discussion Board

Results 1 to 5 of 5

Thread: J2ME SAX Parser

  1. #1
    Regular Contributor
    Join Date
    Dec 2008
    Posts
    106

    J2ME SAX Parser

    Hi guys,

    I am trying to parse a simple xml file with SAXParser(jsr 172):
    Code:
    <tag1>value1</tag1>
    <tag2>value2</tag2>
    <tag3>value3</tag3>
    It always throws me SAXParseException every time I want to parse it. But works OK only if I have 1 tag and 1 value in my xml file.

    I couldn't find enough good examples or tutorials on how to use it, but here is what I have so far:
    Code:
    saxFactory = SAXParserFactory.newInstance();
    parser = saxFactory.newSAXParser();
    handler = new SAXHandler();
    parser.parse(is, handler);
    Where handler is my extended version of DefaultHandler with following functions:

    Code:
    // Opening tag
    public void startElement(String uri, String localName,
                             String qName, Attributes attrs)
                                            throws SAXParseException {
            tag = qName;
            System.out.println(tag);
    }
    
    // Value
    public void characters(char[] ch, int start, int length)
                                            throws SAXParseException {
            value = new String(ch, start, length);
            System.out.println(value);
    }
    
    // Closing tag
    public void endElement(String uri, String localName,
                             String qName, Attributes attrs)
                                            throws SAXParseException {
            hashTable.put(tag, value);
    }
    When I try to parse the xml file above it prints:
    Code:
    tag1
    value1
    org.xml.sax.SAXParseException: 
            at org.xml.sax.helpers.DefaultHandler.fatalError(+1)
            at com.sun.ukit.jaxp.Parser.panic(+18)
            at com.sun.ukit.jaxp.Parser.parse(+186)
            at com.sun.ukit.jaxp.Parser.parse(+47)
            at com.sun.ukit.jaxp.Parser.parse(+31)
            ...
    When I tried to debug it I found out that it never goes to endElement() function, throws exception before it.

    Please help,
    Thanks.

  2. #2
    Regular Contributor
    Join Date
    May 2008
    Location
    Budapest, Hungary
    Posts
    84

    Re: J2ME SAX Parser

    Quote Originally Posted by Unit1ed View Post
    I am trying to parse a simple xml file with SAXParser(jsr 172):
    Code:
    <tag1>value1</tag1>
    <tag2>value2</tag2>
    <tag3>value3</tag3>
    It always throws me SAXParseException every time I want to parse it. But works OK only if I have 1 tag and 1 value in my xml file.
    I think I've found your problem right here. XML files must have only one root node, that's why your code works with one tag and explodes with three. This is simply a not well-formed XML.

    So add a root element around your tags, like this:

    Code:
    <testdata>
        <tag1>value1</tag1>
        <tag2>value2</tag2>
        <tag3>value3</tag3>
    </testdata>
    You could also include an XML header to specify the character encoding, like this:

    Code:
    <?xml version="1.0" encoding="UTF-8" ?>
    <testdata>
        <tag1>value1</tag1>
        <tag2>value2</tag2>
        <tag3>value3</tag3>
    </testdata>
    HTH,
    Zsolt

  3. #3
    Regular Contributor
    Join Date
    Dec 2008
    Posts
    106

    Re: J2ME SAX Parser

    Quote Originally Posted by VaradiZsolt View Post
    This is simply a not well-formed XML.
    Thanks a lot. It was very stupid of me to forget this. It works fine now =)

    But endElement() function is not called at all.
    Last edited by Unit1ed; 2009-03-08 at 05:42.

  4. #4
    Regular Contributor
    Join Date
    May 2008
    Location
    Budapest, Hungary
    Posts
    84

    Re: J2ME SAX Parser

    Quote Originally Posted by Unit1ed View Post
    But endElement() function is not called at all.
    That's because you aren't overriding the base class' EndElement method, there's a mistake in your method signature.

    Here:
    Code:
    public void endElement(String uri, String localName,
                             String qName, Attributes attrs)
    There's no Attributes argument in the endElement call. It wouldn't even make sense, because SAX doesn't remember the start tag's attributes by design.

    Change it to this:
    Code:
    public void endElement(String uri, String localName,
                             String qName)

  5. #5
    Regular Contributor
    Join Date
    Dec 2008
    Posts
    106

    Re: J2ME SAX Parser

    Quote Originally Posted by VaradiZsolt View Post
    There's no Attributes argument in the endElement call. It wouldn't even make sense, because SAX doesn't remember the start tag's attributes by design.
    Thanks a lot for your help, I feel so embarrassed right now. I'll try to be more careful next time when I read javadocs =)

Similar Threads

  1. J2ME or C++ for VoIP on Mobile
    By sandeepkumar03 in forum VoIP
    Replies: 4
    Last Post: 2009-02-02, 10:28
  2. Calling a native c or c++ application from J2ME
    By satyarajasekhar in forum Mobile Java Networking & Messaging & Security
    Replies: 5
    Last Post: 2009-01-02, 12:10
  3. J2ME or C++ for VoIP on Mobile Phone
    By sandeepkumar03 in forum Mobile Java Media (Graphics & Sounds)
    Replies: 2
    Last Post: 2008-11-13, 20:14
  4. Acessing voice channel using J2ME
    By diegomello in forum VoIP
    Replies: 1
    Last Post: 2008-10-21, 15:57
  5. Can J2ME be used for sending MMS Message ?
    By seno_ap in forum Mobile Java General
    Replies: 1
    Last Post: 2006-11-24, 14:36

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×