Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8657517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:32:24+00:00 2026-06-12T15:32:24+00:00

From some library, I’m getting such an input string: <link> <name>button1</name> <target>there</target> </link> <link>

  • 0

From some library, I’m getting such an input string:

<link>
    <name>button1</name>
    <target>there</target>
</link>
<link>
    <name>button2</name>
    <target>there2</target>
</link>

(note that this is not an XML document as it has no root) and I have this class:

@XmlRootElement(name = "link")
public class TableTagLinkElement {
    private String name;
    private String target;

    // getters and setters
}

How can I unmarshall this easily into a list of TableTagLinkElements, in a generic way such that I could implement such a method:

public <T> List<T> parseCollection(String xmlString, Class<T> rootClass)

i.e., without any previous knowledge of the TableTagLinkElement class or of the name of the <link> tag?

I know about solutions that create a wrapper class with a list, but I think they’re not applicable here, are they?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T15:32:25+00:00Added an answer on June 12, 2026 at 3:32 pm

    If you can wrap a <root>...</root> element around the complete XML string, then you could create an XMLStreamReader reading from that string, then loop over the reader, unmarshalling each link as you go. For example (exception handling omitted)

    public <T> List<T> parseCollection(String xmlString, Class<T> rootClass) {
      XMLInputFactory inFac = XMLInputFactory.newFactory();
      XMLStreamReader reader = inFac.createXMLStreamReader(
                        new StringReader("<root>" + xmlString + "</root>"));
      reader.nextTag(); // move to the <root> tag
      reader.nextTag(); // move to the first child
      List<T> list = new ArrayList<T>();
      while(reader.getEventType() == XMLStreamConstants.START_ELEMENT) {
        list.add(declaredType.cast(unmarshaller.unmarshal(reader)));
    
        // unmarshal leaves the reader pointing at the event *after* the
        // closing tag, not the END_ELEMENT event itself, so we can't just
        // do nextTag unconditionally.  We may already be on the next opening
        // tag or the closing </root> but we might need to advance if there
        // is whitespace between tags
        if(reader.getEventType() != XMLStreamConstants.START_ELEMENT &&
           reader.getEventType() != XMLStreamConstants.END_ELEMENT) {
          reader.nextTag();
        }
      }
      reader.close();
      return list;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using a validation library that removes some common XSS attacks from the input
Is there some library out there that can figure out if a given string
I have to use class Matrix4x4 from some 3rd party library, and I need
I want to copy some functions from OpenCV library to my embedded application. Rewriting
I'm having some trouble with c code from a library that I use with
Im a getting errors from some code developed in Java that want to use
I'm trying to use some library code written in scala from a java program.
Is there some library or generator that I can use to generate multiple templated
Is there a public API from some big company to get my public ip
I've been getting reports from some visitors of one of my web sites that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.