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 8272883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:16:52+00:00 2026-06-08T07:16:52+00:00

From my simple XML below, I want to create an instance of SomeApplication with

  • 0

From my simple XML below, I want to create an instance of SomeApplication with a collection of Field instances where each Field instance to represent a child element of <some-application>. The Field instances would have two String properties, name and value which are assigned from the node name and node value respectively.

XML input:

<some-application>
    <field1>foo</field1>
    <field2>bar</field2>
</some-application>

Java types:

public class Field {

    private String name;   
    private String value;

    public Field(String name, String value) {
        this.name = name;
        this.value = value;
    }

    public String getName(){
        return name;
    }

    public String getValue(){
        return value;
    }
}

public class SomeApplication {

    private List<Field> fields;

    public SomeApplication(){
        fields = new ArrayList<Field>();
    }

    public void addField(Field field){
        fields.add(field);
    }
}

What would be the best strategy for converting field elements (children of <some-application>) to Field instances?

Please note that I’ve simplified the requirement for the sake of brevity.

Thanks in advance.

  • 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-08T07:16:53+00:00Added an answer on June 8, 2026 at 7:16 am

    I’ve managed to achieve it using a custom converter – SomeApplicationConverter. In my custom converter, while unmarshalling, for every child node I encounter, I create my Field type instance using the node name and value. Below is my custom converter

    public class SomeApplicationConverter implements Converter {
    
        @Override
        @SuppressWarnings("unchecked")
        public boolean canConvert(Class type) {
            if (type.equals(SomeApplication.class)) {
                return true;
            }
            return false;
        }
    
        @Override
        public void marshal(Object source, HierarchicalStreamWriter writer,
                MarshallingContext context) {
            throw new UnsupportedOperationException(
                    "Marshalling is not supported yet");
        }
    
        @Override
        public Object unmarshal(HierarchicalStreamReader reader,
                UnmarshallingContext context) {
            SomeApplication application = new SomeApplication();
            while (reader.hasMoreChildren()) {
                reader.moveDown();
                String name = reader.getNodeName();
                String value = reader.getValue();
                Field field = new Field(name, value);
                application.addField(field);
                reader.moveUp();
            }
            return application;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to scrape a list of facts from simple website. Each one of
I'm not able to get html from a very simple XML file as below.
The XML file I want to extract data from looks as below: `<groups> <group>approved</group>
I have a simple problem with XML got from database. Now, this XML can
I have a pretty simple Linq to XML query: var q = from c
I have written simple code to get content from xml file to php. $xml
I’m making a simple LOB app which loads data from an XML file and
I want to create a list out of an xml file where only values
This is pretty simple and straightforward. I want to throw a 503 error from
Simple requirement: I want to store a flat unchanged XML strings into a MySQL-DB

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.