{"id":684,"date":"2023-02-26T14:41:52","date_gmt":"2023-02-26T06:41:52","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=684"},"modified":"2023-04-29T17:58:11","modified_gmt":"2023-04-29T09:58:11","slug":"using-xstream-to-transform-java-objects-and-xml-into-each-other","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/26\/using-xstream-to-transform-java-objects-and-xml-into-each-other\/","title":{"rendered":"\u5229\u7528XStream\u5728Java\u5bf9\u8c61\u548cXML\u4e4b\u95f4\u76f8\u4e92\u8f6c\u6362"},"content":{"rendered":"<h2>Xstream\u7b80\u4ecb<\/h2>\n<p>Xstream\u662f\u4e00\u79cdOXMapping \u6280\u672f\uff0c\u662f\u7528\u6765\u5904\u7406XML\u6587\u4ef6\u5e8f\u5217\u5316\u7684\u6846\u67b6,\u5728\u5c06JavaBean\u5e8f\u5217\u5316\uff0c\u6216\u5c06XML\u6587\u4ef6\u53cd\u5e8f\u5217\u5316\u7684\u65f6\u5019\uff0c\u4e0d\u9700\u8981\u5176\u5b83\u8f85\u52a9\u7c7b\u548c\u6620\u5c04\u6587\u4ef6\uff0c\u4f7f\u5f97XML\u5e8f\u5217\u5316\u4e0d\u518d\u7e41\u7d22\u3002Xstream\u4e5f\u53ef\u4ee5\u5c06JavaBean\u5e8f\u5217\u5316\u6210Json\u6216\u53cd\u5e8f\u5217\u5316\uff0c\u4f7f\u7528\u975e\u5e38\u65b9\u4fbf\u3002<\/p>\n<p><!-- more --><\/p>\n<p>\u4e3b\u8981\u4f7f\u7528<\/p>\n<ul>\n<li><code>@XStreamAlias(&quot;student&quot;)<\/code>: Java\u5bf9\u8c61\u5728xml\u4e2d\u4ee5\u6807\u7b7e\u7684\u5f62\u5f0f\u663e\u793a\u65f6\uff0c\u5982\u679c\u540d\u5b57\u4e0e\u7c7b\u540d\u6216\u8005\u5c5e\u6027\u540d\u4e0d\u4e00\u81f4\uff0c\u53ef\u4ee5\u4f7f\u7528\u8be5\u6807\u7b7e\u5e76\u5728\u62ec\u53f7\u5185\u6ce8\u660e\u522b\u540d\u3002\u7b49\u540c\u4e8e<code>xstream.alias(&quot;student&quot;, Student.class)<\/code><\/li>\n<li><code>@XStreamOmitField<\/code>: \u5728\u8f93\u51faXML\u7684\u65f6\u5019\u5ffd\u7565\u8be5\u5c5e\u6027<\/li>\n<li><code>@XStreamImplicit<\/code>: \u5982\u679c\u8be5\u5c5e\u6027\u662f\u4e00\u4e2a\u5217\u8868\u6216\u8005\u6570\u7ec4\uff0c\u5728XML\u4e2d\u4e0d\u663e\u793alist\u6216\u8005Array\u5b57\u6837\u3002\u5f53\u9700\u8981\u5c06collection\u6216map\u7c7b\u578b\u7684\u6210\u5458\u53d8\u91cf\u4e2d\u6570\u636e\u8f6c\u6362\u6210xml\u76f8\u540c\u5c42\u6b21\u7684\u5143\u7d20\u65f6\uff0c\u53ef\u4ee5\u5728\u8be5\u6210\u5458\u53d8\u91cf\u4f7f\u7528\u8be5\u6ce8\u89e3\uff0c\u4f1a\u5c06\u6dfb\u52a0\u6ce8\u91ca\u7684\u8282\u70b9\u53bb\u6389<code>@XStreamImplicit(itemFieldName=&quot;studentList&quot;)<\/code><\/li>\n<li><code>@XStreamAsAttribute<\/code>: \u8be5\u5c5e\u6027\u4e0d\u5355\u72ec\u663e\u793a\u6210XML\u8282\u70b9\uff0c\u800c\u662f\u4f5c\u4e3a\u5c5e\u6027\u663e\u793a\u51fa\u6765\u3002\u7b49\u540c\u4e8e<code>xstream.useAttributeFor(Student.class, &quot;sid&quot;)<\/code><\/li>\n<li><code>@XStreamContainedType<\/code><\/li>\n<li><code>@XStreamConverter<\/code>: \u8bbe\u7f6e\u8f6c\u6362\u5668\uff0c\u7528\u4e8e\u6307\u5b9aclass\u53caField\u7684converter\uff08\u8f6c\u6362\u65b9\u5f0f\uff09<\/li>\n<li><code>@XStreamConverters<\/code>: \u4e3b\u8981\u7528\u4e8e\u5c06\u67d0\u4e9b\u5b57\u6bb5\u8fdb\u884c\u590d\u6742\u7684\u8f6c\u6362\uff0c\u8f6c\u6362\u8fc7\u7a0b\u5199\u5728\u4e00\u4e2a\u7c7b\u4e2d<\/li>\n<\/ul>\n<h2>Xstream\u4f7f\u7528<\/h2>\n<p>\uff081\uff09Maven\u4f9d\u8d56<\/p>\n<pre><code class=\"language-xml\">&lt;dependency&gt;\n    &lt;groupId&gt;com.thoughtworks.xstream&lt;\/groupId&gt;\n    &lt;artifactId&gt;xstream&lt;\/artifactId&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<p>\uff082\uff09\u6620\u5c04\u7684xml\u4ee3\u7801<\/p>\n<pre><code class=\"language-xml\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\n&lt;province province_code=&quot;ZJ&quot;&gt;\n    &lt;city id=&quot;1001&quot; city_name=&quot;\u676d\u5dde&quot; city_code=&quot;hanghzou&quot; \/&gt;\n    &lt;city id=&quot;1002&quot; city_name=&quot;\u5b81\u6ce2&quot; city_code=&quot;ningbo&quot; \/&gt;\n    &lt;city id=&quot;1003&quot; city_name=&quot;\u6e29\u5dde&quot; city_code=&quot;wenzhou&quot; \/&gt;\n&lt;\/province&gt;<\/code><\/pre>\n<p>\uff083\uff09\u5728bean\u4e2d\u52a0\u4e0a\u76f8\u5173\u6ce8\u89e3<\/p>\n<p>\u57ce\u5e02bean<\/p>\n<pre><code class=\"language-java\">@Data\n@AllArgsConstructor\n@NoArgsConstructor\n@XStreamAlias(&quot;city&quot;)\npublic class City {\n    @XStreamAsAttribute\n    @XStreamAlias(&quot;id&quot;)\n    private String cityId;\n\n    @XStreamAsAttribute\n    @XStreamAlias(&quot;city_name&quot;)\n    private String cityName;\n\n    @XStreamAlias(&quot;city_code&quot;)\n    @XStreamAsAttribute\n    private String cityCode;\n}<\/code><\/pre>\n<p>\u57ce\u5e02\u96c6\u5408bean<\/p>\n<pre><code class=\"language-java\">@Data\n@AllArgsConstructor\n@NoArgsConstructor\n@XStreamAlias(&quot;province&quot;)\npublic class CityList {\n    @XStreamImplicit(itemFieldName=&quot;city&quot;)\n    private List&lt;City&gt; cityList;\n}<\/code><\/pre>\n<p>\u91cd\u547d\u540d\u6ce8\u89e3\uff1a<code>@XStreamAlias()<\/code><br \/>\n\u7701\u7565\u96c6\u5408\u6839\u8282\u70b9\uff1a<code>@XStreamImplicit<\/code><br \/>\n\u5b57\u6bb5\u8282\u70b9\u8bbe\u7f6e\u6210\u5c5e\u6027\uff1a<code>@XStreamAsAttribute<\/code><br \/>\n\u8fd9\u4e9b\u547d\u540d\u90fd\u9700\u8981\u548c\u89e3\u6790\u7684xml\u7684\u5c5e\u6027\u540d\u4e00\u4e00\u5bf9\u5e94\uff0c\u5426\u5219\u4f1a\u62a5<code>com.thoughtworks.xstream.mapper.CannotResolveClassException<\/code>\u5f02\u5e38\uff0c\u627e\u4e0d\u5230\u5bf9\u5e94\u7684\u7c7b\u5c5e\u6027<br \/>\n\u96c6\u5408\u5c5e\u6027\u9700\u8981\u4f7f\u7528\uff1a<code>@XStreamImplicit<\/code>\uff0c\u4e0d\u7136\u4f1a\u62a5<code>com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$DuplicateFieldException<\/code>\u8f6c\u6362\u5668\u6620\u5c04\u5f02\u5e38<\/p>\n<p>\uff084\uff09\u6700\u540e\u9700\u8981\u5199\u4e2a\u8f6c\u6362\u7684\u5de5\u5177\u7c7b<\/p>\n<pre><code class=\"language-java\">public class XsteamUtil {\n    public static Object toBean(Class&lt;?&gt; clazz, String xml) {\n        Object xmlObject = null;\n        XStream xstream = new XStream();\n        xstream.processAnnotations(clazz);\n        xstream.autodetectAnnotations(true);\n        xmlObject= xstream.fromXML(xml);\n        return xmlObject;\n    }\n}<\/code><\/pre>\n<p><code>Class&lt;?&gt; clazz<\/code>\u6307\u5b9a\u6211\u4eec\u9700\u8981\u8f6c\u6362\u7684\u5bf9\u8c61\uff0c<code>xml<\/code>\u662f\u9700\u8981\u8f6c\u7684xml\u5b57\u7b26\u4e32<\/p>\n<h2>\u62d3\u5c55\u4e86\u89e3<\/h2>\n<p>XStream\u63d0\u4f9b\u4e86\u5f88\u591a\u65b9\u6cd5\u4f9b\u6211\u4eec\u4f7f\u7528<\/p>\n<ul>\n<li><code>autodetectAnnotations()<\/code>: \u81ea\u52a8\u68c0\u6d4b\u6ce8\u89e3<\/li>\n<li><code>processAnnotations()<\/code>: \u5e94\u7528\u4f20\u9012\u7684\u7c7b\u7684\u6ce8\u89e3<\/li>\n<li><code>fromXML()<\/code>: XML\u53cd\u5e8f\u5217\u5316(JSON\u4e5f\u662f\u4e00\u6837)<\/li>\n<li><code>toXML()<\/code>: XML\u5e8f\u5217\u5316(JSON\u4e5f\u662f\u4e00\u6837)<\/li>\n<li>\u81ea\u5b9a\u4e49\u89e3\u6790\u5668<\/li>\n<\/ul>\n<p>Xstream\u5e8f\u5217\u5316XML\uff0c\u89e3\u6790\u5668\u7528StaxDriver<br \/>\n\u6307\u5b9a\u89e3\u6790\u5668\uff1a<code>XStream xstream = new XStream(new StaxDriver());<\/code><\/p>\n<p>Xstream\u5e8f\u5217\u5316Json\uff0c\u89e3\u6790\u5668\u7528JettisonMappedXmlDriver<br \/>\n\u6307\u5b9a\u89e3\u6790\u5668\uff1a<code>XStream xstream = new XStream(new JettisonMappedXmlDriver());<\/code><\/p>\n<h2>\u6df1\u5165\u4e86\u89e3<\/h2>\n<p><code>XStreamxstream = new XStream();<\/code>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cXStream\u4f1a\u91c7\u7528Xpp3\u5e93\uff0cXPP3\u662f\u4e00\u79cd\u8fd0\u884c\u6548\u7387\u975e\u5e38\u9ad8\u7684XML\u5168\u89e3\u6790\u5b9e\u73b0\u3002\u5982\u679c\u4e0d\u60f3\u4f9d\u9760Xpp3\u5e93\u7684\u8bdd\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u6807\u51c6\u7684JAXP DOM\u89e3\u6790\u5668\uff0c\u53ef\u4ee5\u91c7\u7528\u4ee5\u4e0b\u8bed\u53e5\u8fdb\u884c\u521d\u59cb\u5316\uff1a<\/p>\n<pre><code class=\"language-java\">\/\/\u4e0d\u4f7f\u7528XPP3\u5e93\nXStreamxstream = new XStream(new DomDriver());<\/code><\/pre>\n<p>\u6b64XStream\u5b9e\u4f8b\uff0c\u4e3a\u7ebf\u7a0b\u5b89\u5168\u7684\uff0c\u53ef\u4ee5\u4f9b\u591a\u4e2a\u7ebf\u7a0b\u8fdb\u884c\u8c03\u7528\uff0c\u5171\u4eab\u4f7f\u7528\u3002\u7cfb\u7edf\u63d0\u4f9b\u4e86\u591a\u79cd\u6807\u8bc6\u89e3\u6790\u5668\u4f9b\u6211\u4eec\u9009\u62e9\uff0c\u5305\u62ec<code>DomDriver<\/code>\u3001<code>JDomDriver<\/code>\u3001<code>StaxDriver<\/code>\u7b49\u7b49\u3002<\/p>\n<p>XStream\u63d0\u4f9b\u4e86\u5bf9Json\u7684\u652f\u6301\uff0c\u662f\u56e0\u4e3aXstream\u5185\u7f6e\u4e86\u4e24\u4e2aDriver\uff1a<\/p>\n<ol>\n<li><code>JsonHierarchicalStreamDriver<\/code>\uff1a\u4e0d\u4f9d\u8d56\u5176\u4ed6\u7c7b\u5e93\uff0c\u53ea\u5b9e\u73b0 obj-&gt;JSON<\/li>\n<li><code>JettisonMappedXmlDriver<\/code>\uff1a\u4f9d\u8d56jettison\u7c7b\u5e93\uff0c\u5b9e\u73b0 JSON-&gt;obj or obj-&gt;JSON<\/li>\n<\/ol>\n<p>\u4e24\u79cdDriver\u5728\u5904\u7406\u76f8\u540c\u8bbe\u7f6e\u7684Object\u65f6\u4f1a\u5f97\u5230\u4e0d\u540c\u7684JSON\u4e32\uff0c<code>JsonHierarchicalStreamDriver<\/code>\u5f97\u5230\u7684\u4e32\u66f4\u7b80\u6d01\uff0c\u786e\u5982\u5b98\u7f51\u6240\u8bf4\u3002<code>JettisonMappedXmlDriver<\/code>\u6709\u4e2a\u5c0f\u95ee\u9898\uff0c\u5373\u9ed8\u8ba4\u8f93\u51fa\u5e26\u683c\u5f0f\u7684JSON\u4e32\uff0c\u7ed3\u6784\u4e2d\u5e26\u7a7a\u683c\u3001\u6362\u884c\uff0c\u5e76\u4e14\u6ca1\u6709\u63d0\u4f9b\u4fee\u9970\u65b9\u5f0f\u3002<\/p>\n<p>\u603b\u7684\u6765\u8bf4\uff0cXStream\u4f7f\u7528\u8d77\u6765\u66f4\u7b80\u4fbf\uff0c\u4ee3\u7801\u8ddf\u7b80\u5355\uff0c\u4e5f\u5bb9\u6613\u7406\u89e3\uff0c\u5bf9\u4e8exml\u548cJson\u90fd\u63d0\u4f9b\u4e86\u652f\u6301\uff0c\u4e0d\u9700\u8981\u5176\u5b83\u8f85\u52a9\u7c7b\u548c\u6620\u5c04\u6587\u4ef6\uff0c\u4f7f\u5f97XML\uff0cJson\u5e8f\u5217\u5316\u4e0d\u518d\u7e41\u7410\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Xstream\u7b80\u4ecb Xstream\u662f\u4e00\u79cdOXMapping \u6280\u672f\uff0c\u662f\u7528\u6765\u5904\u7406XML\u6587\u4ef6\u5e8f\u5217\u5316\u7684\u6846\u67b6,\u5728\u5c06Jav [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[106],"class_list":["post-684","post","type-post","status-publish","format-standard","hentry","category-java-basic","tag-xstream"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/684","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=684"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/684\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=684"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=684"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=684"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}