astro 2023-02-16 15:53 采纳率: 100%
浏览 56
已结题

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>

  • 写回答

3条回答 默认 最新

  • pzzhao 2023-02-16 20:39
    关注

    结果

    <?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));
        }
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
    1人已打赏
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 2月24日
  • 已采纳回答 2月16日
  • 修改了问题 2月16日
  • 创建了问题 2月16日

悬赏问题

  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集
  • ¥15 在启动roslaunch时出现如下问题
  • ¥15 汇编语言实现加减法计算器的功能
  • ¥20 关于多单片机模块化的一些问题
  • ¥30 seata使用出现报错,其他服务找不到seata
  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
  • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
  • ¥15 Google speech command 数据集获取
  • ¥15 vue3+element-plus页面崩溃