java list to xml,使用Jackson XML映射器将Java List序列化为XML

Hi I need to create an XML from JAVA using Jackson-dataformat XMLMapper.

The XML should be like

1

Mighty Pulpo

austin

TX

Hong Kong

Hong Kong

But I get it always like with an extra "< addresses> < /addresses>" tag.

1

Mighty Pulpo

austin

TX

Hong Kong

Hong Kong

I am using below code to create XML

JaxbAnnotationModule jaxbAnnotationModule = new JaxbAnnotationModule();

XmlMapper mapper = new XmlMapper();

mapper.enable(SerializationFeature.INDENT_OUTPUT);

mapper.registerModule(jaxbAnnotationModule);

mapper.registerModule(new GuavaModule());

String xml = mapper.writeValueAsString(customer);

System.out.println(xml);

Please can some one help me? How can I remove the extra tag please. I have tried to use @XmlElement but it does not help help. TIA!!

解决方案

Try the below code

@JacksonXmlRootElement(localName = "customer")

class Customer {

@JacksonXmlProperty(localName = "id")

private int id;

@JacksonXmlProperty(localName = "name")

private String name;

@JacksonXmlProperty(localName = "addresses")

@JacksonXmlElementWrapper(useWrapping = false)

private Address[] address;

//getters, setters, toString

}

class Address {

@JacksonXmlProperty(localName = "city")

private String city;

@JacksonXmlProperty(localName = "state")

private String state;

// getter/setter

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用JavaXML解析器解析XML文件,并返回一个BigDecimal类型List的示例: ```java import java.io.File; import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class XmlBigDecimalListExample { public static void main(String[] args) { List<BigDecimal> bigDecimalList = parseXmlFile("example.xml"); System.out.println(bigDecimalList); } private static List<BigDecimal> parseXmlFile(String fileName) { List<BigDecimal> bigDecimalList = new ArrayList<BigDecimal>(); try { File xmlFile = new File(fileName); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(xmlFile); doc.getDocumentElement().normalize(); NodeList nodeList = doc.getElementsByTagName("number"); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) node; BigDecimal bigDecimal = new BigDecimal(element.getTextContent()); bigDecimalList.add(bigDecimal); } } } catch (Exception e) { e.printStackTrace(); } return bigDecimalList; } } ``` 在本示例中,我们首先定义了一个名为`parseXmlFile`的方法,该方法接受一个XML文件名称作为参数,并返回一个包含`BigDecimal`类型元素的`List`。然后,在`parseXmlFile`方法中,我们使用Java内置的XML解析器(`DocumentBuilder`和`DocumentBuilderFactory`)从XML文件中提取number元素,并将其转换为`BigDecimal`类型,最后将其添加到`bigDecimalList`中。最后,我们在示例的主方法中调用`parseXmlFile`方法,并打印结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值