如何用JAXB解析包含``标签的XML?

如何用jaxb解析包含``标签的xml?

如何用jaxb解析包含标签的xml

在本例中,我们需要解析的xml结构如下:

<speak><voice name="zh-cn-yunxineural">我是
        <break time="1000ms"></break>张三
    </voice><voice name="zh-cn-xiaoxiaoneural">他是
        <break time="100ms"></break>李四
    </voice></speak>

为了实现这一目标,我们需要更新voicexbj和breakxbj模型类:

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

}</object>

在voicexbj类中,我们使用@xmlmixed和@xmlelementref注解来将标签及其内容映射为content列表。这将允许我们处理标签之间的文本内容。

现在,我们可以使用修改后的模型类和辅助方法更新测试用例:

@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));
    }
}</object></object></voicexbj>

输出的xml将如下:

<?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"></break>张三</voice><voice name="zh-CN-XiaoxiaoNeural">他是
        <break time="100ms"></break>李四</voice></speak>

以上就是如何用JAXB解析包含``标签的XML?的详细内容,更多请关注其它相关文章!