{"id":202788,"date":"2025-05-22T10:44:41","date_gmt":"2025-05-22T02:44:41","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202788\/"},"modified":"2025-05-22T10:44:41","modified_gmt":"2025-05-22T02:44:41","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-jaxb-%e6%9e%84%e5%bb%ba%e6%98%a0%e5%b0%84%e4%bb%a5%e4%b8%8b-xml-%e7%bb%93%e6%9e%84%e7%9a%84-java-%e5%af%b9%e8%b1%a1%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202788\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 JAXB \u6784\u5efa\u6620\u5c04\u4ee5\u4e0b XML \u7ed3\u6784\u7684 Java \u5bf9\u8c61\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u4f7f\u7528 JAXB \u6784\u5efa\u6620\u5c04\u4ee5\u4e0b XML \u7ed3\u6784\u7684 Java \u5bf9\u8c61\uff1f<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span>    <\/p>\n<p>\u7f16\u7a0b\u5e76\u4e0d\u662f\u4e00\u4e2a\u673a\u68b0\u6027\u7684\u5de5\u4f5c\uff0c\u800c\u662f\u9700\u8981\u6709\u601d\u8003\uff0c\u6709\u521b\u65b0\u7684\u5de5\u4f5c\uff0c\u8bed\u6cd5\u662f\u56fa\u5b9a\u7684\uff0c\u4f46\u89e3\u51b3\u95ee\u9898\u7684\u601d\u8def\u5219\u662f\u4f9d\u9760\u4eba\u7684\u601d\u7ef4\uff0c\u8fd9\u5c31\u9700\u8981\u6211\u4eec\u575a\u6301\u5b66\u4e60\u548c\u66f4\u65b0\u81ea\u5df1\u7684\u77e5\u8bc6\u3002\u4eca\u5929\u5c31\u6574\u7406\u5206\u4eab\u300a\u5982\u4f55\u4f7f\u7528 JAXB \u6784\u5efa\u6620\u5c04\u4ee5\u4e0b XML \u7ed3\u6784\u7684 Java \u5bf9\u8c61\uff1f\u300b\uff0c\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\uff0c\u5982\u679c\u4f60\u5bf9\u6587\u7ae0\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\uff0c\u5728\u8fd9\u53ef\u4ee5\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5f00\u53d1\u80fd\u529b\u7684\u63d0\u5347\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729942049671cd221ac631.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u4f7f\u7528jaxb\u6784\u5efa\u6620\u5c04xml\u7ed3\u6784\u7684java\u5bf9\u8c61<\/strong><\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong><\/p>\n<p>\u6784\u5efajava\u5bf9\u8c61\uff0c\u4ee5\u4fbf\u6620\u5c04\u4ee5\u4e0bxml\u7ed3\u6784\uff1a<\/p>\n<pre>&lt;speak&gt;\n    &lt;voice name=\"zh-cn-yunxineural\"&gt;\u6211\u662f\n        &lt;break time=\"1000ms\" \/&gt;\n        \u5f20\u4e09\n    &lt;\/voice&gt;\n    &lt;voice name=\"zh-cn-xiaoxiaoneural\"&gt;\u4ed6\u662f\n        &lt;break time=\"100ms\" \/&gt;\n        \u674e\u56db\n    &lt;\/voice&gt;\n&lt;\/speak&gt;<\/pre>\n<p><strong>\u5b9e\u73b0\uff1a<\/strong><\/p>\n<p>\u8981\u4f7f\u7528jaxb\u6784\u5efa\u6620\u5c04\u6b64xml\u7ed3\u6784\u7684java\u5bf9\u8c61\uff0c\u9700\u8981\u5b9a\u4e49\u4ee5\u4e0b\u7c7b\uff1a<\/p>\n<p><strong>speakxbj.java<\/strong><\/p>\n<pre>@data\n@xmlaccessortype(xmlaccesstype.field)\n@xmlrootelement(name = \"speak\")\npublic class speakxbj {\n\n    private string version = \"1.0\";\n    private string xmlns = \"http:\/\/www.w3.org\/2001\/10\/synthesis\";\n    private string mstts = \"http:\/\/www.w3.org\/2001\/mstts\";\n    private string emo = \"http:\/\/www.w3.org\/2009\/10\/emotionml\";\n    private string lang = \"zh-cn\";\n    private list&lt;voicexbj&gt; voice;\n}<\/pre>\n<p><strong>voicexbj.java<\/strong><\/p>\n<pre>@data\n@xmlaccessortype(xmlaccesstype.field)\n@xmlrootelement(name = \"voice\")\npublic class voicexbj {\n\n    private string name;\n    @xmlmixed\n    @xmlelementref(type = breakxbj.class)\n    private list&lt;object&gt; content;\n}<\/pre>\n<p><strong>breakxbj.java<\/strong><\/p>\n<pre>@data\n@xmlaccessortype(xmlaccesstype.field)\n@xmlrootelement(name = \"break\")\npublic class breakxbj {\n\n    private string time;\n}<\/pre>\n<p>\u8fd9\u4e9b\u7c7b\u4e2d\u7684@xmlrootelement\u548c@xmlaccessortype\u6ce8\u89e3\u7528\u4e8e\u914d\u7f6ejaxb\u7684xml\u4e0e\u5bf9\u8c61\u6620\u5c04\u3002@xmlmixed\u6ce8\u89e3\u5141\u8bb8\u5728\u4e00\u4e2avoicexbj\u5143\u7d20\u4e2d\u6df7\u5408\u6587\u672c\u548c\u5b50\u5143\u7d20\u3002<\/p>\n<p><strong>\u4f7f\u7528jaxb\u8f6c\u6362\u5bf9\u8c61\uff1a<\/strong><\/p>\n<p>\u8981\u5c06java\u5bf9\u8c61\u8f6c\u6362\u4e3axml\uff0c\u53ef\u4ee5\u4f7f\u7528xmlutil\u7c7b\u4e2d\u7684converttoxml\u65b9\u6cd5\uff1a<\/p>\n<pre>public class xmlutil {\n\n    public static string converttoxml(object obj) {\n        \/\/ ... \u7701\u7565\u4ee3\u7801\n    }\n}<\/pre>\n<p><strong>\u6d4b\u8bd5\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u4ee5\u4e0b\u4ee3\u7801\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u521b\u5efaspeakxbj\u5bf9\u8c61\u5e76\u5c06\u5176\u8f6c\u6362\u4e3axml\uff1a<\/p>\n<pre>public class SsmlTest {\n\n    @Test\n    public void test1() {\n        SpeakXbj xbj = new SpeakXbj();\n        \/\/ ... \u7701\u7565\u5176\u4ed6\u8bbe\u7f6e\u4ee3\u7801\n        System.out.println(XmlUtil.convertToXml(xbj));\n    }\n}<\/pre>\n<p>\u901a\u8fc7\u8fd0\u884c\u6b64\u6d4b\u8bd5\u65b9\u6cd5\uff0c\u5c06\u8f93\u51fa\u4e00\u4e2a\u4e0e\u7ed9\u5b9axml\u7ed3\u6784\u76f8\u540c\u7684xml\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300a\u5982\u4f55\u4f7f\u7528 JAXB \u6784\u5efa\u6620\u5c04\u4ee5\u4e0b XML \u7ed3\u6784\u7684 Java \u5bf9\u8c61\uff1f\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03\u6587\u7ae0\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1aphp \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528 JAXB \u6784\u5efa\u6620\u5c04\u4ee5\u4e0b&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-202788","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202788","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=202788"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202788\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202788"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202788"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202788"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}