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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:25:11+00:00 2026-06-15T00:25:11+00:00

I have XML in this format: <element1> some inner text <child1>11</child1> <child2>True</child2> </element1> And

  • 0

I have XML in this format:

<element1>
     some inner text
     <child1>11</child1>
     <child2>True</child2>
</element1>

And I try to map this XML on object by simple framework:

@Element(name = "element1")
public static class Something {

    @Element(required=false)
    public int child1;

    @Element(required=false)
    public boolean child2;

}

I can’t change the XML and I need to map also value “some inner text” on some field in class. Unfortunatelly I don’t know how to do that (and if it is possible). I tried @Text annotation but it can’t be used together with @Element in one class.

Thanks for any help.

  • 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-15T00:25:13+00:00Added an answer on June 15, 2026 at 12:25 am

    EDIT: You can actually do this with version 2.6.9 and up. Use the following annotations, note the @Text annotation can be used with @ElementListUnion.

    @Root
    public static class Something {
    
        @Text 
        @ElementListUnion({
           @ElementList(entry="child1", type=Integer.class, inline=true),
           @ElementList(entry="child2", type=Boolean.class, inline=true)
        })
        public List<Object> values;
    }
    

    This can only be done as a hack job, by default simple does not support free text as it does not map consistently to an object. Here is how you can do it, however its probably not worth it unless this is a special case within a large file of cases that work well in Simple.

    package org.simpleframework.xml.convert;
    
    import java.io.StringReader;
    import java.lang.reflect.Constructor;
    import java.lang.reflect.Field;
    
    import org.simpleframework.xml.Root;
    import org.simpleframework.xml.ValidationTestCase;
    import org.simpleframework.xml.core.Persister;
    import org.simpleframework.xml.stream.InputNode;
    import org.simpleframework.xml.stream.NodeBuilder;
    import org.simpleframework.xml.stream.OutputNode;
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    
    public class HackJobToGrabFloatingTextTest extends ValidationTestCase {
    
       private static final String SOURCE =
       "<element1>\n"+
       "   some inner text\n"+
       "   <child1>11</child1>\n"+
       "   <child2>True</child2>\n" +
       "</element1>";
    
       private static class SomethingConverter implements Converter<Something> {
    
          public Something read(InputNode node) throws Exception {
             Object source = node.getSource();
             Element element = (Element)source;
             NodeList elements = element.getChildNodes();
             String child1 = null;
             String child2 = null;
             String text = "";
             for(int i = 0; i < elements.getLength(); i++) {
                Node next = elements.item(i);
                if(next.getNodeType() == Node.TEXT_NODE) {
                   text += next.getNodeValue();
                }
                if(next.getNodeType() == Node.ELEMENT_NODE) {
                   if(next.getNodeName().equals("child1")) {
                      child1 = next.getTextContent();
                   }
                   if(next.getNodeName().equals("child2")) {
                      child2 = next.getTextContent();
                   }
                }
             }
             return new Something(child1, child2, text.trim());
          }
    
          public void write(OutputNode node, Something car) throws Exception {
             // Free text not supported!!
          }
       }
    
       @Root(name = "element1")
       @Convert(SomethingConverter.class)
       public static class Something {
           public String child1;
           public String child2;
           public String text;
           public Something(String child1, String child2, String text) {
              this.child1 = child1;
              this.child2 = child2;
              this.text = text;
           }
       }
    
       public void testHackJob() throws Exception {
          Class<?> type = Class.forName("org.simpleframework.xml.stream.DocumentProvider");
          Constructor<?> constructor = type.getDeclaredConstructor();
          constructor.setAccessible(true);
          Object value = constructor.newInstance();
          Field[] fields = NodeBuilder.class.getDeclaredFields();
    
          for(Field field : fields) {
             if(field.getName().equalsIgnoreCase("provider")) {
                field.setAccessible(true);
                field.set(null, value);
             }
          }
          StringReader reader = new StringReader(SOURCE);
          InputNode source = NodeBuilder.read(reader);
          AnnotationStrategy strategy = new AnnotationStrategy();
          Persister persister = new Persister(strategy);
          Something something = persister.read(Something.class, source);
    
          assertNotNull(something);
          assertEquals(something.text, "some inner text");
          assertEquals(something.child1, "11");
          assertEquals(something.child2, "True");
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an xml in the following format <root> <page> <title>text</title> <text attrib1=1>some text</text>
I have some simple data in XML format which I need to convert to
I have a XML file with this format: <object> <origin>1:1:1</origin> <normal>2:2:2</normal> <leafs> <object> <origin>1:1:1</origin>
I have an getting an xml response on this format <?xml version=\1.0\ encoding=\utf-8\?>\r\n <PlatformResponse
Is there any tool that can parse/conver xml files to this format? I have
I have a string in XML format and I want to use this string
I'm trying to format a table from XML. Lets say I have this line
Let's suppose I have xml like this one: <Server Active=No> <Url>http://some.url</Url> </Server> C# class
I have this xml file which has text init. i.e Hi my name is
I have some code like this: using System; using System.Collections.Generic; using System.Linq; using System.Xml.Linq;

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.