使用JAXB怎样构建java对象能映射出下面的xml结构?

作者站长头像
站长
· 阅读数 5
<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>
回复
1个回答
avatar
test
2024-07-10
@Data
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "voice")
public class VoiceXbj {
 
    @XmlAttribute
    private String name;
 
    @XmlMixed
    @XmlElementRef(type = BreakXbj.class)
    private List<Object> content;
 
}
@Data
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "break")
public class BreakXbj {
 
    @XmlAttribute
    private String time;
}
@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));
    }
}

输出结果

<?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>
回复
likes
适合作为回答的
  • 经过验证的有效解决办法
  • 自己的经验指引,对解决问题有帮助
  • 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
  • 询问内容细节或回复楼层
  • 与题目无关的内容
  • “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容