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 13 of 13

Thread: How to parse an XML having same tag.

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    13
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default How to parse an XML having same tag.

    How to parse an xml having same tag as given below :

    <number>
    <value>12</value>
    <value>14</value>
    </number>

    If it needs to use either Vector or ArrayList then please let me know how to do it.

    If possible please parse it using Digester.

    Thanks in advance.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,360
    My Mood
    Hungover
    Thanks
    144
    Thanked 626 Times in 537 Posts

    Default Re: How to parse an XML having same tag.

    What have you tried? Where are you stuck? What did google tell you? How do you want to parse this? As a list of ints? As a single Number Object containing several ints? Something else?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    1bun100 (February 21st, 2012)

  4. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    13
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: How to parse an XML having same tag.

    Thanks for your quick reply.

    I need to use either Vector or Arraylist to hold the values.
    I am creating a class that have getter and setter methods for that value tag. But I don't know how to create getter and setter methods as there are two tags with same name.
    I just want to know the logic.

  5. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,360
    My Mood
    Hungover
    Thanks
    144
    Thanked 626 Times in 537 Posts

    Default Re: How to parse an XML having same tag.

    How are you parsing it now?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. The Following User Says Thank You to KevinWorkman For This Useful Post:

    1bun100 (February 21st, 2012)

  7. #5
    Junior Member
    Join Date
    Aug 2011
    Posts
    13
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: How to parse an XML having same tag.

    Using Digester. I have already tried using other xml that have different tags.

  8. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,360
    My Mood
    Hungover
    Thanks
    144
    Thanked 626 Times in 537 Posts

    Default Re: How to parse an XML having same tag.

    I am not familiar with Digester. If it's a requirement to use Digester, you might be best off looking for a forum specific to that product.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. The Following User Says Thank You to KevinWorkman For This Useful Post:

    1bun100 (February 21st, 2012)

  10. #7
    Junior Member
    Join Date
    Aug 2011
    Posts
    13
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: How to parse an XML having same tag.

    If you know JAXB (using @xmlElement), can you help me.
    I just need to know how to set those values to either Vector or Arraylist object using getter and setter methods.

  11. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,360
    My Mood
    Hungover
    Thanks
    144
    Thanked 626 Times in 537 Posts

    Default Re: How to parse an XML having same tag.

    Well, I'm still not sure how exactly you want this to be parsed. But assuming you want a list of values, I would use JAXB by defining a schema that has a maxOccurs="2" or "unlimited", so the classes generated by JAXB would contain a List of values instead of a single int. Not sure whether that helps you though.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  12. The Following User Says Thank You to KevinWorkman For This Useful Post:

    1bun100 (February 21st, 2012)

  13. #9
    Junior Member
    Join Date
    Aug 2011
    Posts
    13
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: How to parse an XML having same tag.

    How should I do if I don't want to use schema ?

  14. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,360
    My Mood
    Hungover
    Thanks
    144
    Thanked 626 Times in 537 Posts

    Default Re: How to parse an XML having same tag.

    Quote Originally Posted by 1bun100 View Post
    How should I do if I don't want to use schema ?

    That I'm not sure about. Can't you just change it to a List?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  15. The Following User Says Thank You to KevinWorkman For This Useful Post:

    1bun100 (February 24th, 2012)

  16. #11
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: How to parse an XML having same tag.

    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  17. The Following User Says Thank You to JavaPF For This Useful Post:

    1bun100 (February 24th, 2012)

  18. #12
    Junior Member
    Join Date
    Aug 2011
    Posts
    13
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: How to parse an XML having same tag.

    I have got a solution for this thread. I have posted it here:

    How to parse an XML having same tag? - Java | DaniWeb

    If any one tried different way using digester please update it here.

  19. #13
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,360
    My Mood
    Hungover
    Thanks
    144
    Thanked 626 Times in 537 Posts

    Default Re: How to parse an XML having same tag.

    It would have been courteous of you to tell us you had crossposted your question on other forums. Please read this: The problems with cross-posting
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Need help to parse text files.
    By Num1701 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: November 18th, 2011, 01:11 PM
  2. [SOLVED] parse txt file
    By maliv in forum File I/O & Other I/O Streams
    Replies: 16
    Last Post: November 17th, 2010, 02:54 PM
  3. How to parse an object to and from XML using JAXB
    By Json in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: January 20th, 2010, 02:42 AM
  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