{"id":65662,"date":"2025-05-03T11:00:13","date_gmt":"2025-05-03T03:00:13","guid":{"rendered":"https:\/\/fwq.ai\/blog\/65662\/"},"modified":"2025-05-03T11:00:13","modified_gmt":"2025-05-03T03:00:13","slug":"java%e6%80%8e%e4%b9%88%e6%8a%8a%e6%95%b0%e7%bb%84%e8%bd%ac%e6%88%90xml-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/65662\/","title":{"rendered":"java\u600e\u4e48\u628a\u6570\u7ec4\u8f6c\u6210xml"},"content":{"rendered":"<blockquote><p>\n  \u53ef\u4ee5\u4f7f\u7528 java \u7684 jaxb \u5e93\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a xml\u3002\u5176\u6b65\u9aa4\u5305\u62ec\uff1a\u521b\u5efa\u4e0e\u6570\u7ec4\u7ed3\u6784\u5bf9\u5e94\u7684 java \u6a21\u578b\u7c7b\uff0c\u5e76\u4f7f\u7528 @xmlrootelement \u6807\u8bb0\u6839\u5143\u7d20\u3002\u521b\u5efa jaxbcontext \u5b9e\u4f8b\u4ee5\u6307\u5b9a\u6a21\u578b\u7c7b\u3002\u4ece jaxbcontext \u521b\u5efa marshaller \u5b9e\u4f8b\u4ee5\u5c06\u5bf9\u8c61\u8f6c\u6362\u4e3a xml\u3002\u8bbe\u7f6e marshaller \u7684\u683c\u5f0f\u5316\u5c5e\u6027\uff08\u53ef\u9009\uff09\u3002\u4f7f\u7528 marshaller \u7684 marshal \u65b9\u6cd5\u5c06\u6570\u7ec4\u5bf9\u8c61\u8f6c\u6362\u4e3a xml \u5e76\u5199\u5165\u8f93\u51fa\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111306483980323.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u628a\u6570\u7ec4\u8f6c\u6210xml\u63d2\u56fe\" alt=\"java\u600e\u4e48\u628a\u6570\u7ec4\u8f6c\u6210xml\u63d2\u56fe\" \/><\/p>\n<p><strong>Java\u4e2d\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3aXML<\/strong><\/p>\n<p><strong>\u65b9\u6cd5\uff1a<\/strong><\/p>\n<p>\u4f7f\u7528Java\u7684JAXB (Java Architecture for XML Binding) \u5e93\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3aXML\u3002<\/p>\n<p><strong>\u6b65\u9aa4\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p><strong>1. \u521b\u5efa\u4e00\u4e2a\u6a21\u578b\u7c7b\uff1a<\/strong><\/p>\n<p>\u521b\u5efa\u4e0e\u6570\u7ec4\u7ed3\u6784\u76f8\u5bf9\u5e94\u7684Java\u6a21\u578b\u7c7b\uff08POJO\uff09\uff0c\u8be5\u7c7b\u5e94\u8be5\u6807\u6ce8@XmlRootElement\u6ce8\u89e3\uff0c\u4ee5\u6307\u5b9a\u6839\u5143\u7d20\u3002<\/p>\n<pre>@XmlRootElement(name = \"myArray\")\npublic class MyArray {\n\n    private String[] values;\n\n    \/\/ \u6784\u9020\u51fd\u6570\u3001getter\u548csetter\u7701\u7565...\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528JAXBContext\uff1a<\/strong><\/p>\n<p>\u521b\u5efaJAXBContext\u5b9e\u4f8b\uff0c\u6307\u5b9a\u8981\u7ed1\u5b9a\u7684\u6a21\u578b\u7c7b\u3002<\/p>\n<pre>JAXBContext context = JAXBContext.newInstance(MyArray.class);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u521b\u5efaMarshaller\uff1a<\/strong><\/p>\n<p>\u4eceJAXBContext\u521b\u5efaMarshaller\u5b9e\u4f8b\uff0c\u7528\u4e8e\u5c06\u5bf9\u8c61\u8f6c\u6362\u4e3aXML\u3002<\/p>\n<pre>Marshaller marshaller = context.createMarshaller();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u8bbe\u7f6e\u683c\u5f0f\uff1a<\/strong><\/p>\n<p>\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8bbe\u7f6eMarshaller\u7684\u683c\u5f0f\u5316\u5c5e\u6027\uff0c\u4ee5\u4f18\u5316XML\u8f93\u51fa\u3002<\/p>\n<pre>marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>5. \u6267\u884c\u8f6c\u6362\uff1a<\/strong><\/p>\n<p>\u5c06\u6570\u7ec4\u5bf9\u8c61\u4f20\u9012\u7ed9Marshaller\u7684marshal\u65b9\u6cd5\uff0c\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3aXML\u5e76\u5199\u5165\u6307\u5b9a\u7684\u8f93\u51fa\u3002<\/p>\n<pre>marshaller.marshal(myArray, System.out);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u4ee5\u4e0b\u793a\u4f8b\u6f14\u793a\u5982\u4f55\u5c06\u8f6c\u6362\u4e3aXML\uff1a<\/p>\n<pre>String[] array = {\"Item 1\", \"Item 2\", \"Item 3\"};\nMyArray myArray = new MyArray(array);\n\nJAXBContext context = JAXBContext.newInstance(MyArray.class);\nMarshaller marshaller = context.createMarshaller();\nmarshaller.marshal(myArray, System.out);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u8f93\u51fa XML\uff1a<\/strong><\/p>\n<pre>&lt;myArray&gt;\n  &lt;values&gt;Item 1&lt;\/values&gt;\n  &lt;values&gt;Item 2&lt;\/values&gt;\n  &lt;values&gt;Item 3&lt;\/values&gt;\n&lt;\/myArray&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u628a\u6570\u7ec4\u8f6c\u6210xml\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8IDCBABY\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u53ef\u4ee5\u4f7f\u7528 java \u7684 jaxb \u5e93\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a xml\u3002\u5176\u6b65\u9aa4\u5305\u62ec\uff1a\u521b\u5efa\u4e0e\u6570\u7ec4\u7ed3\u6784\u5bf9\u5e94\u7684 java \u6a21\u578b\u7c7b\uff0c\u5e76\u4f7f\u7528 @xmlrootelement \u6807\u8bb0\u6839\u5143\u7d20\u3002\u521b\u5efa jaxbcontext \u5b9e\u4f8b\u4ee5\u6307\u5b9a\u6a21\u578b\u7c7b\u3002\u4ece jaxbcontext \u521b\u5efa marshaller \u5b9e\u4f8b\u4ee5\u5c06\u5bf9\u8c61\u8f6c\u6362\u4e3a xml\u3002\u8bbe\u7f6e marshaller \u7684\u683c\u5f0f\u5316\u5c5e\u6027\uff08\u53ef\u9009\uff09\u3002\u4f7f\u7528 marshaller \u7684 marshal \u65b9\u6cd5\u5c06\u6570\u7ec4\u5bf9\u8c61\u8f6c\u6362\u4e3a xml \u5e76\u5199\u5165\u8f93\u51fa\u3002 Java\u4e2d\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3aXML \u65b9\u6cd5\uff1a \u4f7f\u7528Java\u7684JAXB (Java Architecture for XML Binding) \u5e93\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3aXML\u3002 \u6b65\u9aa4\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b 1. \u521b\u5efa\u4e00\u4e2a\u6a21\u578b\u7c7b\uff1a \u521b\u5efa\u4e0e\u6570\u7ec4\u7ed3\u6784\u76f8\u5bf9\u5e94\u7684Java\u6a21\u578b\u7c7b\uff08POJO\uff09\uff0c\u8be5\u7c7b\u5e94\u8be5\u6807\u6ce8@XmlRootElement\u6ce8\u89e3\uff0c\u4ee5\u6307\u5b9a\u6839\u5143\u7d20\u3002 @XmlRootElement(name = &#8220;myArray&#8221;) public class MyArray { private String[] values; \/\/ \u6784\u9020\u51fd\u6570\u3001getter\u548csetter\u7701\u7565&#8230; } \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528JAXBContext\uff1a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-65662","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65662","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=65662"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65662\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=65662"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=65662"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=65662"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}