Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: How to read an integer attribute using dom.element

  1. #1
    Member
    Join Date
    Apr 2014
    Posts
    31
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default How to read an integer attribute using dom.element

    I don't know how to read the attribute maxLenth. The way in the image I have done drive me to the error below.!

    Piece of XSL:

    <?xml version="1.0" encoding="UTF-8"?>

    <c:message xmlns:c="ictt"><c:de format="B" lengthField="0" name="BIT MAP, PRIMARY" number="000"/><c:de format="B" lengthField="0" maxLength="008" minLength="008" name="BIT MAP, SECONDARY" number="001" subFields="00"/>


    Piece of Java Code:
    int length = Integer.parseInt(spec.getAttribute("maxLength"));

    [5/13/14 14:52:19:497 CDT] 00000028 SystemErr R java.lang.NumberFormatException: For input string: ""

    [5/13/14 14:52:19:497 CDT] 00000028 SystemErr R at java.lang.NumberFormatException.forInputString(Num berFormatException.java:63)

    [5/13/14 14:52:19:497 CDT] 00000028 SystemErr R at java.lang.Integer.parseInt(Integer.java:502)

    [5/13/14 14:52:19:497 CDT] 00000028 SystemErr R at java.lang.Integer.parseInt(Integer.java:531)









    maxlenth.jpg
    Last edited by DemeCarv; May 13th, 2014 at 03:18 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to read an integer attribute using dom.element

    Please post the code here that you have questions about. Be sure to wrap the code in code tags.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: How to read an integer attribute using dom.element

    Quote Originally Posted by DemeCarv View Post
    Piece of XSL:

    <?xml version="1.0" encoding="UTF-8"?>

    <c:message xmlns:c="ictt"><c:de format="B" lengthField="0" name="BIT MAP, PRIMARY" number="000"/><c:de format="B" lengthField="0" maxLength="008" minLength="008" name="BIT MAP, SECONDARY" number="001" subFields="00"/>


    Piece of Java Code:
    int length = Integer.parseInt(spec.getAttribute("maxLength"));
    maxLength is an attribute of the c:de element, and the above call (where spec is an Element object) is the correct call to do this. However from the XML (not XSL) fragment above you'd see that the first c:de element does not have the maxLength attribute. Also, the image you attached to your post is too small to make out what spec is. (Btw, please paste your code into your post as text, wrapped in [code] tags, and not as a screenshot.)

    Quote Originally Posted by DemeCarv View Post
    [5/13/14 14:52:19:497 CDT] 00000028 SystemErr R java.lang.NumberFormatException: For input string: ""
    What we know from the exception message that the call to getAttribute() returned an empty string. The API doc for this method states,

    "Returns:
    The Attr value as a string, or the empty string if that attribute does not have a specified or default value."

    Therefore it is possible that spec is referring to an element that does not have the maxLength attribute, just like the first c:de element.

    You can confirm this by using hasAttribute(String name), or by printing out the element, e.g., via an identity transform:
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.transform(new DOMSource(spec), new StreamResult(System.out));

    If you're still unable to solve this problem, we'll need more than the 1 line of code and a tiny screenshot.

Similar Threads

  1. [SOLVED] Read String to List<Integer> and back
    By Cornix in forum Java Theory & Questions
    Replies: 2
    Last Post: February 14th, 2014, 07:15 PM
  2. Read A File and Store Values into a 2-Dimensional Integer Array?
    By Kimimaru in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 9th, 2011, 09:13 PM
  3. how to read an integer of DOUBLE datatype with type casting
    By amr in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 14th, 2010, 03:03 PM
  4. How to read an XML document in Java with DOM Parse?
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: May 20th, 2008, 07:04 AM
  5. How to read an XML document in Java with DOM Parse?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: May 20th, 2008, 07:04 AM

Tags for this Thread