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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:43:00+00:00 2026-05-26T08:43:00+00:00

I’m attempting to write a small library which will convert Java objects, through reflection,

  • 0

I’m attempting to write a small library which will convert Java objects, through reflection, into XML. I’ve got the majority of it working, but ran into an error when attempting to iterate through an array.

Here are the domain objects i’m using for testing:

In Company.java:

import java.util.List;

import com.google.common.collect.Lists;

public class Company
{
    public Employee employeeArray[];

    public Employee[] getEmployeeArray()
    {
        return employeeArray;
    }

    public void setEmployeeArray(Employee[] employeeArray)
    {
        this.employeeArray = employeeArray;
    }
}

In Employee.java:

public class Employee
{
    public String firstName;
    public String lastName;

    public Employee() {}

    public Employee(String firstName, String lastName)
    {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String getFirstName()
    {
        return firstName;
    }

    public void setFirstName(String firstName)
    {
        this.firstName = firstName;
    }

    public String getLastName()
    {
        return lastName;
    }

    public void setLastName(String lastName)
    {
        this.lastName = lastName;
    }
}

The core of the library (ObjectXMLWriter.java):

import java.io.StringWriter;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.util.List;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import com.falcondev.web.DOMFactory;
import com.google.common.collect.Lists;

public class ObjectXMLWriter
{
    private static final Logger logger = Logger.getLogger(ObjectXMLWriter.class);

    private String fileLocation;
    private Object object;
    private boolean shouldOverride;

    public ObjectXMLWriter(String fileLocation, Object object) {
        this.fileLocation = fileLocation;
        this.object = object;
        this.shouldOverride = false;
    }

    public ObjectXMLWriter(String fileLocation, Object object, boolean shouldOverride) {
        this(fileLocation, object);
        this.shouldOverride = shouldOverride;
    }

    public String getFileLocation()
    {
        return fileLocation;
    }

    public void setFileLocation(String fileLocation)
    {
        this.fileLocation = fileLocation;
    }

    public Object getObject()
    {
        return object;
    }

    public void setObject(Object object)
    {
        this.object = object;
    }

    public boolean isShouldOverride()
    {
        return shouldOverride;
    }

    public void setShouldOverride(boolean shouldOverride)
    {
        this.shouldOverride = shouldOverride;
    }

    public boolean saveObject() throws Exception {

        boolean saveSuccessful = false;
        Document document = createDocument();

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

        StringWriter stringWriter = new StringWriter();
        StreamResult result = new StreamResult(stringWriter);
        DOMSource source = new DOMSource(document);
        transformer.transform(source, result);
        String xmlString = stringWriter.toString();

        //print XML
        System.out.println("Here's the xml:\n\n" + xmlString);

        //TODO save XML file
        return saveSuccessful;
    }

    private Node createNode(Document document, Object object) throws Exception {
        Node node = document.createElement(getObjectClassName(object));
        logger.trace("NODE: " + node);

        if(node != null) {
            //create children nodes from object fields
            List<Field> fields = Lists.newArrayList(object.getClass().getFields());
            for(Field field: fields) {
                Object obj = field.get(object);
                logger.trace("OBJECT: " + obj);

                if(obj == null || !checkInstantiability(obj) || field.getType().isAssignableFrom(List.class) || field.getType().isArray()) {
                    logger.debug("ATTEMPTING TO CREATE NODE FOR FIELD: " + field.getName());
                    logger.debug("FIELD TYPE: " + field.getType());

                    //TODO add types as needed
                    String fieldValue = "";
                    if(List.class.isAssignableFrom(field.getType())) { //TODO check if object is iterable instead
                      //TODO figure out how to iterate through iterable's
                    }
                    else if(field.getType().isArray()) {
                        Object array = field.get(obj); //Fails here
                        int length = Array.getLength(array);
                        for (int i = 0; i < length; i++) {
                          System.out.println(Array.get(array, i));
                          node.appendChild(createNode(document, Array.get(array, i)));
                        }
                    }
                    else if(field.getType() == Class.class) {
                        fieldValue = obj.toString().replaceFirst("class ", "");
                    }
                    else {
                        fieldValue = obj.toString();
                    }
                    logger.debug("FIELD OBJECT VALUE: '" + fieldValue + "'");

                    //TODO check for annotation to choose whether to create element or attribute
                    Element element = document.createElement(field.getName());
                    element.setTextContent(fieldValue);
                    node.appendChild(element);
                }
                else {
                    logger.debug("ATTEMPTING TO CREATE OBJECT NODE FOR FIELD: " + field.getName());
                    node.appendChild(createNode(document, obj));
                }
            }
        }

        return node;
    }

    private Document createDocument() throws Exception {
        Document document = DOMFactory.create();
        if(checkInstantiability(object)) {
            Node rootNode = createNode(document, object);
            document.appendChild(rootNode);
        }
        else {
            logger.error("CANNOT SAVE UNINSTANTIABLE OBJECT. DOCUMENT MUST HAVE A ROOT NODE");
        }

        return document;
    }

    private String getObjectClassName(Object object) {
        return object.getClass().getSimpleName().toLowerCase();
    }

    private boolean checkInstantiability(Object object) {
        try
        {
            object.getClass().newInstance();
        }
        catch (InstantiationException exception)
        {
            return false;
        }
        catch (IllegalAccessException exception)
        {
            return false;
        }

        return true;
    }
}

And the Test driver (Test.java):

public class Test
{
    public static void main(String[] args) throws Exception
    {
        Employee[] employees = new Employee[3];
        employees[0] = new Employee("Tom", "C");
        employees[1] = new Employee("Paul", "E");
        employees[2] = new Employee("George", "A");
        Company company = new Company();
        company.setEmployeeArray(employees);

        new ObjectXMLWriter("resources/test2.xml", company).saveObject();
    }
}

The error I get when running this code is as follows:

Exception in thread "main" java.lang.IllegalArgumentException: Can not set [Lcom.falcondev.orm.test.Employee; field com.falcondev.orm.test.Company.employeeArray to [Lcom.falcondev.orm.test.Employee;
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:146)
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:150)
    at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:37)
    at sun.reflect.UnsafeObjectFieldAccessorImpl.get(UnsafeObjectFieldAccessorImpl.java:18)
    at java.lang.reflect.Field.get(Field.java:358)
    at com.falcondev.orm.ObjectXMLWriter.createNode(ObjectXMLWriter.java:116)
    at com.falcondev.orm.ObjectXMLWriter.createDocument(ObjectXMLWriter.java:149)
    at com.falcondev.orm.ObjectXMLWriter.saveObject(ObjectXMLWriter.java:74)
    at com.falcondev.orm.test.Test.main(Test.java:37)

I realize this is quite a bit of code to post. I tried the following example as shown in this post (http://stackoverflow.com/questions/2200399/iterating-over-arrays-by-reflection/2200493#2200493), and it worked fine, so there must be some subtle difference from the simple example and what I am doing in the above code.

Simple Test which works:

public class Test
{

    public static void main(String[] args) throws Exception
    {
        Employee[] employees = new Employee[3];
        employees[0] = new Employee("Tom", "C");
        employees[1] = new Employee("Paul", "E");
        employees[2] = new Employee("George", "A");
        Company company = new Company();
        company.setEmployeeArray(employees);

        Field field = company.getClass().getField("employeeArray");
        if (field.getType().isArray()) {
          Object array = field.get(company);
          int length = Array.getLength(array);
          for (int i = 0; i < length; i++) {
            System.out.println(Array.get(array, i));
          }
        }
    }
}

As for technologies info, for this example I am using: windows 7, eclipse 3.7, jdk 1.6.0_26, log4j 1.2.16, apache commons-lang3-3.0.1, and google guava 10.0

I’ve been trying to get this to work for the past few weeks, so any help would be greatly appreciated.

Edit:

For future reference and other’s use, the relevant code to fix the issue is as follows:

boolean shouldSaveFieldValue = true;

if(... || field.getType() == String.class) {
  ..    
  else if(field.getType().isArray()) {
    Object array = field.get(object);
    ...
    shouldSaveFieldValue = false;
  }

  if(shouldSaveFieldValue) {
    ...
    Element element = document.createElement(field.getName());
    element.setTextContent(fieldValue);
    node.appendChild(element);
  }
}
  • 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-05-26T08:43:01+00:00Added an answer on May 26, 2026 at 8:43 am

    First, I second the recommendation to use a library rather than rolling your own. I’ve had really good luck with JAXB which doesn’t event require a third party jar. To your question, I think you’re simply getting confused between the variable names “object” and “obj”:

    Company object = a Company
    Field field = [ Company.Employee[] employeeArray ]
    Employee[] obj = object.employeeArray
    

    field.get(obj) cannot be evaluated because the parameter is not of type Company. Here’s the relevant bits of code:

    private Node createNode(Document document, Object object) {
        ..
        List<Field> fields = Lists.newArrayList(object.getClass().getFields());
            for(Field field: fields) {
            ...
            Object obj = field.get(object);
            ...
            else if(field.getType().isArray()) {
                Object array = field.get(obj); //Fails here
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I am trying to understand how to use SyndicationItem to display feed which is
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am trying to loop through a bunch of documents I have to put
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

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.