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日

悬赏问题

  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持