xml字符串转java对象

 

下面是一个简单的例子

 

package com.yydone.dto;

import java.io.Serializable;
import java.util.List;

public class ItemInfoDto implements Serializable {

	private static final long serialVersionUID = 1L;
	
	private Integer itemCount;
	private List<ItemDto> itemList;
	
	public Integer getItemCount() {
		return itemCount;
	}
	public void setItemCount(Integer itemCount) {
		this.itemCount = itemCount;
	}
	public List<ItemDto> getItemList() {
		return itemList;
	}
	public void setItemList(List<ItemDto> itemList) {
		this.itemList = itemList;
	}
}

 

package com.yydone.dto;

import java.io.Serializable;
import java.math.BigDecimal;

public class ItemDto implements Serializable {

	private static final long serialVersionUID = 1L;
	
	private String itemProductName;
	private BigDecimal itemUnitPrice;
	private Integer itemQuantity;
	
	public String getItemProductName() {
		return itemProductName;
	}
	public void setItemProductName(String itemProductName) {
		this.itemProductName = itemProductName;
	}
	public BigDecimal getItemUnitPrice() {
		return itemUnitPrice;
	}
	public void setItemUnitPrice(BigDecimal itemUnitPrice) {
		this.itemUnitPrice = itemUnitPrice;
	}
	public Integer getItemQuantity() {
		return itemQuantity;
	}
	public void setItemQuantity(Integer itemQuantity) {
		this.itemQuantity = itemQuantity;
	}
	
	
}

 

package com.test.util;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.junit.Test;

import com.yydone.dto.ItemDto;
import com.yydone.dto.ItemInfoDto;
import com.thoughtworks.xstream.XStream;

public class TestXmlUtil {

	@Test
	public void parse() {
		String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
				"<ItemInfo>" +
				"	<ItemCount>2</ItemCount>" +
				"	<ItemList>" +
				"		<Item>" +
				"			<ItemProductName>商品1</ItemProductName>" +
				"			<ItemUnitPrice>5.63</ItemUnitPrice>" +
				"			<ItemQuantity>1</ItemQuantity>" +
				"		</Item>" +
				"		<Item>" +
				"			<ItemProductName>商品2</ItemProductName>" +
				"			<ItemUnitPrice>8.36</ItemUnitPrice>" +
				"			<ItemQuantity>2</ItemQuantity>" +
				"		</Item>" +
				"   </ItemList>" +
				"</ItemInfo>";
		
		XStream xStream = new XStream();
		xStream.alias("ItemInfo", ItemInfoDto.class);
		xStream.alias("Item", ItemDto.class);
		xStream.aliasField("ItemCount", ItemInfoDto.class, "itemCount");
		xStream.aliasField("ItemList", ItemInfoDto.class, "itemList");
		
		xStream.aliasField("ItemProductName", ItemDto.class, "itemProductName");
		xStream.aliasField("ItemUnitPrice", ItemDto.class, "itemUnitPrice");
		xStream.aliasField("ItemQuantity", ItemDto.class, "itemQuantity");
		ItemInfoDto dto = (ItemInfoDto)xStream.fromXML(xml);
		System.out.println(ToStringBuilder.reflectionToString(dto));
	}
}

需要依赖的 xml的pom为:

<dependency>
	<groupId>xstream</groupId> 
	<artifactId>xstream</artifactId> 
	<version>1.2.2</version> 
</dependency>

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,可以使用JAXB(Java Architecture for XML Binding)来实现XML字符串Java对象之间的换。 1. 将Java对象换为XML字符串 使用JAXB的Marshaller类可以将Java对象换为XML字符串。以下是一个示例代码: ```java // 创建JAXBContext对象 JAXBContext jaxbContext = JAXBContext.newInstance(Student.class); // 创建Marshaller对象 Marshaller marshaller = jaxbContext.createMarshaller(); // 设置Marshaller的属性 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); // 将Java对象换为XML字符串 Student student = new Student("Tom", 18); StringWriter writer = new StringWriter(); marshaller.marshal(student, writer); String xmlString = writer.toString(); System.out.println(xmlString); ``` 2. 将XML字符串换为Java对象 使用JAXB的Unmarshaller类可以将XML字符串换为Java对象。以下是一个示例代码: ```java // 创建JAXBContext对象 JAXBContext jaxbContext = JAXBContext.newInstance(Student.class); // 创建Unmarshaller对象 Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); // 将XML字符串换为Java对象 String xmlString = "<student><name>Tom</name><age>18</age></student>"; StringReader reader = new StringReader(xmlString); Student student = (Student) unmarshaller.unmarshal(reader); System.out.println(student.getName() + ", " + student.getAge()); ``` 需要注意的是,在将Java对象换为XML字符串和将XML字符串换为Java对象时,都需要先创建JAXBContext对象。JAXBContext是线程安全的,因此建议在应用程序启动时创建一次即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值