JAXB- @XmlMixed read use @XmlValue and @XmlElement

I saw a similar question that was posted here, but this did not help me solve the problem, so I post my question here to find out if anyone can change my code to make it work.

Question: How to access mixed String content 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 can get everything that was converted to an object and save and save the emoticons, except for the actual phrase "Hello World". I know that for this complex element I have to use some kind of @XmlMixed, but I can not understand.

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>

, setPhrase (String value).

+3
1

:

Input.xml

XML-. root . , . node, .

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

Demo

XML , 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);
    }

}

№1 -

@XmlMixed , List , . , , .

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;
    }

}

.

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

CASE # 2 -

.

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;
    }

}

.

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

    Hello

    World

</root>

CASE # 3 - String

, non-List , @XmlMixed .

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;
    }

}

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

Source: https://habr.com/ru/post/1729389/


All Articles