{"id":876,"date":"2023-03-09T21:48:35","date_gmt":"2023-03-09T13:48:35","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=876"},"modified":"2023-04-29T16:42:13","modified_gmt":"2023-04-29T08:42:13","slug":"spring-boot-integrate-xstream-converter-handling-both-attributes-and-values-in-xml-nodes","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/09\/spring-boot-integrate-xstream-converter-handling-both-attributes-and-values-in-xml-nodes\/","title":{"rendered":"Spring Boot\u96c6\u6210XStream\u8f6c\u6362\u5668\uff1a\u5904\u7406xml\u8282\u70b9\u4e2d\u65e2\u6709\u5c5e\u6027\u53c8\u6709\u503c"},"content":{"rendered":"<h2>\u51c6\u5907\u5de5\u4f5c<\/h2>\n<p>\u9700\u5904\u7406\u7684\u6570\u636e<\/p>\n<pre><code class=\"language-xml\">&lt;cart_goods order_name=&quot;iPhone&quot; order_type=&quot;3C&quot; order_price=&quot;5000.00&quot;&gt;2&lt;\/cart_goods&gt;<\/code><\/pre>\n<p><!-- more --><\/p>\n<p>\u5904\u7406xml\u8282\u70b9\u4e2d\u65e2\u6709\u5c5e\u6027\u53c8\u6709\u503c\uff0c\u6709\u4e24\u79cd\u65b9\u5f0f<\/p>\n<ul>\n<li>\u4f7f\u7528Xstram\u81ea\u5e26\u7684\u8f6c\u6362\u5668<\/li>\n<li>\u81ea\u5b9a\u4e49\u7684\u8f6c\u6362\u5668<\/li>\n<\/ul>\n<h2>\u4f7f\u7528Xstram\u81ea\u5e26\u7684\u8f6c\u6362\u5668<\/h2>\n<h3>pom.xml\u4f9d\u8d56<\/h3>\n<pre><code>&lt;dependency&gt;\n    &lt;groupId&gt;com.thoughtworks.xstream&lt;\/groupId&gt;\n    &lt;artifactId&gt;xstream&lt;\/artifactId&gt;\n    &lt;version&gt;1.4.11.1&lt;\/version&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<h3>JavaBean\u5b9e\u4f53<\/h3>\n<pre><code class=\"language-java\">import com.thoughtworks.xstream.annotations.XStreamAlias;\nimport com.thoughtworks.xstream.annotations.XStreamAsAttribute;\nimport com.thoughtworks.xstream.annotations.XStreamConverter;\nimport com.thoughtworks.xstream.converters.extended.ToAttributedValueConverter;\nimport lombok.AllArgsConstructor;\nimport lombok.Data;\nimport lombok.NoArgsConstructor;\nimport me.yezhou.springboot.xml.converter.CartGoodsConverter;\n\n@Data\n@NoArgsConstructor\n@AllArgsConstructor\n@XStreamAlias(&quot;cart_goods&quot;)\n\/\/ \u81ea\u5e26\u7684\u8f6c\u6362\u5668\n\/\/ @XStreamConverter(value= ToAttributedValueConverter.class, strings={&quot;goodsQuantity&quot;})\n\/\/ \u81ea\u5b9a\u4e49\u7684\u8f6c\u6362\u5668\n@XStreamConverter(CartGoodsConverter.class)\npublic class CartGoods {\n    @XStreamAsAttribute\n    @XStreamAlias(&quot;goods_name&quot;)\n    private String goodsName;\n    @XStreamAsAttribute\n    @XStreamAlias(&quot;goods_category&quot;)\n    private String goodsCategory;\n    @XStreamAsAttribute\n    @XStreamAlias(&quot;goods_price&quot;)\n    private String goodsPrice;\n    \/\/ @XStreamOmitField\n    private String goodsQuantity;\n}<\/code><\/pre>\n<h3>\u6d4b\u8bd5<\/h3>\n<pre><code class=\"language-java\">@RunWith(SpringRunner.class)\n@SpringBootTest\npublic class SpringbootXmlApplicationTests {\n\n    @Test\n    public void parseCartGoodsWithXStreamConverter() {\n        CartGoods cartGoods = new CartGoods(&quot;iPhone&quot;, &quot;3C&quot;, &quot;5000.00&quot;, &quot;2&quot;);\n        \/\/XStream xStream = new XStream();\n        XStream xStream = new XStream(new XppDriver(new XmlFriendlyNameCoder(&quot;_-&quot;, &quot;_&quot;)));\/\/\u89e3\u51b3\u4e0b\u5212\u7ebf\u95ee\u9898\n        xStream.autodetectAnnotations(true); \/\/\u81ea\u52a8\u68c0\u6d4b\u6ce8\u89e3\n        xStream.processAnnotations(CartGoods.class); \/\/\u5e94\u7528Bean\u7c7b\u7684\u6ce8\u89e3\n        String toXML = xStream.toXML(cartGoods);\n        \/\/ \u5e8f\u5217\u5316\n        System.out.println(&quot;\u5e8f\u5217\u5316: &quot; + toXML);\n        \/\/ \u53cd\u5e8f\u5217\u5316\n        Object fromXML = xStream.fromXML(toXML);\n        System.out.println(&quot;\u53cd\u5e8f\u5217\u5316: &quot; + fromXML);\n    }\n\n}<\/code><\/pre>\n<pre><code>\u5e8f\u5217\u5316: &lt;cart_goods order_name=&quot;iPhone&quot; order_type=&quot;3C&quot; order_price=&quot;5000.00&quot;&gt;2&lt;\/cart_goods&gt;\n\u53cd\u5e8f\u5217\u5316: CartGoods(goodsName=iPhone, goodsCategory=3C, goodsPrice=5000.00, goodsQuantity=2)<\/code><\/pre>\n<h2>\u81ea\u5b9a\u4e49\u8f6c\u6362\u5668<\/h2>\n<pre><code class=\"language-java\">import com.thoughtworks.xstream.converters.Converter;\nimport com.thoughtworks.xstream.converters.MarshallingContext;\nimport com.thoughtworks.xstream.converters.UnmarshallingContext;\nimport com.thoughtworks.xstream.io.HierarchicalStreamReader;\nimport com.thoughtworks.xstream.io.HierarchicalStreamWriter;\nimport me.yezhou.springboot.xml.model.CartGoods;\n\npublic class CartGoodsConverter implements Converter {\n    \/**\n     * \u5c06java\u5bf9\u8c61\u8f6c\u4e3axml\u65f6\u4f7f\u7528\n     * @param source\n     * @param writer\n     * @param context\n     *\/\n    @Override\n    public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {\n        CartGoods cartGoods = (CartGoods) source;\n        writer.addAttribute(&quot;order_name&quot;, cartGoods.getGoodsName());\n        writer.addAttribute(&quot;order_type&quot;, cartGoods.getGoodsCategory());\n        writer.addAttribute(&quot;order_price&quot;, cartGoods.getGoodsPrice());\n        writer.setValue(cartGoods.getGoodsQuantity());\n    }\n\n    \/**\n     * \u5c06xml\u8f6c\u4e3ajava\u5bf9\u8c61\u4f7f\u7528\n     * @param reader\n     * @param context\n     * @return\n     *\/\n    @Override\n    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {\n        \/\/ \u5728\u89e3\u6790order\u5143\u7d20\u65f6\uff0c\u5148\u521b\u5efa\u4e00\u4e2aOrder\u5bf9\u8c61\n        CartGoods cartGoods = new CartGoods();\n        \/\/ \u5c06order\u5143\u7d20\u7684name\u5c5e\u6027\u8bbe\u7f6e\u4e3aOrder\u5bf9\u8c61\u7684name\u5c5e\u6027\u503c\n        cartGoods.setGoodsName(reader.getAttribute(&quot;order_name&quot;));\n        cartGoods.setGoodsCategory(reader.getAttribute(&quot;order_type&quot;));\n        cartGoods.setGoodsPrice(reader.getAttribute(&quot;order_price&quot;));\n        \/\/ \u5c06order\u5143\u7d20\u7684txt\u503c\u8bbe\u7f6e\u4e3aOrder\u5bf9\u8c61\u7684value\u503c\n        cartGoods.setGoodsQuantity(reader.getValue());\n        return cartGoods;\n    }\n\n    \/**\n     * \u8f6c\u6362\u6761\u4ef6\n     * @param type\n     * @return\n     *\/\n    @Override\n    public boolean canConvert(Class type) {\n        return type.equals(CartGoods.class);\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u51c6\u5907\u5de5\u4f5c \u9700\u5904\u7406\u7684\u6570\u636e &lt;cart_goods order_name=&quot;iPhone&#038;quot [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41],"tags":[247,106],"class_list":["post-876","post","type-post","status-publish","format-standard","hentry","category-spring-boot","tag-xml","tag-xstream"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/876","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=876"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/876\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=876"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=876"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=876"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}