Results 1 to 10 of 10
  1. #1
    alexandra12 is offline Member
    Join Date
    Jul 2011
    Posts
    34
    Rep Power
    0

    Default error for empty element in sax parser

    Is their a way to get the default handler in Sax Parser to ignore empty elements and continue with the parsing ?
    I am getting the error :
    java.lang.NumberFormatException: empty String
    at sun.misc.FloatingDecimal.readJavaFormatString(Floa tingDecimal.java:1038)
    at java.lang.Float.valueOf(Float.java:400)

    It is happening at the endElement

    Thanks in advance

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,851
    Rep Power
    19

    Default Re: error for empty element in sax parser

    What is the definition of the element it is trying to parse?
    If it's a number and the element is supplied then it can't be blank.
    The W3C XML Schema document covers this.

    At least, that's how I've always understood it...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    alexandra12 is offline Member
    Join Date
    Jul 2011
    Posts
    34
    Rep Power
    0

    Default Re: error for empty element in sax parser

    Quote Originally Posted by Tolls View Post
    What is the definition of the element it is trying to parse?
    If it's a number and the element is supplied then it can't be blank.
    The W3C XML Schema document covers this.

    At least, that's how I've always understood it...
    Thanks for your response tolls. I will take a look at the link you sent. It is a Float as the values have decimal points. I also cannot guarantee that the </num> element wont be empty as I have no control over it.

    Maybe I should parse the float as a string and that might overcome the issue ?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,851
    Rep Power
    19

    Default Re: error for empty element in sax parser

    Then it's not XML.
    If the XSD for that file defines the <num> element as a float (which I'm guessing it does as that's how the parser seems to be treating it) then, as far as I am aware, it can't be blank. Unless it is marked (in the xsd) as xsi:nil true.

    Now, if this is just something you are parsing manually then you can do whatever you want.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    alexandra12 is offline Member
    Join Date
    Jul 2011
    Posts
    34
    Rep Power
    0

    Default Re: error for empty element in sax parser

    I am just getting a little bit confused here.

    Initially I had the <num> element set as an int and it would not parse for me.

    public void setNum(int num) {
    this.num =(num);

    }

    if(qName.equalsIgnoreCase("num")){
    // Tmp.setNum(Integer.toString(tmpString));
    // Tmp.setNum(Integer.valueOf(tmpString).intValue());
    }

    On looking at some of the values in the <num> element I noticed that it had a decimal point.
    I then changed the setNum method to a float instead of an int - and I was able to get data back with the code

    if(qName.equalsIgnoreCase("num")){
    Tmp.setNum(Float.valueOf(tmpString).floatValue());
    }

    Now looking at the documentation for the data on the server it says
    Element <num>
    The num element contains the value of the user database object which has been configured as a type number
    Contains
    The element contains a signed integer.
    May be included in elements
    object
    <object id="P1O4" type="num" status="ok" updated="2009-01-22T12:15:00">
    <label>Hours Run</label>
    <num min="0" max="60000">123</num>
    <units>Hrs</units>
    </object>


    In the above documentation it says that it is optional - which is why the element is empty - which should be OK - except I am not able to parse it when empty.
    But when I look at the values I see data like

    <num>1436.79</num>
    <units>hrs</units>
    </object>
    <object id="P47" type="num" status="ok" updated="2012-07-29T23:06:00">
    <label>Damper</label>
    <num>0</num>
    <units>%</units>

    Here is the code that I have been trying to get to work - with setting the get method to either an int or a float

    Am I missing something simple here that I am just not getting ???

    Sorry for the trail of questions !

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,851
    Rep Power
    19

    Default Re: error for empty element in sax parser

    Right, so this is your own parser.
    It would have helped had you said that.

    The parseInt will throw a parse exception since (as you say) these are not ints.
    Now I would argue that they have no real definition for these elements...where's the xsd?

    Anyway, that aside...check whether 'tmpString' isEmpty().
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    alexandra12 is offline Member
    Join Date
    Jul 2011
    Posts
    34
    Rep Power
    0

    Default Re: error for empty element in sax parser

    Sorry about not saying I was using my own SaxParser

    Their is a sample XSD stored in the documentation for the tool that I am trying to get the data from.

    <xs:element name="num">
    <xs:complexType>
    <xs:simpleContent>
    <xs:extension base="xs:integer">
    <xs:attribute name="max" type="xs:integer" default="0" />
    <xs:attribute name="min" type="xs:integer" default="0" />
    </xs:extension>
    </xs:simpleContent>
    </xs:complexType>
    </xs:element>

    using the code

    if(qName.equalsIgnoreCase("num")){

    if((tmpString).isEmpty()){
    System.out.println("is empty");
    }
    else System.out.println("not empty");
    // Tmp.setNum(Integer.toString(tmpString));
    // Tmp.setNum(Integer.valueOf(tmpString).intValue());
    Tmp.setNum(Float.valueOf(tmpString).floatValue());
    }
    The tmpString is not empty until it reaches the empty num element. Below is the output to the console.


    <units>
    </units>
    </object>
    <object>
    in the start object element
    <label>
    </label>
    <num>
    </num>
    not empty
    java.lang.NumberFormatException: empty String


    I have commented out the line of code that prints the characters when doing this test

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

    tmpString=new String(ch, start, length);

    //this prints out all the text node values but not the attributes
    // System.out.println(new String(ch,start,length));
    // System.out.println(tmpString);

    }

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,851
    Rep Power
    19

    Default Re: error for empty element in sax parser

    Hmm.
    Based on that xsd then the XML you received is invalid (as you suspect).

    Anyway, that result is not possible from that code.
    If tmpString is empty then you will get both the 'is empty' text and the exception.
    You will not get the 'not empty' followed by that exception.
    So that exception is coming from somewhere else.

    You need to print the stack trace and find out exactly where that exception is coming from.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: error for empty element in sax parser

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  10. #10
    alexandra12 is offline Member
    Join Date
    Jul 2011
    Posts
    34
    Rep Power
    0

    Default Re: error for empty element in sax parser

    Thanks for all your help Tolls. I really appreciate it.

    I ended up parsing the </num> element as a string and it is parsing OK with no errors !

Similar Threads

  1. Why is this List empty ?
    By fatabass in forum New To Java
    Replies: 3
    Last Post: 02-06-2012, 12:38 AM
  2. Replies: 0
    Last Post: 07-12-2010, 11:57 AM
  3. Empty FileInputStream..
    By dudejonne in forum New To Java
    Replies: 5
    Last Post: 11-08-2009, 08:21 PM
  4. empty arrays.
    By circuspeanuts in forum New To Java
    Replies: 11
    Last Post: 04-06-2009, 07:08 PM
  5. BufferedReader empty
    By Peter in forum Advanced Java
    Replies: 2
    Last Post: 07-02-2007, 06:52 PM

Posting Permissions

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