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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:00:49+00:00 2026-06-06T03:00:49+00:00

I saw a similar question being posted here, yet it did not help me

  • 0

I saw a similar question being posted here, yet it did not help me solve the problem so I am posting my question here to see if someone can modify my code to make it work.

Question: How to access mixed content String value and save it in setPhrase(String value) method?

caption.xml:

<?xml version="1.0" encoding="UTF-8"?>
<tt xmlns="link1" xmlns:prefix2="link2" prefix1:att1="att1">
    <head>
        <styling>
            <style prefix1:att1="att1" prefix2:att2="att2" prefix2:att3="att3" prefix2:att4="att4" />
        </styling>
        <layout />
    </head>
    <body xmlns:prefix3="link3">
        <div prefix1:att1="att1" prefix1:att2="att2">
            <prefix3:info att1="att1" att2="att2" />
            <p att1="att1" att2="att2" att3="att3">
                <prefix3:status att1="att1" att2="att2" />
                Hello World.
            </p>
        </div>
    </body>
</tt>

Caption.java:

package com;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlElementRefs;
import javax.xml.bind.annotation.XmlMixed;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement(name = "p")
@XmlType(propOrder = { "att1", "att2", "att3", "phrase", "subelement"})
public class Caption {
    private String  att1;
    private String  att2;
    private String  att3;
    private String  phrase;
    private Subelement subelement = new Subelement();

   @XmlMixed
   public void setPhrase(String value)
   {
      this.phrase = value;
   }
   public String getPhrase()
   {
      return phrase;
   }

   @XmlElementRefs({@XmlElementRef(name = "subelement", type = Subelement.class)})
   @XmlMixed
   public void setSubelement(Subelement subelement )
   {
      this.subelement = subelement;
   }
   public Subelement getSubelement()
   {
      return subelement;
   }

   @XmlAttribute
   public void setAtt1( String att1 )
   {
      this.att1 = att1;
   }
   public String getAtt1()
   {
      return att1;
   }

   @XmlAttribute
   public void setAtt2( String att2 )
   {
      this.att2 = att2;
   }
   public String getAtt2()
   {
      return att2;
   }

   @XmlAttribute
   public void setAtt3( String att3 )
   {
      this.att3 = att3;
   }
   public String getAtt3()
   {
      return att3;
   }
}

After using JAXB unmarshall and marshall I am able to get everything converted into and object and saved accorderling, except for the actual phrase “Hello World.”. I know I must use some sort of @XmlMixed for this complex element but I cannot figure it out.

My current output.xml:

<?xml version="1.0" encoding="UTF-8"?>
<tt xmlns="link1" xmlns:prefix2="link2" prefix1:att1="att1">
    <head>
        <styling>
            <style prefix1:att1="att1" prefix2:att2="att2" prefix2:att3="att3" prefix2:att4="att4" />
        </styling>
        <layout />
    </head>
    <body xmlns:prefix3="link3">
        <div prefix1:att1="att1" prefix1:att2="att2">
            <prefix3:info att1="att1" att2="att2" />
            <p att1="att1" att2="att2" att3="att3">
                <prefix3:status att1="att1" att2="att2" />
            </p>
        </div>
    </body>
</tt>

Desire output.xml: (same as caption.xml)

<?xml version="1.0" encoding="UTF-8"?>
<tt xmlns="link1" xmlns:prefix2="link2" prefix1:att1="att1">
    <head>
        <styling>
            <style prefix1:att1="att1" prefix2:att2="att2" prefix2:att3="att3" prefix2:att4="att4" />
        </styling>
        <layout />
    </head>
    <body xmlns:prefix3="link3">
        <div prefix1:att1="att1" prefix1:att2="att2">
            <prefix3:info att1="att1" att2="att2" />
            <p att1="att1" att2="att2" att3="att3">
                <prefix3:status att1="att1" att2="att2" />
                Hello World.
            </p>
        </div>
    </body>
</tt>

Thanks in advance to any help I may get to access this value and save it in setPhrase(String value) method.

  • 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-06T03:00:50+00:00Added an answer on June 6, 2026 at 3:00 am

    I’ll try to answer your question with an example:

    input.xml

    We will use the following XML document for this example. The root element has mixed content. Having mixed conent means that text nodes can appear mixed in with the elements. Since more than one text node can appear a unary property isn’t a good fit.

    <?xml version="1.0" encoding="UTF-8"?>
    <root>
        <root/>
        Hello
        <root/>
        World
        <root/>
    </root>
    

    Demo

    The following code will be used in to read in the XML to object form and then write it back to XML.

    package forum10940267;
    
    import java.io.File;
    import javax.xml.bind.*;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(Root.class);
    
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            File xml = new File("src/forum10940267/input.xml");
            Root root = (Root) unmarshaller.unmarshal(xml);
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(root, System.out);
        }
    
    }
    

    USE CASE #1 – One List to Hold Mixed Content

    @XmlMixed is most often used to with another annotation, so that the resulting List contains both element and text content. One advantage of this is that order is maintained so that the document can be round tripped.

    package forum10940267;
    
    import java.util.*;
    import javax.xml.bind.annotation.*;
    
    @XmlRootElement
    public class Root {
    
        private List<Object> mixedContent = new ArrayList<Object>();
    
        @XmlElementRef(name="root", type=Root.class)
        @XmlMixed
        public List<Object> getMixedContent() {
            return mixedContent;
        }
    
        public void setMixedContent(List<Object> mixedContent) {
            this.mixedContent = mixedContent;
        }
    
    }
    

    Output

    The output matches the input.

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <root>
        <root/>
        Hello
        <root/>
        World
        <root/>
    </root>
    

    USE CASE #2 – Separate List for Mixed Content

    You can can also introduce a separate list property for the text content.

    package forum10940267;
    
    import java.util.*;
    import javax.xml.bind.annotation.*;
    
    @XmlRootElement
    public class Root {
    
        private List<Object> mixedContent = new ArrayList<Object>();
        private List<String> text;
    
        @XmlElementRef(name="root", type=Root.class)
        public List<Object> getMixedContent() {
            return mixedContent;
        }
    
        public void setMixedContent(List<Object> mixedContent) {
            this.mixedContent = mixedContent;
        }
    
        @XmlMixed
        public List<String> getText() {
            return text;
        }
    
        public void setText(List<String> text) {
            this.text = text;
        }
    
    }
    

    Output

    The output no longer matches the input.

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <root>
        <root/>
        <root/>
        <root/>
    
        Hello
    
        World
    
    </root>
    

    USE CASE #3 – String Property for Text Content

    Since text nodes can occur multiple times in mixed content, a non-List property isn’t a good fit and it appears as though the @XmlMixed annotation is being ignored.

    package forum10940267;
    
    import java.util.*;
    import javax.xml.bind.annotation.*;
    
    @XmlRootElement
    public class Root {
    
        private List<Object> mixedContent = new ArrayList<Object>();
        private String text;
    
        @XmlElementRef(name="root", type=Root.class)
        public List<Object> getMixedContent() {
            return mixedContent;
        }
    
        public void setMixedContent(List<Object> mixedContent) {
            this.mixedContent = mixedContent;
        }
    
        @XmlMixed
        public String getText() {
            return text;
        }
    
        public void setText(String text) {
            this.text = text;
        }
    
    }
    

    Output

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <root>
        <root/>
        <root/>
        <root/>
    </root>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to solve interviewstreet's median challenge. I saw a similar question posted here:
I saw similar question here in SO, where someone wants to do the same
I saw a similar question asked and answered for ASP.net here How do I
I saw another similar question answered here - Velocity editor plugin for Eclipse? .
I saw this similar question here but can't figure out how to use Contains
I saw that there are many similar question being asked. But in my case,
I saw couple of similar question here on StackOverFlow but none of them to
I saw a similar question but no answers yet. I have the following SQL
I saw a similar question here on SO but I believe mine differs a
I have a question involving the Replace Method. I saw a question similar to

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.