java对象映射xml不会弄了,大家给看下

将java对象映射成下面的xml


<speak>
    <voice name=\"zh-CN-YunxiNeural\">我是
        <break time=\"1000ms\" />张三
    </voice>
    <voice name=\"zh-CN-XiaoxiaoNeural\">他是
        <break time=\"100ms\" />李四
    </voice>
</speak>"

下面是我的代码和输出效果,接下来不会弄了,break标签旁边的文字不知道怎么处理


@Data
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "speak")
public class SpeakXbj {

    @XmlAttribute
    private String version = "1.0";

    @XmlAttribute
    private String xmlns = "http://www.w3.org/2001/10/synthesis";

    @XmlAttribute(name = "xmlns:mstts")
    private String mstts = "http://www.w3.org/2001/mstts";

    @XmlAttribute(name = "xmlns:emo")
    private String emo = "http://www.w3.org/2009/10/emotionml";

    @XmlAttribute(name = "xml:lang")
    private String lang = "zh-CN";

    private List<VoiceXbj> voice;
}

@Data
@XmlAccessorType(XmlAccessType.FIELD)
public class VoiceXbj {

    @XmlAttribute
    private String name;

    @XmlElement(name = "break")
    private List<BreakXbj> breakXbj;
}

@Data
@XmlAccessorType(XmlAccessType.FIELD)
public class BreakXbj {

    @XmlAttribute
    private String time;
}

@Slf4j
public class XmlUtil {

    /**
     * 对象转XML JAXB
     * @return  xml
     */
    public static String convertToXml(Object obj) {
        StringWriter sw = new StringWriter();
        try {
            JAXBContext context = JAXBContext.newInstance(obj.getClass());

            Marshaller marshaller = context.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_ENCODING, "GBK");
            //marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);// 去掉报文头
            // 格式化xml输出的格式
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            // 将对象转换成输出流形式的xml
            marshaller.marshal(obj, sw);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return sw.toString();
    }
}
@Slf4j
public class SsmlTest {

    @Test
    public void test1() {
        SpeakXbj xbj = new SpeakXbj();
        List<VoiceXbj> voiceList = new ArrayList<>();
        VoiceXbj voice = new VoiceXbj();
        voice.setName("Yunxi");
        BreakXbj breakXbj = new BreakXbj();
        breakXbj.setTime("100ml");
        List<BreakXbj> breakList = new ArrayList<>();
        breakList.add(breakXbj);
        voice.setBreakXbj(breakList);
        voiceList.add(voice);
        VoiceXbj voice1 = new VoiceXbj();
        voice1.setName("Xiaomeng");
        voiceList.add(voice1);
        xbj.setVoice(voiceList);
        log.info(XmlUtil.convertToXml(xbj));
    }
}

输出

<speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis" xmlns:mstts="http://www.w3.org/2001/mstts" xmlns:emo="http://www.w3.org/2009/10/emotionml" xml:lang="zh-CN">
    <voice name="Yunxi">
        <break time="100ml"/>
    </voice>
    <voice name="Xiaomeng"/>
</speak>

结果

<?xml version="1.0" encoding="GBK" standalone="yes"?>
<speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis" xmlns:mstts="http://www.w3.org/2001/mstts" xmlns:emo="http://www.w3.org/2009/10/emotionml" xml:lang="zh-CN">
    <voice name="zh-CN-YunxiNeural">我是
        <break time="1000ms"/>张三</voice>
    <voice name="zh-CN-XiaoxiaoNeural">他是
        <break time="100ms"/>李四</voice>
</speak>

VoiceXbj


@Data
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "voice")
public class VoiceXbj {
 
    @XmlAttribute
    private String name;

    @XmlMixed
    @XmlElementRef(type = BreakXbj.class)
    private List<Object> content;

}

BreakXbj


@Data
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "break")
public class BreakXbj {
 
    @XmlAttribute
    private String time;
}

SsmlTest

@Slf4j
public class SsmlTest {
 
    @Test
    public void test1() {
        SpeakXbj xbj = new SpeakXbj();
        List<VoiceXbj> voiceList = new ArrayList<>();
        VoiceXbj voice = new VoiceXbj();
        voice.setName("Yunxi");
        BreakXbj breakXbj = new BreakXbj();
        breakXbj.setTime("1000ms");
        List<Object> breakList = new ArrayList<>();
        breakList.add("我是");
        breakList.add(breakXbj);
        breakList.add("张三");
        voice.setContent(breakList);
        voiceList.add(voice);

        VoiceXbj voice1 = new VoiceXbj();
        voice1.setName("Aixia");
        BreakXbj breakXbj1 = new BreakXbj();
        breakXbj1.setTime("100ms");
        List<Object> breakList1 = new ArrayList<>();
        breakList1.add("他是");
        breakList1.add(breakXbj1);
        breakList1.add("李四");
        voice1.setContent(breakList1);
        voiceList.add(voice1);

        xbj.setVoice(voiceList);
        System.out.println(XmlUtil.convertToXml(xbj));
    }
}

仅供参考:
你需要在 VoiceXbj 类中添加一个字符串字段,用于表示 break 标签旁边的文字,然后在输出 XML 时将其添加到 break 标签之后即可。

修改后的 VoiceXbj 类如下:

@Data
@XmlAccessorType(XmlAccessType.FIELD)
public class VoiceXbj {

    @XmlAttribute
    private String name;

    @XmlElement(name = "break")
    private List<BreakXbj> breakXbj;

    @XmlValue
    private String text;
}

同时,你需要修改测试方法,以便将文本添加到 VoiceXbj 对象中:

@Test
public void test1() {
    SpeakXbj xbj = new SpeakXbj();
    List<VoiceXbj> voiceList = new ArrayList<>();
    VoiceXbj voice = new VoiceXbj();
    voice.setName("zh-CN-YunxiNeural");
    BreakXbj breakXbj = new BreakXbj();
    breakXbj.setTime("1000ms");
    voice.setText("我是");
    List<BreakXbj> breakList = new ArrayList<>();
    breakList.add(breakXbj);
    voice.setBreakXbj(breakList);
    voiceList.add(voice);

    VoiceXbj voice1 = new VoiceXbj();
    voice1.setName("zh-CN-XiaoxiaoNeural");
    voice1.setText("他是");
    BreakXbj breakXbj1 = new BreakXbj();
    breakXbj1.setTime("100ms");
    List<BreakXbj> breakList1 = new ArrayList<>();
    breakList1.add(breakXbj1);
    voice1.setBreakXbj(breakList1);
    voiceList.add(voice1);

    xbj.setVoice(voiceList);
    log.info(XmlUtil.convertToXml(xbj));
}

输出如下:

<?xml version="1.0" encoding="GBK" standalone="yes"?>
<speak xmlns="http://www.w3.org/2001/10/synthesis" xmlns:emo="http://www.w3.org/2009/10/emotionml" xmlns:mstts="http://www.w3.org/2001/mstts" xml:lang="zh-CN" version="1.0">
    <voice name="zh-CN-YunxiNeural">我是<break time="1000ms"/></voice>
    <voice name="zh-CN-XiaoxiaoNeural">他是<break time="100ms"/></voice>
</speak>